C++ std::map contains() Function

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

Definition

In C++20, std::map::contains() answers one question: is this key in the map? It returns true or false.

It does not read the value and it does not add the key. It replaces the older style m.count(key) > 0 or m.find(key) != m.end() with a name that says exactly what you mean. Include <map> and use -std=c++20 or newer.

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. Use the bool result in an if.

C++
if (m.contains("Raj")) {
    // the key exists
}
bool has = m.contains("Zoe");
CallWhat it does
m.contains(key)Returns true if the key is in the map, and false if it is not.

If your compiler is older than C++20, write m.count(key) != 0 or m.find(key) != m.end(). They give the same answer.

Library declarations
C++
bool contains(const Key& key) const;
template<class K> bool contains(const K& x) const;   // C++20

Parameters

ParameterDescription
keyThe key to look for.

Return Value

Return ValueDescription
trueThe map contains a pair with this key.
falseThe map has no pair with this key.

C++ Examples

Example 1 — contains() Returns a Bool

Print the result of contains() for a present and a missing key.

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

int main() {
    std::map<std::string, int> age = {{"Mia", 21}, {"Raj", 25}};
    std::cout << std::boolalpha;

    std::cout << "Raj: " << age.contains("Raj") << '\n';
    std::cout << "Zoe: " << age.contains("Zoe") << '\n';
    return 0;
}

How It Works

  1. boolalpha makes the program print true and false instead of 1 and 0.
  2. contains("Raj") finds the key and returns true.
  3. contains("Zoe") does not find it and returns false. The map is not changed in either case.

Example 2 — contains() in an if Statement

Check a username before showing a message.

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

int main() {
    std::map<std::string, std::string> users = {{"ann", "admin"}, {"ben", "guest"}};

    for (const std::string name : {"ann", "cy"}) {
        if (users.contains(name)) {
            std::cout << name << " has role " << users.at(name) << '\n';
        } else {
            std::cout << name << " is unknown\n";
        }
    }
    return 0;
}

How It Works

  1. The loop tries two names: "ann" and "cy". Each is checked against the keys of users.
  2. "ann" is a key, so contains() is true and at(name) is safe to call. It prints the role admin.
  3. "cy" is not a key, so the else branch runs. Guarding at() with contains() avoids the std::out_of_range exception.

Example 3 — contains() Compared with count() and find()

Three ways to ask the same question, with the same answers.

C++
#include <iostream>
#include <map>

int main() {
    std::map<int, char> m = {{1, 'a'}, {2, 'b'}, {3, 'c'}};
    std::cout << std::boolalpha;

    for (int key : {2, 7}) {
        bool a = m.contains(key);
        bool b = m.count(key) > 0;
        bool c = m.find(key) != m.end();
        std::cout << key << ": " << a << " " << b << " " << c << '\n';
    }
    return 0;
}

How It Works

  1. For each key, three tests are run: contains() (C++20), count() > 0, and find() != end().
  2. Key 2 is in the map, so all three say true.
  3. Key 7 is not in the map, so all three say false. Pick contains() when you can, because it is the shortest and clearest.

Frequently Asked Questions

It returns true if the map has a pair with the given key and false if it does not.
C++20. In older versions use count(key) != 0 or find(key) != end().
No. It only checks. That is different from operator[], which adds a missing key.
It works, but it searches twice. If you need the value, call find() once and use the iterator.
For a std::map they take the same logarithmic time. contains() is mostly about clearer code.

Next: count()

Keep going through the std::map functions. Tell whether a key exists: the answer is 0 or 1 for a map.

count() 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.

12 people found this page helpful