. Advertisement .
..3..
. Advertisement .
..4..
This instruction will guide you thoroughly on how to iterate through map C++. Little did you know, there are even more sophisticated techniques to conduct this process using subsequent versions of the language.
In this tutorial, 4 different approaches will be discussed below.
How To Iterate Through Map C++?
Method #1: Utilize Traditional for Loop to Iterate Over std::map Elements
Let’s now employ the conventional for iteration method, which is perhaps the least readable.
Running the code:
#include <iostream>
#include <map>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::map;
int main() {
map<int, string> tempMap = {{1, "Candy",},
{2, "Chocolate",},
{3, "Sweet",},
{4, "Croissant",},
{5, "Cherry",},
{6, "Cookie",}};
for (auto iter = tempMap.begin(); iter != tempMap.end(); ++iter){
cout << "[" << iter->first << ","
<< iter->second << "]\n";
}
cout << endl;
return 0;
}
Output:
[1,Candy]
[2,Chocolate]
[3,Sweet]
[4,Croissant]
[5,Cherry]
[6,Cookie]
Method #2: Employ while Loop to Iterate Over std::map Elements
To further illustrate possible solutions, we first construct the temporary map structure tempMap and populate it with random key/value pairs, which we will emit to stdout.
Running the code:
#include <iostream>
#include <map>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::map;
int main() {
map<int, string> tempMap = {{1, "Candy",},
{2, "Chocolate",},
{3, "Sweet",},
{4, "Croissant",},
{5, "Cherry",},
{6, "Cookie",}};
auto iter = tempMap.begin();
while (iter != tempMap.end()) {
cout << "[" << iter->first << ","
<< iter->second << "]\n";
++iter;
}
cout << endl;
return 0;
}
Output:
[1,Candy]
[2,Chocolate]
[3,Sweet]
[4,Croissant]
[5,Cherry]
[6,Cookie]
Method #3: Adopt Range-Based for Loop to Iterate Over std::map Key-Value Pairs
Since the C++17 standard, this version has been established to provide associative containers with iteration that is more flexible.
The key-value access convenience in the map structure, which also assures improved readability for a programmer, is this method’s major benefit over earlier instances.
Running the code:
#include <iostream>
#include <map>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::map;
int main() {
map<int, string> tempMap = {{1, "Candy",},
{2, "Chocolate",},
{3, "Sweet",},
{4, "Croissant",},
{5, "Cherry",},
{6, "Cookie",}};
for (const auto& [key, value] : tempMap) {
cout << "[" << key << "," << value << "]\n";
}
cout << endl;
return 0;
}
Output:
[1,Candy]
[2,Chocolate]
[3,Sweet]
[4,Croissant]
[5,Cherry]
[6,Cookie]
Method #4: Utilize Range-Based for Loop to Iterate Over std::map Elements
For a time now, range-based loops have been the go-to option for C++ writers.
If your compiler is compatible with C++11, you should stop using old-fashioned, inefficient loops and instead admire the elegance of the given example.
Running the code:
#include <iostream>
#include <map>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::map;
int main() {
map<int, string> tempMap = {{1, "Candy",},
{2, "Chocolate",},
{3, "Sweet",},
{4, "Croissant",},
{5, "Cherry",},
{6, "Cookie",}};
for (const auto &item : tempMap) {
cout << "[" << item.first << "," << item.second << "]\n";
}
cout << endl;
return 0;
}
Output:
[1,Candy]
[2,Chocolate]
[3,Sweet]
[4,Croissant]
[5,Cherry]
[6,Cookie]
Conclusion
The information provided above should be sufficient to understand how to iterate through map C++.
Make sure you’ve read this article completely so you can benefit greatly from these ideas. Also, don’t be afraid to comment if you have any questions about anything!
Leave a comment