Print out all keys and values from a map in C++
This post will discuss how to print out all keys and values from a std::map
or std::unordered_map
in C++.
Maps are associative containers whose elements are key-value pairs. Using a map, we can associate a value with some key and later retrieve that value using the same key. The retrieval operation in a map is very fast. There are several ways in C++ to print out all pairs present on the map:
1. Using range-based for-loop
The recommended approach in C++11 is to use the new range-based for-loops for printing the map pairs, as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <unordered_map> template<typename K, typename V> void print_map(std::unordered_map<K, V> const &m) { for (auto const &pair: m) { std::cout << "{" << pair.first << ": " << pair.second << "}\n"; } } int main() { std::unordered_map<int, char> m = { {1, 'A'}, {2, 'B'}, {3, 'C'} }; print_map(m); return 0; } |
Output:
{3: C}
{1: A}
{2: B}
2. Using std::for_each
function
Another simple solution is to use std::for_each
. It takes an iterator to the beginning and end of the map, and applies a specified function on every pair within that range. The specified function may be a unary function, a lambda expression, or an object of a class overloading the ()
operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#include <iostream> #include <unordered_map> #include <algorithm> class myclass { template<typename K, typename V> void operator()(const std::pair<K, V> &p) { std::cout << "{" << p.first << ": " << p.second << "}\n"; } } ob; template<typename K, typename V> void print(const std::pair<K, V> &p) { std::cout << "{" << p.first << ": " << p.second << "}\n"; } template<typename K, typename V> void print_map(std::unordered_map<K, V> const &m) { // specify a lambda expression std::for_each(m.begin(), m.end(), [](const std::pair<int, char> &p) { std::cout << "{" << p.first << ": " << p.second << "}\n"; }); // or pass an object of a class overloading the ()operator // std::for_each(m.begin(), m.end(), ob); // or specify a function // std::for_each(m.begin(), m.end(), print<K, V>); } int main() { std::unordered_map<int, char> m = { {1, 'A'}, {2, 'B'}, {3, 'C'} }; print_map(m); return 0; } |
Output:
{3: C}
{1: A}
{2: B}
3. Using Iterator
We can also use iterators to iterate a map and print its pairs. It is recommended to use the const_iterator
from C++11, as we’re not modifying the map pairs inside the for-loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <unordered_map> template<typename K, typename V> void print_map(std::unordered_map<K, V> const &m) { for (auto it = m.cbegin(); it != m.cend(); ++it) { std::cout << "{" << (*it).first << ": " << (*it).second << "}\n"; } } int main() { std::unordered_map<int, char> m = { {1, 'A'}, {2, 'B'}, {3, 'C'} }; print_map(m); return 0; } |
Output:
{3: C}
{1: A}
{2: B}
4. Operator<< Overloading
To get std::cout
to accept the map object, we can overload the <<
operator to recognize an ostream object on the left and a map object on the right, as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> #include <unordered_map> template<typename K, typename V> std::ostream &operator<<(std::ostream &os, const std::unordered_map<K, V> &m) { for (const std::pair<K, V> &p: m) { os << "{" << p.first << ": " << p.second << "}\n"; } return os; } int main() { std::unordered_map<int, char> m = { {1, 'A'}, {2, 'B'}, {3, 'C'} }; std::cout << m; return 0; } |
Output:
{3: C}
{1: A}
{2: B}
That’s all about printing out all keys and values from a map in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)