Map
Initialization
std::map<int, std::string> myMap;
std::unordered_map<int, std::string> myMap = {
{1, "one"},
{2, "two"},
{3, "three"}
};
Insert Elements
myMap[1] = "one"; // Insert key-value pair
// or
myMap.insert(std::make_pair(1, "one")); // Insert key-value pair
myMap.emplace(1, "one"); // create key-value pair directly
Access Elements
std::string value = myMap[1]; // Access value by key, returns std::string
auto it = myMap.find(1); // Returns iterator to the element if key exists, otherwise returns myMap.end()
Removing Elements
myMap.erase(1); // Removes element with key 1, returns number of elements removed (size_t)
Query Attributes
size_t size = myMap.size(); // Returns the number of elements in the map (size_t)
bool isEmpty = myMap.empty(); // Returns true if the map is empty, otherwise false (bool)
Iterating through Map
for (auto it = myMap.begin(); it != myMap.end(); ++it) {
// it->first: key
// it->second: value
}
- Using Range-based For Loop:
for (const auto& [key, value] : myMap) {
// key: key
// value: value
}
Other Common Operations
myMap.clear(); // Removes all elements from the map