map — sorted (red-black tree). unordered_map — hash table.
map:
1#include <map>23std::map<int, std::string> m;4m[3] = "three";5m[1] = "one";6m[2] = "two";78// Sorted: 1, 2, 39for (const auto& [key, value] : m) {10 std::cout << key << ": " << value << "\n";11}
unordered_map:
1#include <unordered_map>23std::unordered_map<int, std::string> um;4um[3] = "three";5um[1] = "one";6um[2] = "two";78// Unordered: 3, 1, 2 (or any order)9for (const auto& [key, value] : um) {10 std::cout << key << ": " << value << "\n";11}
When to use: