Home C++ Sets Functions end() C++ std::set end() Function Definition In C++, std::set::end() returns an iterator to the position after the last value . It is a marker, not a real value, so you must never dereference it .
You use it to know when a loop is finished, and to test the result of find(), lower_bound(), and upper_bound(), which return end() when nothing matches. 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 end with empty brackets. Use it as the stop value in a loop or as a “not found” test.
if (s.find("Zoe") == s.end()) {
// not found
}
auto last = std::prev(s.end()); // the last value Call What it does s.end()Returns an iterator one past the last value. On a const set it returns a const_iterator.
To get the last value, write std::prev(s.end()) or use s.rbegin(). Do not write *s.end() and do not use s.end() - 1, because set iterators do not support subtraction.
Library declarations iterator end() noexcept;
const_iterator end() const noexcept; Parameters Parameter Description None end() takes no parameters. Call it with empty brackets.
Return Value Return Value Description iteratorAn iterator to the position after the last value. It is equal to begin() if the set is empty.
C++ Examples 1. Test a search result 2. Get the last value 3. An empty set Example 1 — end() Means Not Found Compare the result of find() with end().
#include <iostream>
#include <set>
#include <string>
int main() {
std::set<std::string> names = {"Mia", "Raj"};
for (const std::string name : {"Raj", "Zoe"}) {
auto it = names.find(name);
if (it == names.end()) {
std::cout << name << ": not found\n";
} else {
std::cout << name << ": " << *it << '\n';
}
}
return 0;
} How It Works find() returns an iterator. It cannot return nullptr, so end() is the agreed way to say “nothing found”.For "Raj", the iterator points at a real value, so it is not equal to end() and the value prints. For "Zoe", find() returns end(), so the first branch runs. The code never reads a value from end(). Example 2 — end() and std::prev() Step back one position from end() to reach the last value, which is the biggest.
#include <iostream>
#include <iterator>
#include <set>
int main() {
std::set<int> s = {5, 1, 9};
if (!s.empty()) {
auto last = std::prev(s.end());
std::cout << "last: " << *last << '\n';
}
return 0;
} How It Works s.end() is one position after the last value, so reading it directly would be an error.std::prev(s.end()) moves one position back, onto the last value. That is the biggest value, 9.The !s.empty() check matters. On an empty set end() equals begin(), and stepping back would be undefined. Example 3 — end() on an Empty Set On an empty set begin() and end() are the same, so a loop does not run.
#include <iostream>
#include <set>
int main() {
std::set<int> s;
std::cout << std::boolalpha;
std::cout << "begin == end: " << (s.begin() == s.end()) << '\n';
int loops = 0;
for (auto it = s.begin(); it != s.end(); ++it) {
++loops;
}
std::cout << "loop ran " << loops << " times\n";
s.insert(1);
std::cout << "begin == end now: " << (s.begin() == s.end()) << '\n';
return 0;
} begin == end: true
loop ran 0 times
begin == end now: false How It Works An empty set has no first value, so begin() and end() are the same position, and the comparison is true. The loop condition it != s.end() is false at once, so the body never runs and loops stays 0. After insert(1), there is a real first value. begin() now points at it, and end() is still one position after, so they differ. Frequently Asked Questions What does std::set::end() return? An iterator to the position after the last value. It does not point at a real value.
Can I dereference end()? No. That is undefined behavior. Only compare it with other iterators.
How do I get the last value of a set? Use std::prev(s.end()) or s.rbegin(), after checking that the set is not empty.
Why does find() return end()? A search needs a way to say "not found". Because end() is never a real value, it is a safe signal.
What is end() on an empty set? It equals begin(), so any loop from begin() to end() does nothing.
Next: equal_range() Keep going through the std::set functions. Get the range of values equal to the given value.
equal_range() 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.
9 people found this page helpful
Helpful Share Copy link Suggestion