Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

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.

#include <iostream>
#include <map>
std::map< K, V > _mymap;
for (const auto& pair : _mymap) {
    std::cout << pair.first << ": " << pair.second << "\n";
}

std::map is a sorted collection that uses std::less by default.

New implementation...