C++ std::unordered_map begin() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::unordered_map

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.

C++
auto it = m.begin();           // first pair in iteration order
for (auto p = m.begin(); p != m.end(); ++p) {
    // p->first, p->second
}
CallWhat 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
C++
iterator begin() noexcept;
const_iterator begin() const noexcept;

Parameters

ParameterDescription
Nonebegin() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
iteratorAn iterator to the first pair. For an empty map it equals end(), and it must not be dereferenced.

C++ Examples

Example 1 — begin() Versus end()

Check that begin is not end, then print size.

C++
#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;
}

How It Works

  1. Three keys are inserted. Iteration order is unspecified, so this demo does not print begin()->first.
  2. begin() == end() is false because the unordered_map is not empty.
  3. 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.

C++
#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;
}

How It Works

  1. stock.begin() starts the walk. The first key is unspecified.
  2. Each step adds the value and counts a visit. The loop stops at end().
  3. 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.

C++
#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;
}

How It Works

  1. find("Ann") returns an iterator to that pair. Using find keeps the demo independent of begin order.
  2. it->second += 5 raises Ann from 50 to 55. The key stays const.
  3. begin() != end() is true while pairs remain. Ben is unchanged at 70.

Frequently Asked Questions

An iterator to the first pair in the current iteration order, which is unspecified.
It equals end(). You can compare it, but you must not dereference it.
No. The key of a pair in an unordered_map is const. You can change the value through it->second.
In an unspecified bucket order. Do not expect sorted keys like std::map.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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