C++ Standard Library: Associative Containers
This course will teach you in a practical way, with slides and demo code, how to use important associative containers available in the C++ Standard Library (e.g. std::map). You will learn their pros and cons, and common operations.
What you'll learn
Non-trivial computer programs need to store data and process it. Developing data structures and containers from scratch in C++ is a daunting and bug-prone task. In fact, you would have to consider advanced aspects like manual memory and resource management, proper handling subtle corner cases, writing both generic code and code that is optimized for special cases, and so on. Moreover, there are some data structures like balanced binary search trees or hash tables that are very useful in applications; however, it is definitely non-trivial to implement from scratch.
Thankfully, the C++ Standard Library already offers containers that are high-quality, efficient, and well-tested. It’s a joy to develop C++ code reusing them: in fact, using these containers in your C++ code will boost your productivity, as the C++ Standard Library gives you on a platter high-quality implementations of common and useful data structures that you can easily leverage in your C++ code. C++ Standard Library’s maintainers spend hours looking at these data structures, and improving, refining, and optimizing their C++ implementation code. In this course, C++ Standard Library: Associative Containers, you will learn how to simply reuse that treasure in your own C++ applications.
First, you will explore std::map, which isa very versatile and convenient associative container that you can use when you need to associate some unique keys to additional values. This can come in handy in lots of applications, from translation dictionaries, to computer graphics programs to databases.
Next, you will discover important operations, like inserting, removing, and searching elements. You will first see them described in theory using slides, including a discussion of their asymptotic runtime complexity. Then, you’ll also see them in action in practical demo code. You will also touch on how to achieve even better performance than std::map, using std::unordered_map. You’ll see how that comes at a cost, though, like losing element ordering that is guaranteed by std::map. Then, you will see a practical demo code with a benchmark comparing std::unordered_map vs. std::map performance in action.
Finally, you will learn how to easily store unique elements in a specific container, maintaining element ordering. This is possible using std::set. You will see how to store custom objects in std::set, including how to fix a subtle bug. I hope that discussing subtle bugs that are especially frequent for those starting to learn the C++ Standard Library containers will save you precious time and headache during your C++ programming. To proficiently follow this course, you only need a basic knowledge of C++ language features. You also need to know some basic architectural elements ofthe C++ Standard Library, like iterators, that are described in my “C++ Standard Library: Sequential Containers” course.
After completing this course, you will be able to use high-quality efficient and well-tested C++ Standard Library associative containers like std::map, std::unordered_map and std::set in your own C++ code. You will have practical knowledge about them, and be able to make proper judgement about picking one or the other based on the problem at hand. You will also have knowledge about important common operations with these standard associative containers such as inserting, removing, and searching elements.
Table of contents
- Introduction 2m
- Introducing std::set 3m
- Creating std::set and Querying Common Attributes 2m
- Inserting New Elements in std::set 4m
- Iterating through std::set Elements 2m
- Removing Elements from std::set 3m
- Looking up Elements in std::set 3m
- Demo: std::set in Action 4m
- Demo: Storing Objects of Your Own Classes in std::set 3m
- Summary 2m
- Introduction 2m
- Why std::map? 2m
- Creating and Initializing std::map 1m
- Inserting and Looking up Associations with map::operator 3m
- Iterating through std::map Associations 3m
- Demo: std::map in Action with Number Pronunciations 3m
- Demo: Subtle Bug with std::map’s operator 3m
- Searching Associations with the map::find Method 4m
- Demo: Fixing the Subtle Bug when Looking up Items in Constant std::map 2m
- Demo: Implementing a Simple English-Italian Dictionary with std::map 3m
- Demo: Implementing a Simple Airport Database with std::map 4m
- Removing Associations from std::map 3m
- Summary 3m
- Introduction 1m
- Introducing std::unordered_map 3m
- unordered_map vs. map: Element Sorting 1m
- unordered_map vs. map Under the Hood: Performance Differences 6m
- Demo: unordered_map vs. map Performance in Action: Counting Words 7m
- A Brief Touch on Using Custom Classes as Keys 2m
- Recommendations to Learn More 1m
- Summary and Thank You 2m