Logo

Programming-Idioms

History of Idiom 13 > diff from v58 to v59

Edit summary for version 59 by programming-idioms.org:
[Cpp] Split 2 ways into 2 snippets (implementations)

Version 58

2019-08-05, 16:37:33

Version 59

2019-08-05, 16:38:52

Idiom #13 Iterate over map keys and values

Print each key k with its value x from an associative array mymap

Illustration

Idiom #13 Iterate over map keys and values

Print each key k with its value x from an associative array mymap

Illustration
Extra Keywords
table dictionary hash traverse traversal
Extra Keywords
table dictionary hash traverse traversal
Imports
#include <iostream>
Imports
#include <iostream>
Code
for (const auto& [key, value]: mymap) {
	std::cout << "Key: " << key << " Value: " value << '\n';
}
Code
for (const auto& kv: mymap) {
	std::cout << "Key: " << kv.first << " Value: " kv.second << std::endl;
}
Comments bubble
the [key, value] syntax is a structured binding declaration: https://en.cppreference.com/w/cpp/language/structured_binding

prefer '\n' to std::endl. std::endl flushes stdout, which you don't typically need or want.
Comments bubble
std::endl flushes stdout.