There are multiple ways to iterate through map in C++. Here we will discuss five methods that can be used to to iterate through map in C++.
Table of Contents
- Using range-based for loop to to iterate through map in C++
- Using range-based for loop to iterate through key-value pairs of map
- Using for_each and lambda function to iterate through map in C++
- Using simple ‘for loop’ using begin() and end() methods to iterate through map in C++
- Using standard template library iterator and while loop to iterate through map in C++
- Conclusion
Using range-based
for loop to to iterate through map in C++
This method is the first and the common choice for iterating over map elements. This method is supported in C++11 version and if your compiler supports it, then it is one
of the easiest ways to iterate.
Note: We use ‘auto’ type specifier as it is recommended for readability and we don’t have to mention the data types of key-value pairs every time.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <map> using namespace std; int main(){ map<int, string> test = {{1, "Java",}, {2, "Python",}, {3, "C++",}, {4, "Javascript",} }; for (const auto &item: test) { cout << "{" << item.first << "," << item.second << "}\n"; } cout << endl; return 0; } |
Output:
{2,Python}
{3,C++}
{4,Javascript}
Using range-based
for loop to iterate through key-value pairs of map
in C++
This method was defined in the C++17
version for better flexibility for iteration in the containers. The main benefit of using this method is the easy access of key-value pairs in the map structure. It helps in ensuring better readability as well.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <map> using namespace std; int main(){ map<int, string> test = {{1, "Java",}, {2, "Python",}, {3, "C++",}, {4, "Javascript",} }; for (const auto& [key,value] : test) { cout << "{" << key << ", " << value << "}\n"; } cout << endl; return 0; } |
Output:
{2, Python}
{3, C++}
{4, Javascript}
Using for_each and lambda function to iterate through map in C++
In this method, we use the standard template library algorithm for_each
to iterate through the map in C++. This loop will iterate on each element of the map and will call the callback provided. We can use a lambda function as callback here. The lambda function will get each element in a pair.
Code:
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 |
#include <iostream> #include <map> #include <string> #include <iterator> #include <algorithm> using namespace std; int main(){ map<int, string> test = {{1, "Java",}, {2, "Python",}, {3, "C++",}, {4, "Javascript",}, }; //creating a map iterator which points at the beginning of the map map<int, string>:: iterator it=test.begin(); //iterating over a map using lambda and for_each for_each(test.begin(), test.end(),[ ](pair<int, string> element) { //accessing key from the elements int number=element.first; //accessing values from elements string word=element.second; cout << "{" << number << ", " << word << "}\n"; }); return 0; } |
Output:
{2, Python}
{3, C++}
{4, Javascript}
Using simple ‘for loop’ using begin() and end() methods to iterate through map in C++
We can iterate through map by using the traditional for loop. We will use begin()
and end()
method to point at the beginning and ending positions respectively. This method will come at last position when it comes to readability.
Code:
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 |
#include <iostream> #include <map> #include <string> #include <iterator> #include <algorithm> using namespace std; int main(){ map<int, string> test={{1, "Java",}, {2, "Python",}, {3, "C++",}, {4, "Javascript",}, }; //accessing the beginning position of the map map<int, string> :: iterator it=test.begin(); //iteration using traditional for loop for(it=test.begin();it!=test.end();it++) { //accessing keys int number=it->first; //accessing values string word=it->second; //print the elements cout << "{" << number << ", " << word << "}\n"; } return 0; } |
Output:
{2, Python}
{3, C++}
{4, Javascript}
Using standard template library iterator
and while loop
to iterate through map in C++
We declare a header ‘
Note: Map stores elements in a pair format, so each iterator points to the address of the pair.
Code:
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 |
#include <iostream> #include <map> #include <string> #include <iterator> #include <algorithm> using namespace std; int main(){ map<int, string> test={{1, "Java",}, {2, "Python",}, {3, "C++",}, {4, "Javascript",}, }; //accessing the beginning position of the map map<int, string> :: iterator it=test.begin(); //iteration using while loop while(it != test.end()) { //accessing keysint number=it->first; //accessing values int number=it->first; string word=it->second; //print the elements cout << "{" << number << ", " << word << "}\n"; it++; } return 0; } |
Output:
{2, Python}
{3, C++}
{4, Javascript}
Conclusion
In this article, we discussed five different ways to to iterate through map in C++. Some methods are supported by the latest versions of C++ only.
Happy Learning!