C++ std::unordered_set size() Function

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

Definition

In C++, std::unordered_set::size() returns the number of values currently stored in the unordered_set. Because an unordered_set keeps each value only once, the size is the count of different values.

The result type is size_type, an unsigned integer. The call takes constant time and does not change the unordered_set. Include <unordered_set> to use it.

This is a member of std::unordered_set<T>, a hash table of unique values. Find, insert, and erase are usually average constant time. Unlike std::set, values are not sorted and iteration order is unspecified. Elements are const, so you cannot change a stored value through an iterator. There is no operator[] and no at(). Prefer find() or contains() to look up a value, and call reserve() before many inserts.

Syntax

Call it with empty brackets and store the result in std::size_t.

C++
std::unordered_set<int> s = {4, 2, 4, 9};
std::size_t n = s.size();      // 3
if (s.size() > 2) {
    // more than two values
}
CallWhat it does
s.size()Returns the number of values in the unordered_set as a size_type.

Do not compare size() with a negative number. It is unsigned, so s.size() - 1 on an empty unordered_set wraps around to a huge value. An unordered_set has no capacity() and no resize(), so size() is the only count you need.

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 values in the unordered_set. It is 0 for an empty unordered_set.

C++ Examples

Example 1 — size() Counts Unique Values

Build an unordered_set from a list that has repeats and read its size.

C++
#include <iostream>
#include <unordered_set>

int main() {
    std::unordered_set<int> s = {4, 2, 4, 9, 2};

    std::cout << "size: " << s.size() << '\n';
    for (int v : s) {
        std::cout << v << '\n';
    }
    return 0;
}

How It Works

  1. The list has five numbers, but 4 and 2 are written twice. The unordered_set keeps only one copy of each.
  2. The values are stored in an unspecified order: 2, 4, 9. So size() is 3, not 5.
  3. The loop prints the three stored values, one per line, in the same hash-bucket order.

Example 2 — size() Before and After insert()

Insert a new value and a repeated value, and see which one changes the size.

C++
#include <iostream>
#include <unordered_set>

int main() {
    std::unordered_set<int> s = {10, 20};
    std::cout << "start: " << s.size() << '\n';

    s.insert(30);
    std::cout << "after insert 30: " << s.size() << '\n';

    s.insert(10);
    std::cout << "after insert 10: " << s.size() << '\n';

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

How It Works

  1. The unordered_set starts with 10 and 20, so the first size is 2.
  2. insert(30) adds a new value and the size becomes 3. insert(10) finds 10 already stored, so nothing is added and the size stays 3.
  3. erase(20) removes one value, so the size drops to 2. The unordered_set now holds 10 and 30.

Example 3 — size() Counts Different Items

Put every letter of a word into an unordered_set. The size tells how many different letters the word has.

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

int main() {
    std::string word = "banana";
    std::unordered_set<char> letters(word.begin(), word.end());

    std::cout << "word length: " << word.size() << '\n';
    std::cout << "distinct letters: " << letters.size() << '\n';
    return 0;
}

How It Works

  1. std::unordered_set<char> letters(word.begin(), word.end()) copies every character of "banana" into the unordered_set.
  2. The word has six letters, but only a, b, and n are different. The unordered_set drops each repeat, so it holds three letters.
  3. word.size() is the string length, 6. letters.size() is the number of different letters, 3. The same function name gives two different kinds of answers.

Frequently Asked Questions

The number of values in the unordered_set. Each value is counted once, because an unordered_set holds no duplicates.
Repeated values are ignored by an unordered_set. Only the first copy is stored, so the size counts unique values.
No. It runs in constant time, because the unordered_set keeps its count and does not walk through the values.
No. An unordered_set stores each value in its own node, so there is no capacity() or reserve(). Use size() to count values.
size() is how many values are in the unordered_set right now. max_size() is some number the unordered_set could ever hold on your system.

Next: swap()

Keep going through the std::unordered_set functions. Swap all values with another unordered_set of the same type.

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.

9 people found this page helpful