Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • C++

Idiom #244 Print a map

Print the contents of the map m to the standard output: keys and values.

#include <map>
#include <iostream>
for (auto entry : m) {
	std::cout << entry.first << ": " << entry.second << "\n";
}

Range-based for loop since C++11.
using System;
Console.WriteLine(string.Join(Environment.NewLine, m));

Takes advantage of IDictionary<T> being derived from IEnumerable<KeyValuePair<TKey, TValue>>.

Separator can be any string.

New implementation...