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:
// C++ program to traverse a map using begin() and end() #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]);
// inserting data to map
map< int , int > m;
for ( int i = 0; i < n; i++)
m[arr[i]]++;
// Printing of MAP
cout << "Element Frequency" << endl;
for ( auto i = m.begin(); i != m.end(); i++)
cout << i->first << " \t\t\t" << i->second << endl;
return 0;
} |
// C++ program to traverse a unordered_map // using begin() and end() #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]]++;
// Printing of Unordered_MAP
cout << " Element Frequency" << endl;
for ( auto i = m.begin(); i != m.end(); i++)
cout << i->first << " " << i->second << endl;
return 0;
} |
Element 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:
// C++ program to traverse a map using Iterators() #include <algorithm> #include <iostream> #include <iterator> #include <map> #include <string> int main()
{ // Map created
std::map<std::string, int > ExampleMap;
// elements are inserted into map
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));
// map iterator created
// iterator pointing to start of map
std::map<std::string, int >::iterator it
= ExampleMap.begin();
// Iterating over the map using Iterator till map end.
while (it != ExampleMap.end()) {
// Accessing the key
std::string word = it->first;
// Accessing the value
int count = it->second;
std::cout << word << " :: " << count << std::endl;
// iterator incremented to point next item
it++;
}
return 0;
} |
// C++ program to traverse a map using // std::for_each() and lambda() #include <algorithm> #include <iostream> #include <iterator> #include <map> #include <string> int main()
{ // Map created
std::map<std::string, int > ExampleMap;
// elements are inserted into map
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));
// map iterator created
// iterator pointing to start of map
std::map<std::string, int >::iterator it
= ExampleMap.begin();
// Iterating over the map till map end.
std::for_each(
ExampleMap.begin(), ExampleMap.end(),
[](std::pair<std::string, int > p) {
std::cout << p.first << " :: " << p.second
<< std::endl;
});
return 0;
} |
Element 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++ program to traverse a unordered_map using // std::for_each() and lambda() #include <algorithm> #include <iostream> #include <iterator> #include <string> #include <unordered_map> int main()
{ // Map created
std::unordered_map<std::string, int > ExampleMap;
// elements are inserted into unordered_map
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));
// unordered_map iterator created
// iterator pointing to start of unordered_map
std::unordered_map<std::string, int >::iterator it
= ExampleMap.begin();
// Iterating over the unordered_map till unordered_map
// end.
std::for_each(ExampleMap.begin(), ExampleMap.end(),
[](std::pair<std::string, int > p) {
std::cout << p.first
<< " :: " << p.second
<< std::endl;
});
return 0;
} |
Friday :: 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:
// C++ program to traverse a unordered_map using // std::for_each() and lambda() #include <algorithm> #include <iostream> #include <iterator> #include <string> #include <unordered_map> int main()
{ // Map created
std::unordered_map<std::string, int > ExampleMap;
// elements are inserted into unordered_map
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));
// unordered_map iterator created
// iterator pointing to start of unordered_map
std::unordered_map<std::string, int >::iterator it
= ExampleMap.begin();
// Iterating over the unordered_map till unordered_map
// end.
std::for_each(ExampleMap.begin(), ExampleMap.end(),
[](std::pair<std::string, int > p) {
std::cout << p.first
<< " :: " << p.second
<< std::endl;
});
return 0;
} |
Friday :: 6 Monday :: 2 Saturday :: 7 Sunday :: 1 Thursday :: 5 Tuesday :: 3 Wednesday :: 4