In C++, std::set::size() returns the number of values currently stored in the set. Because a 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 set. Include <set> to use it.
This is a member of std::set<T>, a sorted container of unique values. A set never keeps a duplicate and always holds its values in sorted order, so it can find, insert, and erase a value in logarithmic time. A set has no operator[] and no at(), because the value is its own key. Every element is const, so you cannot change a value through an iterator.
Syntax
Call it with empty brackets and store the result in std::size_t.
C++
std::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 set as a size_type.
Do not compare size() with a negative number. It is unsigned, so s.size() - 1 on an empty set wraps around to a huge value. A 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 set. It is 0 for an empty set.
C++ Examples
Example 1 — size() Counts Unique Values
Build a set from a list that has repeats and read its size.
C++
#include <iostream>
#include <set>
int main() {
std::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 set keeps only one copy of each.
The values are stored in sorted order: 2, 4, 9. So size() is 3, not 5.
The loop prints the three stored values, one per line, in the same sorted order.
Example 2 — size() Before and After insert()
Insert a new value and a repeated value, and see which one changes the size.
std::set<char> letters(word.begin(), word.end()) copies every character of "banana" into the set.
The word has six letters, but only a, b, and n are different. The 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 set. Each value is counted once, because a set holds no duplicates.
Repeated values are ignored by a set. Only the first copy is stored, so the size counts unique values.
No. It runs in constant time, because the set keeps its count and does not walk through the values.
No. A 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 set right now. max_size() is the largest number the set could ever hold on your system.