The maps are described as mapped associative containers for elements where each element has a key and value assigned to it. Another form of map container seen in the C++ STL is the unordered map. It is the same as map containers just that they don’t store the data in sorted order.
We can traverse map and unordered_map using 4 different ways which are as follows:
- Using a ranged based for loop
- Using begin() and end()
- Using Iterators
- Using std::for_each and lambda function
1. Using a Range Based for Loop
We can use a range-based for loop to iterate over a map or an unordered_map in C++.
Example:
map
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 1, 1, 2, 1, 1, 3, 4, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
map< int , int > m;
for ( int i = 0; i < n; i++)
m[arr[i]]++;
cout << "Element Frequency" << endl;
for ( auto i : m)
cout << i.first << " \t\t\t " << i.second << endl;
return 0;
}
|
unordered_map
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 1, 1, 2, 1, 1, 3, 4, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
unordered_map< int , int > m;
for ( int i = 0; i < n; i++)
m[arr[i]]++;
cout << "Element Frequency" << endl;
for ( auto i : m)
cout << i.first << " " << i.second << endl;
return 0;
}
|
OutputElement Frequency
1 4
2 1
3 2
4 1
Note: The above output is for map only. For unordered_map, the output rows can be in any order.
2. Traversing using begin() and end()
The begin() and end() are the member functions of container classes that return the iterator to the first and the last key-value pair in a map or unordered_map respectively. We can use them to traverse over a map or an unordered_map.
Example:
map
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 1, 1, 2, 1, 1, 3, 4, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
map< int , int > m;
for ( int i = 0; i < n; i++)
m[arr[i]]++;
cout << "Element Frequency" << endl;
for ( auto i = m.begin(); i != m.end(); i++)
cout << i->first << " \t\t\t" << i->second << endl;
return 0;
}
|
unordered_map
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 1, 1, 2, 1, 1, 3, 4, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
unordered_map< int , int > m;
for ( int i = 0; i < n; i++)
m[arr[i]]++;
cout << " Element Frequency" << endl;
for ( auto i = m.begin(); i != m.end(); i++)
cout << i->first << " " << i->second << endl;
return 0;
}
|
OutputElement Frequency
1 4
2 1
3 2
4 1
3. Iterating over a map by using an STL Iterator
We can create an iterator of std::map (or std::unordered_map), initialize it to the start of the map, and increment it till the end to traverse the map.
Example:
C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <string>
int main()
{
std::map<std::string, int > ExampleMap;
ExampleMap.insert(
std::pair<std::string, int >( "Sunday" , 1));
ExampleMap.insert(
std::pair<std::string, int >( "Monday" , 2));
ExampleMap.insert(
std::pair<std::string, int >( "Tuesday" , 3));
ExampleMap.insert(
std::pair<std::string, int >( "Wednesday" , 4));
ExampleMap.insert(
std::pair<std::string, int >( "Thursday" , 5));
ExampleMap.insert(
std::pair<std::string, int >( "Friday" , 6));
ExampleMap.insert(
std::pair<std::string, int >( "Saturday" , 7));
std::map<std::string, int >::iterator it
= ExampleMap.begin();
while (it != ExampleMap.end()) {
std::string word = it->first;
int count = it->second;
std::cout << word << " :: " << count << std::endl;
it++;
}
return 0;
}
|
OutputFriday :: 6
Monday :: 2
Saturday :: 7
Sunday :: 1
Thursday :: 5
Tuesday :: 3
Wednesday :: 4
4. Iterating over a map by using std::for_each and lambda function
By using the std::for_each algorithm and lambda functions we can easily iterate over all the elements of the map. Here the lambda function will be used as a call-back function and will receive each map entry.
Example:
map
#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <string>
int main()
{
std::map<std::string, int > ExampleMap;
ExampleMap.insert(
std::pair<std::string, int >( "Sunday" , 1));
ExampleMap.insert(
std::pair<std::string, int >( "Monday" , 2));
ExampleMap.insert(
std::pair<std::string, int >( "Tuesday" , 3));
ExampleMap.insert(
std::pair<std::string, int >( "Wednesday" , 4));
ExampleMap.insert(
std::pair<std::string, int >( "Thursday" , 5));
ExampleMap.insert(
std::pair<std::string, int >( "Friday" , 6));
ExampleMap.insert(
std::pair<std::string, int >( "Saturday" , 7));
std::map<std::string, int >::iterator it
= ExampleMap.begin();
std::for_each(
ExampleMap.begin(), ExampleMap.end(),
[](std::pair<std::string, int > p) {
std::cout << p.first << " :: " << p.second
<< std::endl;
});
return 0;
}
|
unordered_map
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <unordered_map>
int main()
{
std::unordered_map<std::string, int > ExampleMap;
ExampleMap.insert(
std::pair<std::string, int >( "Sunday" , 1));
ExampleMap.insert(
std::pair<std::string, int >( "Monday" , 2));
ExampleMap.insert(
std::pair<std::string, int >( "Tuesday" , 3));
ExampleMap.insert(
std::pair<std::string, int >( "Wednesday" , 4));
ExampleMap.insert(
std::pair<std::string, int >( "Thursday" , 5));
ExampleMap.insert(
std::pair<std::string, int >( "Friday" , 6));
ExampleMap.insert(
std::pair<std::string, int >( "Saturday" , 7));
std::unordered_map<std::string, int >::iterator it
= ExampleMap.begin();
std::for_each(ExampleMap.begin(), ExampleMap.end(),
[](std::pair<std::string, int > p) {
std::cout << p.first
<< " :: " << p.second
<< std::endl;
});
return 0;
}
|
OutputFriday :: 6
Monday :: 2
Saturday :: 7
Sunday :: 1
Thursday :: 5
Tuesday :: 3
Wednesday :: 4
This article is contributed by Kartik. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.