In C++, std::unordered_map::count() returns the number of pairs that have a given key. A std::unordered_map never holds the same key twice, so the answer is always 0 or 1.
That makes it a simple “does this key exist?” test. It does not read the value and it does not add the key. 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 you are looking for.
C++
if (m.count("Raj") == 1) {
// key exists
}
bool has = m.count("Zoe") > 0;
Call
What it does
m.count(key)
Returns 1 if the key is in the unordered_map, and 0 if it is not.
count() only says whether the key exists. To also get the value, use find(). Since C++20, contains() expresses the same test more clearly. A std::unordered_multimap can return larger numbers, but a std::unordered_map cannot.
count("Raj") searches the keys and finds one match, so the result is 1.
count("Zoe") finds none, so the result is 0. Because keys are unique, it can never be 2 or more.
The size is still 2. Asking with count() does not add "Zoe", unlike age["Zoe"] would.
Example 2 — count() in a Condition
Add a word to an unordered_map only the first time you see it.
C++
#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
int main() {
std::vector<std::string> input = {"red", "blue", "red", "green", "blue"};
std::unordered_map<std::string, int> firstSeen;
for (std::size_t i = 0; i < input.size(); ++i) {
if (firstSeen.count(input[i]) == 0) {
firstSeen[input[i]] = static_cast<int>(i);
}
}
std::cout << "red first at index " << firstSeen.at("red") << '\n';
std::cout << "blue first at index " << firstSeen.at("blue") << '\n';
std::cout << "green first at index " << firstSeen.at("green") << '\n';
return 0;
}
Output
red first at index 0
blue first at index 1
green first at index 3
How It Works
The loop reads each word. count(word) == 0 is true only when the word has never been stored.
For a new word, firstSeen[word] = i saves its index. The second red and the second blue find a count of 1, so they are skipped and the first index stays.
Indexes are printed with at() in a fixed order so the demo does not depend on hash buckets.
Example 3 — count() Added Up
Check how many of several wanted keys are present by adding the results.
C++
#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
int main() {
std::unordered_map<std::string, double> menu = {{"tea", 2.5}, {"coffee", 3.0}, {"juice", 4.0}};
std::vector<std::string> wanted = {"tea", "cake", "juice", "soup"};
int available = 0;
for (const std::string& item : wanted) {
available += static_cast<int>(menu.count(item));
}
std::cout << available << " of " << wanted.size() << " items are on the menu\n";
return 0;
}
Output
2 of 4 items are on the menu
How It Works
menu has three drinks. wanted lists four items, two of which are not on the menu.
Each menu.count(item) is 1 for tea and juice, and 0 for cake and soup. Adding them gives 2.
This only works as a sum because the answer is always 0 or 1 for a std::unordered_map.
Frequently Asked Questions
The number of pairs with the given key. For a std::unordered_map that is only 0 or 1, because keys are unique.
No. It only looks. Using m[key] to test for a key is a mistake, because that inserts the key.
count() only says whether the key exists. find() returns an iterator, so you can also read or change the value without a second search.
They give the same answer for an unordered_map, but contains() (C++20) returns a bool and reads more clearly. count() works in every C++ version.
It is a short exists check, and the same function name works on containers where larger counts are possible, such as unordered_multimap.