Skip to content
C++: Use a composite key in an unordered map

C++: Use a composite key in an unordered map

November 12, 2026

A lookup may need both a region and a numeric identifier. Give that pair a key type, equality, and a hash function so callers do not have to construct ambiguous concatenated strings.

Prepare a scratch directory

Tested on Linux with GCC 14.2.0 in C++20 mode. You need Bash, a C++20 compiler and standard library, a text editor, and the mktemp, rm, and rmdir utilities. Keep the same terminal open. Stop if setup fails, before saving the source:

start_directory="$PWD"
work_directory="$(mktemp -d)" || exit 1
cd -- "$work_directory" || exit 1

Run the example

Save as cpp-unordered-map-composite-key.cpp:

#include <cassert>
#include <cstddef>
#include <functional>
#include <iostream>
#include <string>
#include <unordered_map>

struct Key {
    std::string region;
    int id;
    bool operator==(const Key&) const = default;
};

struct KeyHash {
    std::size_t operator()(const Key& key) const {
        const auto first = std::hash<std::string>{}(key.region);
        const auto second = std::hash<int>{}(key.id);
        return first ^ (second + std::size_t{0x9e3779b9} +
                        (first << 6) + (first >> 2));
    }
};

int main() {
    std::unordered_map<Key, std::string, KeyHash> names;
    names.emplace(Key{"west", 7}, "Ada");
    names.emplace(Key{"east", 7}, "Lin");
    assert(names.size() == 2);
    assert(KeyHash{}(Key{"west", 7}) == KeyHash{}(Key{"west", 7}));
    std::cout << names.at(Key{"west", 7}) << '\n';
    std::cout << names.at(Key{"east", 7}) << '\n';
    assert(!names.contains(Key{"west", 8}));
}

Compile and run:

g++ -std=c++20 -Wall -Wextra -Werror -pedantic cpp-unordered-map-composite-key.cpp -o cpp-unordered-map-composite-key &&
./cpp-unordered-map-composite-key

Observed output:

Ada
Lin

How it works

The defaulted equality operator compares both fields. Equal keys therefore feed equal field values into the hash. Collisions between unequal keys are allowed: the container still compares equality within a bucket. The example prints explicit lookups because unordered-map iteration order is unspecified.

Limits and choices

This demonstration combiner provides neither cryptographic protection nor a defense against hostile collision-heavy input. Standard hash values are not persistent identifiers and need not be stable between executions. If equality later ignores case or normalizes a field, update hashing to use the same equivalence rule.

The C++ working draft specifies the library operation used here. The example uses the C++20 interface tested above; newer draft additions are outside its scope.

Clean up

rm -- cpp-unordered-map-composite-key.cpp cpp-unordered-map-composite-key
cd -- "$start_directory" || exit 1
rmdir -- "$work_directory"
unset start_directory work_directory

If the directory is not empty, inspect the remaining files before removing anything else.

Acknowledgements

This post was created with assistance from AI.

Last updated on