C Program To Print World Map

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:

Easily create and personalize a custom map with MapQuest My Maps. Print and share your next trip or plan out your day. This is a first program in C language; this program will print the text (‘Hello World’) on the output screen.Here, we are going to learn how to write, execute and run a first program in C programming language? In this program we will print Hello World, this is the first program in C programming language.We will print Hello World using user define function’s too. In this article I am sharing the program to print India map. If you like it, please do share it! Also Read: Valentine’s Day Special: C Program to Print Heart Shape with Happy Valentine’s Day Message inside it #include main int a,b,c; int count = 1; for (b=c=10;a=”- C Program to Print India Map Read More ». In this program we will print Hello World, this is the first program in C programming language. We will print Hello World using user define function’s too. This is the very first program in c programming language, you have to include only single header file that is stdio.h, this header file contains the declaration printf function.

2
4
6
8
10
12
14
16
18
20
22
#include <unordered_map>
template<typenameK,typenameV>
{
std::cout<<'{'<<pair.first<<': '<<pair.second<<'}n';
}
intmain()
std::unordered_map<int,char>m={
{2,'B'},
};
}

Output:

{3: C}
{1: A}
{2: B}

2. Using std::for_each function

Program

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.

2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
#include <unordered_map>
{
voidoperator()(conststd::pair<K,V>&p){
}
voidprint(conststd::pair<K,V>&p){
}
template<typenameK,typenameV>
{
std::for_each(m.begin(),
[](conststd::pair<int,char>&p){
});
// or pass an object of a class overloading the ()operator
// std::for_each(m.begin(), m.end(), print<K, V>);
{
{1,'A'},
{3,'C'}
}

Output:

{3: C}
{1: A}
{2: B}

3. Using Iterator

C Program To Print World Map

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.

C Program To Print World Map Online

2
4
6
8
10
12
14
16
18
20
22
#include <unordered_map>
template<typenameK,typenameV>
{
std::cout<<'{'<<(*it).first<<': '<<(*it).second<<'}n';
}
intmain()
std::unordered_map<int,char>m={
{2,'B'},
};
}

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:

2
4
6
8
10
12
14
16
18
20
22
24
#include <unordered_map>
template<typenameK,typenameV>
conststd::unordered_map<K,V>&m){
os<<'{'<<p.first<<': '<<p.second<<'}n';
returnos;
{
{1,'A'},
{3,'C'}
}

C Program To Print World Map Worksheet

Output:

{3: C}
{1: A}
{2: B}

C Program To Print World Map Without

That’s all about printing out all keys and values from a map in C++.

Comments are closed.