In C++, std::map::count() returns the number of pairs that have a given key. A std::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 <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 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 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::multimap can return larger numbers, but a std::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 a map only the first time you see it.
C++
#include <iostream>
#include <map>
#include <string>
#include <vector>
int main() {
std::vector<std::string> input = {"red", "blue", "red", "green", "blue"};
std::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);
}
}
for (const auto& kv : firstSeen) {
std::cout << kv.first << " first at index " << kv.second << '\n';
}
return 0;
}
Output
blue first at index 1
green first at index 3
red first at index 0
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.
The map prints in alphabetical order: blue, green, red, with indexes 1, 3, 0.
Example 3 — count() Added Up
Check how many of several wanted keys are present by adding the results.
C++
#include <iostream>
#include <map>
#include <string>
#include <vector>
int main() {
std::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::map.
Frequently Asked Questions
The number of pairs with the given key. For a std::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 a 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 multimap.