unordered_multimap


Header: <unordered_multimap>

The <unordered_multimap> header provides the std::unordered_multimap container, which is an associative container that stores key-value pairs with non-unique keys. It uses hashing to provide average constant-time complexity for search, insertion, and deletion operations.

Key Characteristics

Key Components

  1. std::unordered_multimap: The main container class
  2. Hash function and equality comparison function objects

Example 1: Basic Usage

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

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

    // Inserting elements
    umm.insert({"apple", 1});
    umm.insert({"banana", 2});
    umm.insert({"apple", 3});  // Duplicate key

    // Iterating and printing
    for (const auto& pair : umm) {
        std::cout << pair.first << ": " << pair.second << std::endl;
    }

    // Counting elements with a specific key
    std::cout << "Count of 'apple': " << umm.count("apple") << std::endl;

    return 0;
}

Example 2: Using equal_range

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

int main() {
    std::unordered_multimap<std::string, int> umm;
    umm.insert({"x", 1});
    umm.insert({"y", 2});
    umm.insert({"x", 3});
    umm.insert({"x", 4});

    auto range = umm.equal_range("x");
    std::cout << "Values for key 'x':" << std::endl;
    for (auto it = range.first; it != range.second; ++it) {
        std::cout << it->second << std::endl;
    }

    return 0;
}

Example 3: Custom Hash Function

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

struct CaseInsensitiveHash {
    size_t operator()(const std::string& key) const {
        size_t hash = 0;
        for (char c : key) {
            hash = hash * 31 + std::tolower(c);
        }
        return hash;
    }
};

struct CaseInsensitiveEqual {
    bool operator()(const std::string& left, const std::string& right) const {
        return std::equal(left.begin(), left.end(), right.begin(), right.end(),
                          [](char a, char b) { return std::tolower(a) == std::tolower(b); });
    }
};

int main() {
    std::unordered_multimap<std::string, int, CaseInsensitiveHash, CaseInsensitiveEqual> umm;

    umm.insert({"Apple", 1});
    umm.insert({"apple", 2});
    umm.insert({"APPLE", 3});

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

    return 0;
}

Additional Considerations

Summary

std::unordered_multimap is a powerful container in C++ for storing multiple key-value pairs with the same key, offering fast average-case lookup times. It's particularly useful in scenarios where you need to associate multiple values with a single key and require quick access to these associations. The ability to use custom hash and equality functions allows for flexible key comparisons, as demonstrated in the case-insensitive string example.

Key points to remember:

  1. Allows duplicate keys
  2. Uses hashing for fast average-case access
  3. Elements are not stored in any particular order
  4. Provides methods like insert(), erase(), find(), and equal_range() for manipulation and access
  5. Can be customized with user-defined hash and equality functions

By leveraging unordered_multimap, C++ developers can implement efficient data structures for scenarios requiring fast lookups with non-unique keys, such as in certain types of caches, indexes, or when modeling many-to-many relationships.

Citations:

[1] https://learn.microsoft.com/en-us/cpp/standard-library/unordered-multimap-class?view=msvc-170 [2] https://cpp-lang.net/docs/std/containers/maps/unordered-multimap/ [3] https://en.cppreference.com/w/cpp/container/unordered_multimap [4] https://www.geeksforgeeks.org/unordered_multimap-and-its-application/ [5] https://www.geeksforgeeks.org/unordered_multimap-insert-in-c-stl/ [6] https://www.programiz.com/cpp-programming/unordered-multimap [7] https://gamedev.stackexchange.com/questions/80832/how-can-i-correctly-use-an-unordered-multimap-as-entity-and-component-storage [8] https://stackoverflow.com/questions/37729367/guarantees-about-key-uniqueness-in-stdunordered-multimap

Previous Page | Course Schedule | Course Content