C++ std::map begin() Function

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

Definition

In C++, std::map::begin() returns an iterator to the first pair of a std::map. Because the map keeps its keys sorted, the first pair is the one with the smallest key.

Through the iterator, it->first is the key and it->second is the value. Together with end() it lets you walk the whole map in key order. Include <map> to use it.

This is a member of std::map<Key, T>, a sorted container of key and value pairs. Each key is unique and the pairs are always kept in key order, so a map can find, insert, and erase a key in logarithmic time. 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();           // pair with the smallest key
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 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() Points at the Smallest Key

Insert keys in a mixed order and read the first pair.

C++
#include <iostream>
#include <map>
#include <string>

int main() {
    std::map<int, std::string> m;
    m[30] = "thirty";
    m[10] = "ten";
    m[20] = "twenty";

    auto it = m.begin();
    std::cout << "first: " << it->first << " " << it->second << '\n';
    return 0;
}

How It Works

  1. The keys are added in the order 30, 10, 20. The map puts each one in its sorted place as it is added.
  2. m.begin() returns an iterator to the first pair in that sorted order, which is the one with key 10.
  3. it->first reads the key 10 and it->second reads the value "ten".

Example 2 — begin() in a Loop

Start at begin() and move with ++ until end().

C++
#include <iostream>
#include <map>
#include <string>

int main() {
    std::map<std::string, int> stock = {{"pear", 4}, {"apple", 9}, {"kiwi", 2}};

    for (auto it = stock.begin(); it != stock.end(); ++it) {
        std::cout << it->first << " x" << it->second << '\n';
    }
    return 0;
}

How It Works

  1. auto it = stock.begin() starts at the pair with the alphabetically first key, apple.
  2. Each ++it moves to the next bigger key. The loop continues until it equals end(), the position after the last pair.
  3. The pairs come out in alphabetical order, apple, kiwi, pear, not in the order they were written.

Example 3 — begin() Lets You Change a Value

Use the first iterator to add to the value. The key stays const.

C++
#include <iostream>
#include <map>
#include <string>

int main() {
    std::map<std::string, int> score = {{"Ben", 70}, {"Ann", 50}};

    auto it = score.begin();         // Ann comes first alphabetically
    it->second += 5;                 // allowed: changing the value
    // it->first = "Zed";            // error: the key is const

    for (const auto& kv : score) {
        std::cout << kv.first << ": " << kv.second << '\n';
    }
    return 0;
}

How It Works

  1. Ann sorts before Ben, so begin() points at Ann even though Ben was written first.
  2. it->second += 5 raises Ann's value from 50 to 55. The commented line would not compile, because the key of a pair in a map is const.
  3. The loop shows that only Ann's value changed, and that the map is still sorted by name.

Frequently Asked Questions

An iterator to the first pair in the map, which is the pair with the smallest key.
It equals end(). You can compare it, but you must not dereference it.
No. The key of a pair in a map is const. You can change the value through it->second.
In ascending key order (or the order of your own comparator), no matter how the pairs were inserted.
begin() on a non-const map lets you change values. cbegin() always gives a const iterator, so you can only read.

Next: cbegin()

Keep going through the std::map functions. Get a read-only iterator to the first pair (C++11).

cbegin() 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