Home C++ Unordered Maps Functions begin() C++ std::unordered_map begin() Function Definition In C++, std::unordered_map::begin() returns an iterator to the first pair of a std::unordered_map in its current iteration order.
Through the iterator, it->first is the key and it->second is the value. Together with end() it lets you walk every pair. The walk order is unspecified (not sorted). Include <unordered_map> to use it.
This is a member of std::unordered_map<Key, T>, a hash table of key and value pairs . Each key is unique. Find, insert, and erase are usually average constant time. Unlike std::map, keys are not sorted and iteration order is unspecified. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.
Syntax Call begin with empty brackets. Compare with end() to know when to stop.
auto it = m.begin(); // first pair in iteration order
for (auto p = m.begin(); p != m.end(); ++p) {
// p->first, p->second
} Call What it does m.begin()Returns an iterator to the first pair. On a const map it returns a const_iterator.
If the unordered_map is empty, begin() equals end(). A range-based for (auto& p : m) 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 pair. For an empty map it equals end(), and it must not be dereferenced.
Important
The key part it->first is const . You can change the value with it->second, but you cannot change a key, because changing a key in place would break the hash table. On an empty map, never dereference begin().
C++ Examples 1. Read the first iterator safely 2. Walk every pair 3. Change a value through an iterator Example 1 — begin() Versus end() Check that begin is not end, then print size.
#include <iostream>
#include <string>
#include <unordered_map>
int main() {
std::unordered_map<int, std::string> m;
m[30] = "thirty";
m[10] = "ten";
m[20] = "twenty";
std::cout << std::boolalpha;
std::cout << "empty: " << (m.begin() == m.end()) << '\n';
std::cout << "size: " << m.size() << '\n';
std::cout << "has begin: " << (m.begin() != m.end()) << '\n';
return 0;
} empty: false
size: 3
has begin: true How It Works Three keys are inserted. Iteration order is unspecified, so this demo does not print begin()->first. begin() == end() is false because the unordered_map is not empty.size() is 3, and begin() != end() is true, so it is safe to dereference begin() if you need to.Example 2 — begin() in a Loop Count pairs with a begin/end loop without printing keys in a fixed order.
#include <iostream>
#include <string>
#include <unordered_map>
int main() {
std::unordered_map<std::string, int> stock = {{"pear", 4}, {"apple", 9}, {"kiwi", 2}};
int total = 0;
int visits = 0;
for (auto it = stock.begin(); it != stock.end(); ++it) {
total += it->second;
++visits;
}
std::cout << "visits: " << visits << '\n';
std::cout << "total: " << total << '\n';
std::cout << "apple: " << stock.at("apple") << '\n';
return 0;
} visits: 3
total: 15
apple: 9 How It Works stock.begin() starts the walk. The first key is unspecified.Each step adds the value and counts a visit. The loop stops at end(). Three visits and total 15 prove every pair was seen. at("apple") reads one key directly. Example 3 — Iterators Can Change Values Find a key, change its value through the iterator, then read both keys.
#include <iostream>
#include <string>
#include <unordered_map>
int main() {
std::unordered_map<std::string, int> score = {{"Ben", 70}, {"Ann", 50}};
auto it = score.find("Ann");
if (it != score.end()) {
it->second += 5; // allowed: changing the value
// it->first = "Zed"; // error: the key is const
}
std::cout << "Ann: " << score.at("Ann") << '\n';
std::cout << "Ben: " << score.at("Ben") << '\n';
std::cout << "begin != end: " << std::boolalpha << (score.begin() != score.end()) << '\n';
return 0;
} Ann: 55
Ben: 70
begin != end: true How It Works find("Ann") returns an iterator to that pair. Using find keeps the demo independent of begin order.it->second += 5 raises Ann from 50 to 55. The key stays const.begin() != end() is true while pairs remain. Ben is unchanged at 70.Frequently Asked Questions What does std::unordered_map::begin() return? An iterator to the first pair in the current iteration order, which is unspecified.
What is begin() on an empty map? It equals end(). You can compare it, but you must not dereference it.
Can I change the key through the iterator? No. The key of a pair in an unordered_map is const. You can change the value through it->second.
In what order does a loop from begin() visit the pairs? In an unspecified bucket order. Do not expect sorted keys like std::map.
What is the difference between begin() and cbegin()? begin() on a non-const map lets you change values. cbegin() always gives a const iterator, so you can only read.
Next: bucket() Keep going through the std::unordered_map functions. Which bucket a key 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.
8 people found this page helpful
Helpful Share Copy link Suggestion