C++ std::map size() Function

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

Definition

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

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

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 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 a 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 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 <map>
#include <string>

int main() {
    std::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 a 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 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 <map>
#include <string>

int main() {
    std::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 map is the number of different words.

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

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

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

    std::cout << "distinct words: " << freq.size() << '\n';
    for (const auto& kv : freq) {
        std::cout << kv.first << ": " << kv.second << '\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. The loop prints the pairs in sorted key order: be, not, or, to.

Frequently Asked Questions

The number of key and value pairs currently stored in the map. It is 0 for an empty map.
No. A map keeps only one pair for each key, so inserting an existing key does not change the size.
It returns std::map::size_type, an unsigned integer type. Compare it with unsigned values, or cast it, to avoid sign warnings.
No. A map allocates one node for every pair, so there is no spare capacity to manage. Only size() and max_size() exist.
No. The map stores its element count, so size() takes constant time even for millions of pairs.

Next: swap()

Keep going through the std::map functions. Exchange the whole content of two 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.

7 people found this page helpful