Logo

Programming-Idioms

History of Idiom 112 > diff from v22 to v23

Edit summary for version 23 by zZzZzz:
New Cpp implementation by user [zZzZzz]

Version 22

2019-09-26, 14:13:32

Version 23

2019-09-26, 16:03:08

Idiom #112 Iterate over map entries, ordered by keys

Print each key k with its value x from an associative array mymap, in ascending order of k.

Idiom #112 Iterate over map entries, ordered by keys

Print each key k with its value x from an associative array mymap, in ascending order of k.

Extra Keywords
traverse traversal
Extra Keywords
traverse traversal
Imports
#include <iostream>
#include <map>
Code
std::map< K, V > _mymap;
for (const auto& pair : _mymap) {
    std::cout << pair.first << ": " << pair.second << "\n";
}
Comments bubble
std::map is a sorted collection that uses std::less by default.
Doc URL
https://en.cppreference.com/w/cpp/container/map