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
}
Call
What 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
Parameter
Description
None
size() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
size_type
The 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;
}
Output
size: 3
2
4
9
How It Works
The list has five numbers, but 4 and 2 are written twice. The unordered_set keeps only one copy of each.
The values are stored in an unspecified order: 2, 4, 9. So size() is 3, not 5.
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.
std::unordered_set<char> letters(word.begin(), word.end()) copies every character of "banana" into the unordered_set.
The word has six letters, but only a, b, and n are different. The unordered_set drops each repeat, so it holds three letters.
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.