C++ std::unordered_map size() Function

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

Definition

In C++, std::unordered_map::size() returns how many pairs the unordered_map holds right now. Each pair is one key together with its value.

Because every key in an unordered_map is unique, size() is also the number of distinct keys. That makes it handy for counting different words or ids. 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

Call size with empty brackets. It returns an unsigned number.

C++
std::size_t n = m.size();     // number of pairs
if (m.size() >= 3) {
    // the unordered_map has at least three pairs
}
CallWhat it does
m.size()Returns the number of elements as size_type, an unsigned integer type (usually std::size_t).

size() is not the same as count(key). size() counts all pairs. count(key) tells you whether one key exists, so it is only 0 or 1 for an unordered_map.

Library declarations
C++
size_type size() const noexcept;

Parameters

ParameterDescription
Nonesize() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
size_typeThe number of key and value pairs in the unordered_map. It is 0 for an empty map.

C++ Examples

Example 1 — size() After Each Change

Print the size after an insert() and after an erase().

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

int main() {
    std::unordered_map<int, std::string> m = {{1, "one"}, {2, "two"}};
    std::cout << "start: " << m.size() << '\n';

    m.insert({3, "three"});
    std::cout << "after insert: " << m.size() << '\n';

    m.erase(1);
    std::cout << "after erase: " << m.size() << '\n';
    return 0;
}

How It Works

  1. The braces create an unordered_map with two pairs, so the first line prints 2.
  2. insert({3, "three"}) adds a new key, so the number of pairs goes up to 3.
  3. erase(1) removes the pair whose key is 1. The unordered_map is back to two pairs.

Example 2 — size() and Duplicate Keys

Try to add the same key twice and see that the size does not grow.

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

int main() {
    std::unordered_map<int, std::string> m;

    m.insert({1, "first"});
    m.insert({1, "second"});          // ignored: key 1 exists
    std::cout << "size: " << m.size() << ", value: " << m[1] << '\n';

    m[1] = "updated";                 // updates, does not add
    std::cout << "size: " << m.size() << ", value: " << m[1] << '\n';
    return 0;
}

How It Works

  1. The first insert() adds key 1. The second insert() sees that the key is already there, so it does nothing and the old value stays.
  2. The first line proves it: one pair, and the value is still "first".
  3. m[1] = "updated" finds the existing key and overwrites its value. Updating a value never changes the size, so it is still 1.

Example 3 — size() Counts Distinct Keys

Count how often each word appears. The size of the unordered_map is the number of different words.

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

int main() {
    std::vector<std::string> words = {"to", "be", "or", "not", "to", "be"};
    std::unordered_map<std::string, int> freq;

    for (const std::string& w : words) {
        ++freq[w];
    }

    std::cout << "distinct words: " << freq.size() << '\n';
    std::cout << "be: " << freq.at("be") << '\n';
    std::cout << "to: " << freq.at("to") << '\n';
    return 0;
}

How It Works

  1. ++freq[w] inserts the word with 0 if it is new, and then adds one. Repeated words only raise the count.
  2. Six words were read but only four are different, so freq.size() is 4.
  3. Counts for be and to are read with at(), so the printed order does not depend on hash buckets.

Frequently Asked Questions

The number of key and value pairs currently stored in the unordered_map. It is 0 for an empty map.
No. An unordered_map keeps only one pair for each key, so inserting an existing key does not change the size.
It returns std::unordered_map::size_type, an unsigned integer type. Compare it with unsigned values, or cast it, to avoid sign warnings.
It has no capacity() like a vector, but it does have reserve() and rehash() to control buckets. size() and max_size() still report element counts.
No. The unordered_map stores its element count, so size() takes constant time even for millions of pairs.

Next: swap()

Keep going through the std::unordered_map functions. Exchange the whole content of two unordered_maps quickly.

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

11 people found this page helpful