In the world of programming, managing data efficiently is crucial. C++ Standard Template Library (STL) provides several containers that help in organizing and manipulating data effectively. Two such powerful containers are std::map and std::multimap. These containers store elements as key-value pairs, where each key is unique for std::map, but can be duplicated in std::multimap.
Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order. The key serves as an index to access the corresponding mapped value. Maps automatically sort their elements based on the keys.
Multimaps, on the other hand, allow duplicate keys but maintain the same ordering criteria for the elements. This makes multimaps useful when you need to store multiple entries with the same key.
Understanding and effectively using std::map and std::multimap can significantly enhance your ability to manage data in C++ applications.
Both std::map and std::multimap store elements as pairs of keys and values. The key is used for sorting and accessing the elements, while the value holds the actual data associated with that key.
1#include <iostream>2#include <map>34int main() {5std::map<int, std::string> myMap;6myMap[1] = "One";7myMap[2] = "Two";8myMap[3] = "Three";910for (const auto& pair : myMap) {11std::cout << pair.first << ": " << pair.second << std::endl;12}1314return 0;15}
To retrieve an element from a map, you can use the find() method or directly access it using the key.
find()1#include <iostream>2#include <map>34int main() {5std::map<int, std::string> myMap;6myMap[1] = "One";7myMap[2] = "Two";8myMap[3] = "Three";910auto search = myMap.find(2);11if (search != myMap.end()) {12std::cout << "Found: " << search->first << ": " << search->second << std::endl;13} else {14std::cout << "Not found" << std::endl;15}1617return 0;18}
Maps provide iterators to traverse the elements in a sorted order.
1#include <iostream>2#include <map>34int main() {5std::map<int, std::string> myMap;6myMap[1] = "One";7myMap[2] = "Two";8myMap[3] = "Three";910for (auto it = myMap.begin(); it != myMap.end(); ++it) {11std::cout << it->first << ": " << it->second << std::endl;12}1314return 0;15}
The operator[] can be used to access and modify the value associated with a key. If the key does not exist, it is inserted with a default-constructed value.
operator[]1#include <iostream>2#include <map>34int main() {5std::map<int, std::string> myMap;6myMap[1] = "One";7myMap[2] = "Two";8myMap[3] = "Three";910std::cout << "Element at key 2: " << myMap[2] << std::endl;1112// Accessing a non-existent key13std::cout << "Element at key 4 (default constructed): " << myMap[4] << std::endl;14std::cout << "Size after accessing key 4: " << myMap.size() << std::endl;1516return 0;17}
| Concept | Description |
|---|---|
| Key-Value Pairs | Maps store elements as key-value pairs where keys are unique. |
| Insertion | Elements can be inserted using insert() or by direct assignment. |
| Retrieval | Use find() to locate an element, or access it directly via the key. |
| Deletion | Use erase() to remove elements from a map. |
| Iteration | Maps provide iterators for traversing elements in sorted order. |
| Multimap | Allows duplicate keys and maintains ordering. |
| Operator[] | Accesses or modifies the value associated with a key, inserting if necessary. |
In the next section, we will explore std::set and std::multiset, which are containers that store unique elements in sorted order. These containers are useful when you need to manage collections of items without duplicates and require efficient search operations.
Stay tuned for more insights into C++ STL!