Home C++ Sets Functions begin() C++ std::set begin() Function 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.
auto it = s.begin(); // smallest value
for (auto p = s.begin(); p != s.end(); ++p) {
// *p
} Call What 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 iterator begin() noexcept;
const_iterator begin() const noexcept; Parameters Parameter Description None begin() takes no parameters. Call it with empty brackets.
Return Value Return Value Description iteratorAn iterator to the first value. For an empty set it equals end(), and it must not be dereferenced.
Important
Every element in a set is const . You can read *it, but you cannot change it. To change a value, erase it and insert a new one. On an empty set, never dereference begin().
C++ Examples 1. The smallest value 2. Walk the set 3. Check before reading Example 1 — begin() Points at the Smallest Value Insert values in a mixed order and read the first one.
#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 The values are added in the order 30, 10, 20. The set puts each one in its sorted place as it is added. s.begin() returns an iterator to the first value in that sorted order, which is 10.*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().
#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 auto it = fruit.begin() starts at the alphabetically first value, apple.Each ++it moves to the next bigger value. The loop continues until it equals end(), the position after the last value. 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.
#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;
} empty begin==end: true
lowest: 15 How It Works On an empty set begin() equals end(), so the first line prints true. After inserting 40 and 15, the sorted order is 15, 40. The !empty() check makes *scores.begin() safe, and the lowest score 15 prints. Frequently Asked Questions What does std::set::begin() return? An iterator to the first value in the set, which is the smallest value.
What is begin() on an empty set? It equals end(). You can compare it, but you must not dereference it.
Can I change a value through the iterator? No. Elements in a set are const. Erase the old value and insert the new one.
In what order does a loop from begin() visit the values? In ascending order (or the order of your own comparator), no matter how the values were inserted.
What is the difference between begin() and cbegin()? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion