C++ std::unordered_map count() Function

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

Definition

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;
CallWhat 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.

Library declarations
C++
size_type count(const Key& key) const;
template<class K> size_type count(const K& x) const;   // transparent hash/eq

Parameters

ParameterDescription
keyThe key to look for.

Return Value

Return ValueDescription
1The key is in the unordered_map.
0The key is not in the unordered_map.

C++ Examples

Example 1 — count() Returns 0 or 1

Ask for one key that exists and one that does not.

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

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

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

How It Works

  1. count("Raj") searches the keys and finds one match, so the result is 1.
  2. count("Zoe") finds none, so the result is 0. Because keys are unique, it can never be 2 or more.
  3. 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;
}

How It Works

  1. The loop reads each word. count(word) == 0 is true only when the word has never been stored.
  2. 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.
  3. 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;
}

How It Works

  1. menu has three drinks. wanted lists four items, two of which are not on the menu.
  2. Each menu.count(item) is 1 for tea and juice, and 0 for cake and soup. Adding them gives 2.
  3. 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.

Next: emplace()

Keep going through the std::unordered_map functions. Build a new pair in place from its constructor arguments (C++11).

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

7 people found this page helpful