Lesson 3
Diving into Associative Containers with C++
Exploring Maps and Multimaps in C++

Welcome to this lesson on Maps and Multimaps in C++, integral parts of the C++ Standard Library. These associative containers like std::map and std::multimap are used to store key-value pairs, maintaining an organized order of elements. Understanding these containers enhances your ability to manage data efficiently in C++.

Understanding std::map

The std::map is a key-value pair container that maintains order based on keys. It ensures each key is unique and directly employs a binary search tree to manage this order. Here's how you can create and utilize a std::map to track fruit quantities:

C++
1#include <iostream> 2#include <map> 3 4int main() { 5 // Create a map with fruits as keys and their counts as values 6 std::map<std::string, int> fruitMap = { 7 {"banana", 3}, 8 {"apple", 4}, 9 {"pear", 1}, 10 {"orange", 2} 11 }; 12 13 // Access and modify elements 14 fruitMap["banana"] = 5; // Update the banana count 15 16 // Insert a new fruit 17 fruitMap.insert({"mango", 2}); 18 19 // Check if a key exists 20 auto search = fruitMap.find("apple"); 21 if (search != fruitMap.end()) { 22 // .end() provides an iterator to the position just after the last element 23 std::cout << "Found 'apple' with count: " << search->second << std::endl; 24 } 25 26 // Iterate over the map 27 for (const auto& [key, value] : fruitMap) { 28 // 'key' refers to first and 'value' refers to second in key-value pair 29 std::cout << key << ": " << value << std::endl; 30 } 31 32 return 0; 33}

A std::map provides numerous methods to manage and traverse its elements efficiently. Here are some key operations:

  • insert: Adds a new key-value pair while maintaining sorted order.
  • find: Locates an element by its key and returns an iterator to it. If the element isn’t found, it returns end(), which is an iterator pointing just past the last element.
  • erase: Removes an element by its key or by iterator.
  • Iteration: Traverse the map's elements ordered by key, where the key is accessed via the first member and the value via second of the key-value pair.
Delving into std::multimap

The std::multimap deals with scenarios where keys can have multiple associated values. This is particularly useful when you need to store duplicate keys with differing values. Like std::map, std::multimap maintains an order based on keys but allows the same key to appear multiple times. Let's explore how you can leverage a std::multimap:

C++
1#include <iostream> 2#include <map> 3 4int main() { 5 // Create a multimap with fruits as keys and their counts as values 6 std::multimap<std::string, int> fruitMultiMap; 7 fruitMultiMap.insert({"banana", 3}); 8 fruitMultiMap.insert({"apple", 4}); 9 fruitMultiMap.insert({"banana", 1}); 10 fruitMultiMap.insert({"orange", 2}); 11 12 // Find and output all values associated with "banana" 13 auto range = fruitMultiMap.equal_range("banana"); 14 // range.first is the start iterator and range.second is the end iterator 15 for (auto it = range.first; it != range.second; ++it) { 16 std::cout << "banana: " << it->second << std::endl; // Output: banana: 3, banana: 1 17 } 18 19 // Iterate over the multimap 20 for (const auto& [key, value] : fruitMultiMap) { 21 std::cout << key << ": " << value << std::endl; 22 // Output: 23 // apple: 4 24 // banana: 3 25 // banana: 1 26 // orange: 2 27 } 28 29 return 0; 30}

Many methods are common between std::map and std::multimap, which include:

  • insert: Adds a key-value pair, allowing duplicate keys in std::multimap.
  • find: Searches for an element by its key and returns an iterator to the first pair with that key.
  • erase: Removes elements based on the key or iterator.

The equal_range method is particularly useful for accessing all elements with a given key, as it provides a pair of iterators marking the beginning and end of the matching range within the multimap. This is an important distinction because std::multimap doesn’t have an operator [] due to the possibility of duplicate keys, necessitating other methods for accessing all associated values. Understanding these shared and unique methods empowers you to effectively manipulate data with std::multimap, tailoring it more precisely to your application’s needs.

Lesson Summary

Congratulations on expanding your understanding of Maps and Multimaps in C++! You've explored the ordered nature of std::map with unique keys and std::multimap allowing key multiplicity. By mastering these concepts, your applications can handle organized and efficient data storage and retrieval. Keep practicing to refine your skills with C++'s powerful associative containers!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.