Home C++ Maps Functions contains() C++ std::map contains() Function 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.
if (m.contains("Raj")) {
// the key exists
}
bool has = m.contains("Zoe"); Call What 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 bool contains(const Key& key) const;
template<class K> bool contains(const K& x) const; // C++20 Parameters Parameter Description keyThe key to look for.
Return Value Return Value Description trueThe map contains a pair with this key. falseThe map has no pair with this key.
C++ Examples 1. Check for a key 2. Use it in a condition 3. Same answer as count and find Example 1 — contains() Returns a Bool Print the result of contains() for a present and a missing key.
#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 boolalpha makes the program print true and false instead of 1 and 0.contains("Raj") finds the key and returns true.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.
#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;
} ann has role admin
cy is unknown How It Works The loop tries two names: "ann" and "cy". Each is checked against the keys of users. "ann" is a key, so contains() is true and at(name) is safe to call. It prints the role admin."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.
#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;
} 2: true true true
7: false false false How It Works For each key, three tests are run: contains() (C++20), count() > 0, and find() != end(). Key 2 is in the map, so all three say true. 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 What does std::map::contains() do? It returns true if the map has a pair with the given key and false if it does not.
Which C++ version has contains()? C++20. In older versions use count(key) != 0 or find(key) != end().
Does contains() add the key if it is missing? No. It only checks. That is different from operator[], which adds a missing key.
Should I call contains() and then at()? It works, but it searches twice. If you need the value, call find() once and use the iterator.
Is contains() faster than count()? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion