unordered map guide

1. Include Header

#include <unordered_map>

2. Declaration

std::unordered_map<KeyType, ValueType> map_name;

3. Insertion


map_name[key] = value;
map_name.insert({key, value});

4. Access Value

ValueType val = map_name[key];

5. Check if Key Exists


if (map_name.find(key) != map_name.end()) {
  // Key exists
}

6. Erase an Element

map_name.erase(key);

7. Iterate Through Elements


for (const auto& pair : map_name) {
  std::cout << pair.first << ": " << pair.second << std::endl;
}

8. Size and Empty Check


map_name.size();     // Returns number of elements
map_name.empty();    // Returns true if empty

edit this page