Home C++ Unordered Sets Functions begin() C++ std::unordered_set begin() Function Definition In C++, std::unordered_set::begin() returns an iterator to one value in a std::unordered_set. The first position is unspecified , because values live in hash buckets and are not sorted.
Through the iterator, *it reads the value. Together with end() it lets you walk every value. Do not rely on the visit order. 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 begin with empty brackets. Compare with end() to know when to stop.
auto it = s.begin();
for (auto p = s.begin(); p != s.end(); ++p) {
// *p (order is unspecified)
} Call What it does s.begin()Returns an iterator to one value. On a const unordered_set it returns a const_iterator.
If the unordered_set is empty, begin() equals end(). A range-based for (const auto& v : s) uses begin() and end() for you. Unlike std::set, there is no guaranteed sorted walk.
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 one value. For an empty unordered_set it equals end(), and it must not be dereferenced.
Important
Every element in an unordered_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 unordered_set, never dereference begin(). Do not expect sorted order.
C++ Examples 1. Read one value safely 2. Count with a begin/end loop 3. Prefer find for a known value Example 1 — begin() Needs a Non-Empty Check Insert values and read through begin() only when the unordered_set is not empty.
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> s;
std::cout << std::boolalpha;
std::cout << "empty begin==end: " << (s.begin() == s.end()) << '\n';
s.insert(30);
s.insert(10);
s.insert(20);
if (!s.empty()) {
std::cout << "has value via begin: " << (*s.begin() == 10 || *s.begin() == 20 || *s.begin() == 30) << '\n';
}
std::cout << "size: " << s.size() << '\n';
return 0;
} empty begin==end: true
has value via begin: true
size: 3 How It Works On an empty unordered_set begin() equals end(), so the first line prints true. After three inserts the table holds 10, 20, and 30. Which one begin() points at is unspecified. The check proves that *begin() is one of those three values, and size() is 3. The demo never prints a single ordered list. Example 2 — begin() in a Loop Walk every value and count how many are greater than 5.
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> nums = {2, 9, 4, 11};
int big = 0;
for (auto it = nums.begin(); it != nums.end(); ++it) {
if (*it > 5) {
++big;
}
}
std::cout << "values > 5: " << big << '\n';
std::cout << "size: " << nums.size() << '\n';
return 0;
} How It Works begin() starts the walk at an unspecified value. The loop continues until end().Each step reads *it. Values 9 and 11 raise the counter; 2 and 4 do not. The printed answers are counts, so they do not depend on hash-bucket order. Example 3 — begin() Versus find() Use find() when you care about a specific value; use begin() only to start a full walk.
#include <iostream>
#include <string>
#include <unordered_set>
int main() {
std::unordered_set<std::string> fruit = {"pear", "apple", "kiwi"};
auto apple = fruit.find("apple");
auto missing = fruit.find("mango");
std::cout << std::boolalpha;
std::cout << "apple found: " << (apple != fruit.end()) << '\n';
std::cout << "mango found: " << (missing != fruit.end()) << '\n';
std::cout << "begin != end: " << (fruit.begin() != fruit.end()) << '\n';
std::cout << "size: " << fruit.size() << '\n';
return 0;
} apple found: true
mango found: false
begin != end: true
size: 3 How It Works find("apple") returns an iterator to "apple" because that value is stored.find("mango") returns end(). That is the safe way to ask for one specific value.begin() != end() only says the unordered_set is not empty. size() is 3. Prefer find() when you care about a named value.Frequently Asked Questions What does std::unordered_set::begin() return? An iterator to one value in the unordered_set. Which value is first is unspecified because of hashing.
What is begin() on an empty unordered_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 an unordered_set are const. Erase the old value and insert the new one.
In what order does a loop from begin() visit the values? In an unspecified hash-bucket order. Unlike std::set, the walk is not sorted.
What is the difference between begin() and cbegin()? On a non-const unordered_set begin() returns iterator and cbegin() returns const_iterator. Elements stay read-only either way.
Next: bucket() Keep going through the std::unordered_set functions. Which bucket a value would use (or already uses).
bucket() 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