Logo

Programming-Idioms

History of Idiom 13 > diff from v56 to v57

Edit summary for version 57 by xryax:
[Cpp] structured bindings allow you to unpack the std::pair.You shouldn't use std::endl unless you really mean to flush stdout

Version 56

2019-05-31, 20:57:18

Version 57

2019-08-02, 18:47:28

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& kv: mymap) {
	std::cout << "Key: " << kv.first << " Value: " kv.second << std::endl;
}
Code
for (const auto& [key, value]: mymap) {
	std::cout << "Key: " << key << " Value: " value << '\n';
}
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.