Logo

Programming-Idioms

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

Idiom #244 Print a map

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

import "fmt"
fmt.Printf("%q", m)

The verb %q prints strings with nice double-quotes.
It's not the best for all types of keys and values, though.
import "fmt"
fmt.Println(m)

This works fine for simple types of keys and values.
It won't dereference pointers.
#include <map>
#include <iostream>
for (auto entry : m) {
	std::cout << entry.first << ": " << entry.second << "\n";
}

Range-based for loop since C++11.

New implementation...