Home C++ Unordered Sets Functions end() C++ std::unordered_set end() Function Definition In C++, std::unordered_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() or equal_range(), which return end() when nothing matches. Include <unordered_set> to use it.
This is a member of std::unordered_set<T>, a hash table of unique values . Find, insert, and erase are usually average constant time. Unlike std::set, values are not sorted and iteration order is unspecified. Elements are const, so you cannot change a stored value through an iterator. There is no operator[] and no at(). Prefer find() or contains() to look up a value, and call reserve() before many inserts.
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
}
for (auto p = s.begin(); p != s.end(); ++p) {
// ...
} Call What it does s.end()Returns an iterator one past the last value. On a const unordered_set it returns a const_iterator.
Do not write *s.end() and do not use s.end() - 1. Unordered_set iterators are forward iterators: there is no reverse walk and no random access. Prefer find() over guessing which value is “last”.
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 unordered_set is empty.
C++ Examples 1. Test a search result 2. An empty unordered_set 3. Stop a membership walk Example 1 — end() Means Not Found Compare the result of find() with end().
#include <iostream>
#include <string>
#include <unordered_set>
int main() {
std::unordered_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() on an Empty Set On an empty unordered_set begin() and end() are the same, so a loop does not run.
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_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 unordered_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. Example 3 — end() Stops a Membership Check Walk until end() while looking for a target without using find().
#include <iostream>
#include <string>
#include <unordered_set>
int main() {
std::unordered_set<std::string> tags = {"cpp", "hash", "set"};
const std::string want = "hash";
bool found = false;
for (auto it = tags.begin(); it != tags.end(); ++it) {
if (*it == want) {
found = true;
break;
}
}
std::cout << std::boolalpha;
std::cout << "found hash by walk: " << found << '\n';
std::cout << "find agrees: " << (tags.find(want) != tags.end()) << '\n';
return 0;
} found hash by walk: true
find agrees: true How It Works The loop starts at begin() and stops at end() or when the target matches. "hash" is stored, so found becomes true and break ends the walk early.find() reports the same answer in average constant time. Prefer find() in real code; the loop only shows what end() means.Frequently Asked Questions What does std::unordered_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.
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.
Does unordered_set have rbegin()? No. There are no reverse iterators. Values are not sorted, so there is no "last" value in sorted order.
What is end() on an empty unordered_set? It equals begin(), so any loop from begin() to end() does nothing.
Next: equal_range() Keep going through the std::unordered_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.
10 people found this page helpful
Helpful Share Copy link Suggestion