C++ std::set begin() Function

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

Definition

In C++, std::set::begin() returns an iterator to the first value of a std::set. Because the set keeps its values sorted, the first value is the smallest one.

Through the iterator, *it reads the value. Together with end() it lets you walk the whole set in sorted order. 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 begin with empty brackets. Compare with end() to know when to stop.

C++
auto it = s.begin();           // smallest value
for (auto p = s.begin(); p != s.end(); ++p) {
    // *p
}
CallWhat it does
s.begin()Returns an iterator to the first value. On a const set it returns a const_iterator.

If the set is empty, begin() equals end(). A range-based for (const auto& v : s) uses begin() and end() for you.

Library declarations
C++
iterator begin() noexcept;
const_iterator begin() const noexcept;

Parameters

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

Return Value

Return ValueDescription
iteratorAn iterator to the first value. For an empty set it equals end(), and it must not be dereferenced.

C++ Examples

Example 1 — begin() Points at the Smallest Value

Insert values in a mixed order and read the first one.

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

int main() {
    std::set<int> s;
    s.insert(30);
    s.insert(10);
    s.insert(20);

    auto it = s.begin();
    std::cout << "first: " << *it << '\n';
    return 0;
}

How It Works

  1. The values are added in the order 30, 10, 20. The set puts each one in its sorted place as it is added.
  2. s.begin() returns an iterator to the first value in that sorted order, which is 10.
  3. *it reads that value. There is no separate key field, because the value is its own key.

Example 2 — begin() in a Loop

Start at begin() and move with ++ until end().

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

int main() {
    std::set<std::string> fruit = {"pear", "apple", "kiwi"};

    for (auto it = fruit.begin(); it != fruit.end(); ++it) {
        std::cout << *it << '\n';
    }
    return 0;
}

How It Works

  1. auto it = fruit.begin() starts at the alphabetically first value, apple.
  2. Each ++it moves to the next bigger value. The loop continues until it equals end(), the position after the last value.
  3. The values come out in alphabetical order, apple, kiwi, pear, not in the order they were written.

Example 3 — begin() Needs an Empty Check

Read the smallest score only when the set is not empty.

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

int main() {
    std::set<int> scores;
    std::cout << std::boolalpha;
    std::cout << "empty begin==end: " << (scores.begin() == scores.end()) << '\n';

    scores.insert(40);
    scores.insert(15);

    if (!scores.empty()) {
        std::cout << "lowest: " << *scores.begin() << '\n';
    }
    return 0;
}

How It Works

  1. On an empty set begin() equals end(), so the first line prints true.
  2. After inserting 40 and 15, the sorted order is 15, 40.
  3. The !empty() check makes *scores.begin() safe, and the lowest score 15 prints.

Frequently Asked Questions

An iterator to the first value in the set, which is the smallest value.
It equals end(). You can compare it, but you must not dereference it.
No. Elements in a set are const. Erase the old value and insert the new one.
In ascending order (or the order of your own comparator), no matter how the values were inserted.
On a non-const set begin() returns iterator and cbegin() returns const_iterator. For a set both still cannot change the element.

Next: cbegin()

Keep going through the std::set functions. Get a read-only iterator to the first value (C++11).

cbegin() 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