Home C++ Maps Functions begin() C++ std::map begin() Function 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.
auto it = m.begin(); // pair with the smallest key
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 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 that could break the sorted order. On an empty map, never dereference begin().
C++ Examples 1. The smallest key 2. Walk the map 3. Change a value Example 1 — begin() Points at the Smallest Key Insert keys in a mixed order and read the first pair.
#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 The keys are added in the order 30, 10, 20. The map puts each one in its sorted place as it is added. m.begin() returns an iterator to the first pair in that sorted order, which is the one with key 10.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().
#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 auto it = stock.begin() starts at the pair with the alphabetically first key, apple.Each ++it moves to the next bigger key. The loop continues until it equals end(), the position after the last pair. 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.
#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 Ann sorts before Ben, so begin() points at Ann even though Ben was written first.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.The loop shows that only Ann's value changed, and that the map is still sorted by name. Frequently Asked Questions What does std::map::begin() return? An iterator to the first pair in the map, which is the pair with the smallest key.
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 a map is const. You can change the value through it->second.
In what order does a loop from begin() visit the pairs? In ascending key order (or the order of your own comparator), no matter how the pairs were inserted.
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: cbegin() Keep going through the std::map functions. Get a read-only iterator to the first pair (C++11).
cbegin() 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