C++ std::unordered_map find() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::unordered_map

Definition

In C++, std::unordered_map::find() searches for a key and returns an iterator to the pair that has it. Through the iterator you can read it->first (the key) and it->second (the value).

If the key is not in the unordered_map, find() returns end(). Always compare with end() before you use the result. Unlike m[key], find() never adds a key, and unlike at() it never throws. Include <unordered_map> to use it.

This is a member of std::unordered_map<Key, T>, a hash table of key and value pairs. Each key is unique. Find, insert, and erase are usually average constant time. Unlike std::map, keys are not sorted and iteration order is unspecified. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.

Syntax

Pass the key and compare the result with end().

C++
auto it = m.find("Raj");
if (it != m.end()) {
    std::cout << it->first << " " << it->second;
}
CallWhat it does
m.find(key)Returns an iterator to the pair with key, or m.end() if it is not found.
cm.find(key)On a const map it returns a const_iterator, which can read but not change the value.

Never write *m.find(k) without checking. When the key is missing, find() returns end(), and dereferencing end() is undefined behavior.

Library declarations
C++
iterator find(const Key& key);
const_iterator find(const Key& key) const;
template<class K> iterator find(const K& x);   // transparent hash/eq

Parameters

ParameterDescription
keyThe key to search for.

Return Value

Return ValueDescription
iteratorAn iterator to the pair whose key matches. It is end() when no key matches. For a const map it is a const_iterator.

C++ Examples

Example 1 — find() Returns an Iterator

Search for a key that exists and one that does not.

C++
#include <iostream>
#include <unordered_map>
#include <string>

int main() {
    std::unordered_map<std::string, int> age = {{"Mia", 21}, {"Raj", 25}, {"Zoe", 30}};

    auto it = age.find("Raj");
    if (it != age.end()) {
        std::cout << it->first << " is " << it->second << '\n';
    }

    if (age.find("Tom") == age.end()) {
        std::cout << "Tom was not found\n";
    }
    return 0;
}

How It Works

  1. find("Raj") hashes the key and returns an iterator to the Raj pair. It is not end(), so it is safe to use.
  2. it->first is the key "Raj" and it->second is the value 25.
  3. find("Tom") finds nothing, so it returns age.end(). The comparison is true and the message prints. The unordered_map is not changed.

Example 2 — find() Lets You Change a Value

Use the iterator from find() to update the value in place.

C++
#include <iostream>
#include <unordered_map>
#include <string>

int main() {
    std::unordered_map<std::string, int> score = {{"Ann", 50}, {"Ben", 70}};

    auto it = score.find("Ann");
    if (it != score.end()) {
        it->second += 25;
    }

    std::cout << "Ann: " << score.at("Ann") << '\n';
    std::cout << "Ben: " << score.at("Ben") << '\n';
    return 0;
}

How It Works

  1. find("Ann") returns an iterator to the pair. The unordered_map is not const, so the iterator allows changes to the value.
  2. it->second += 25 changes the stored value from 50 to 75 directly inside the unordered_map. No second search is needed.
  3. Ben was never touched. Both values are printed with at().

Example 3 — find() in a Helper With a Fallback

Write a small function that returns a default value instead of adding a key or throwing.

C++
#include <iostream>
#include <unordered_map>
#include <string>

int valueOr(const std::unordered_map<std::string, int>& m, const std::string& key, int fallback) {
    auto it = m.find(key);
    if (it == m.end()) {
        return fallback;
    }
    return it->second;
}

int main() {
    std::unordered_map<std::string, int> stock = {{"apple", 5}, {"pear", 0}};

    std::cout << "apple: " << valueOr(stock, "apple", -1) << '\n';
    std::cout << "kiwi: " << valueOr(stock, "kiwi", -1) << '\n';
    std::cout << "size: " << stock.size() << '\n';
    return 0;
}

How It Works

  1. The function takes the unordered_map by const reference. That is why it uses find(): operator[] does not work on a const map.
  2. For "apple", find() succeeds and the function returns the stored 5. For "kiwi" it returns end(), so the function gives back the fallback -1.
  3. The size is still 2, which proves that looking up a missing key with find() adds nothing.

Frequently Asked Questions

An iterator to the pair that has the key, or end() if the key is not in the unordered_map.
Compare the result with m.end(). If it is not equal to end(), the key was found.
find() never adds a key. m[key] adds the key with a default value when it is missing, which changes the unordered_map and its size.
at() returns the value and throws std::out_of_range when the key is missing. find() returns an iterator and gives end() for a missing key, with no exception.
It takes average constant time because an unordered_map is a hash table. For a million keys, that is usually a few hash lookups.

Next: get_allocator()

Keep going through the std::unordered_map functions. Get a copy of the allocator the unordered_map uses for its nodes.

get_allocator() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful