C++ std::map find() Function

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

Definition

In C++, std::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 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 <map> to use it.

This is a member of std::map<Key, T>, a sorted container of key and value pairs. Each key is unique and the pairs are always kept in key order, so a map can find, insert, and erase a key in logarithmic time. 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 comparator

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 <map>
#include <string>

int main() {
    std::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") searches the sorted keys 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 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 <map>
#include <string>

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

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

    for (const auto& kv : score) {
        std::cout << kv.first << ": " << kv.second << '\n';
    }
    return 0;
}

How It Works

  1. find("Ann") returns an iterator to the pair. The 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 map. No second search is needed.
  3. Ben was never touched. The loop prints both pairs in key order.

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 <map>
#include <string>

int valueOr(const std::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::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 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 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 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 logarithmic time because a map is a balanced tree. For a million keys, that is about 20 comparisons.

Next: get_allocator()

Keep going through the std::map functions. Get a copy of the allocator the 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.

14 people found this page helpful