C++ std::set size() Function

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

Definition

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
}
CallWhat 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

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

Return Value

Return ValueDescription
size_typeThe 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;
}

How It Works

  1. The list has five numbers, but 4 and 2 are written twice. The set keeps only one copy of each.
  2. The values are stored in sorted order: 2, 4, 9. So size() is 3, not 5.
  3. 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.

C++
#include <iostream>
#include <set>

int main() {
    std::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 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 set now holds 10 and 30.

Example 3 — size() Counts Different Items

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

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

int main() {
    std::string word = "banana";
    std::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::set<char> letters(word.begin(), word.end()) copies every character of "banana" into the set.
  2. The word has six letters, but only a, b, and n are different. The 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 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.

Next: swap()

Keep going through the std::set functions. Swap all values with another 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.

14 people found this page helpful