unordered_map


Header: <unordered_map>

The <unordered_map> header, introduced in C++11, provides the std::unordered_map and std::unordered_multimap container classes. These are associative containers that store key-value pairs with unique keys (for unordered_map) or multiple keys (for unordered_multimap). They use hash tables for storage, offering average constant-time complexity for search, insertion, and deletion operations.

Key Characteristics

Key Components

  1. std::unordered_map: Container with unique keys
  2. std::unordered_multimap: Container allowing multiple entries with the same key
  3. Hash function and equality comparison function objects

Example 1: Basic Usage of unordered_map

#include <iostream>
#include <unordered_map>
#include <string>

int main() {
    std::unordered_map<std::string, int> age_map;

    // Inserting elements
    age_map["Alice"] = 30;
    age_map["Bob"] = 25;
    age_map.insert({"Charlie", 35});

    // Accessing elements
    std::cout << "Alice's age: " << age_map["Alice"] << std::endl;

    // Checking if a key exists
    if (age_map.find("David") == age_map.end()) {
        std::cout << "David not found in the map" << std::endl;
    }

    // Iterating through the map
    for (const auto& pair : age_map) {
        std::cout << pair.first << " is " << pair.second << " years old" << std::endl;
    }

    return 0;
}

Explanation

Example 2: Using unordered_multimap

#include <iostream>
#include <unordered_map>
#include <string>

int main() {
    std::unordered_multimap<std::string, std::string> country_capitals;

    // Inserting multiple entries for the same key
    country_capitals.insert({"USA", "Washington D.C."});
    country_capitals.insert({"USA", "New York City"});
    country_capitals.insert({"France", "Paris"});
    country_capitals.insert({"Japan", "Tokyo"});

    // Counting entries for a key
    std::cout << "USA has " << country_capitals.count("USA") << " entries" << std::endl;

    // Finding all entries for a key
    auto range = country_capitals.equal_range("USA");
    for (auto it = range.first; it != range.second; ++it) {
        std::cout << "USA: " << it->second << std::endl;
    }

    return 0;
}

Explanation

Example 3: Custom Hash Function

#include <iostream>
#include <unordered_map>
#include <string>

struct Point {
    int x, y;
    bool operator==(const Point& other) const {
        return x == other.x && y == other.y;
    }
};

namespace std {
    template <>
    struct hash<Point> {
        size_t operator()(const Point& p) const {
            return hash<int>()(p.x) ^ (hash<int>()(p.y) << 1);
        }
    };
}

int main() {
    std::unordered_map<Point, std::string> point_map;

    point_map[{1, 2}] = "Point A";
    point_map[{3, 4}] = "Point B";

    for (const auto& pair : point_map) {
        std::cout << "Point (" << pair.first.x << ", " << pair.first.y << "): " 
                  << pair.second << std::endl;
    }

    return 0;
}

Explanation

Additional Considerations

Summary

The <unordered_map> header in C++ provides hash table-based associative containers that offer fast average-case performance for key-based operations. std::unordered_map and std::unordered_multimap are useful when you need quick lookups and don't require the elements to be sorted.

Key points to remember:

  1. unordered_map stores unique key-value pairs, while unordered_multimap allows multiple values per key
  2. Average time complexity for search, insertion, and deletion is O(1)
  3. Elements are not stored in any particular order
  4. Custom types can be used as keys by providing a hash function and equality operator
  5. These containers are particularly useful for scenarios requiring frequent lookups in large datasets

By using unordered_map or unordered_multimap, C++ developers can implement efficient key-value storage and retrieval mechanisms, especially in situations where fast lookups are crucial and the order of elements is not important.

Previous Page | Course Schedule | Course Content