In this article we will discuss 3 different ways to Iterate over a map in C++.
Suppose we have a map of string and int as key-value pair i.e.
std::map<std::string, int> mapOfWordCount;
Now let’s see how to iterate over this map in 3 different ways i.e.
Iterate over a map using STL Iterator
First of all, create an iterator of std::map and initialize it to the beginning of map i.e.
Frequently Asked:
std::map<std::string, int>::iterator it = mapOfWordCount.begin();
Now, let’s iterate over the map by incrementing the iterator until it reaches the end of map. Also, map internally stores element in a std::pair format, therefore each iterator object points to an address of pair.
Access key from iterator using,
it->first
Access value from iterator using,
it->second
Complete example of iterating over a map using stl iterator is as follows,
Best Resources to Learn C++:
#include <iostream> #include <map> #include <string> #include <iterator> #include <algorithm> int main() { std::map<std::string, int> mapOfWordCount; // Insert Element in map mapOfWordCount.insert(std::pair<std::string, int>("first", 1)); mapOfWordCount.insert(std::pair<std::string, int>("second", 2)); mapOfWordCount.insert(std::pair<std::string, int>("third", 3)); mapOfWordCount.insert(std::pair<std::string, int>("third", 4)); mapOfWordCount.insert(std::pair<std::string, int>("third", 5)); // Create a map iterator and point to beginning of map std::map<std::string, int>::iterator it = mapOfWordCount.begin(); // Iterate over the map using Iterator till end. while (it != mapOfWordCount.end()) { // Accessing KEY from element pointed by it. std::string word = it->first; // Accessing VALUE from element pointed by it. int count = it->second; std::cout << word << " :: " << count << std::endl; // Increment the Iterator to point to next entry it++; } return 0; }
Output:
first :: 1 second :: 2 third :: 3
Iterating over the map using C++11 range based for loop
C++11 provides a range based for loop, we can also use that to iterate over the map. In that case we don’t need iterate and it will take less coding. Check out the following example,
#include <iostream> #include <map> #include <string> #include <iterator> #include <algorithm> int main() { std::map<std::string, int> mapOfWordCount; // Insert Element in map mapOfWordCount.insert(std::pair<std::string, int>("first", 1)); mapOfWordCount.insert(std::pair<std::string, int>("second", 2)); mapOfWordCount.insert(std::pair<std::string, int>("third", 3)); mapOfWordCount.insert(std::pair<std::string, int>("third", 4)); mapOfWordCount.insert(std::pair<std::string, int>("third", 5)); // Create a map iterator and point to beginning of map std::map<std::string, int>::iterator it = mapOfWordCount.begin(); // Iterate over the map using c++11 range based for loop for (std::pair<std::string, int> element : mapOfWordCount) { // Accessing KEY from element std::string word = element.first; // Accessing VALUE from element. int count = element.second; std::cout << word << " :: " << count << std::endl; } return 0; }
Output:
first :: 1 second :: 2 third :: 3
Above example is using c++11 feature. So, to compile it on linux use following command,
g++ -std=c++11 example.cpp
Iterating over the map using std::for_each and lambda function
We can also use an stl algorithm std::for_each to iterate over the map. It will iterate on each of the map entry and call the callback provided by us. In below example we will use a lambda function as callback. Lambda function will receive each of the map entry in a pair. Checkout complete example as follows,
#include <iostream> #include <map> #include <string> #include <iterator> #include <algorithm> int main() { std::map<std::string, int> mapOfWordCount; // Insert Element in map mapOfWordCount.insert(std::pair<std::string, int>("first", 1)); mapOfWordCount.insert(std::pair<std::string, int>("second", 2)); mapOfWordCount.insert(std::pair<std::string, int>("third", 3)); mapOfWordCount.insert(std::pair<std::string, int>("third", 4)); mapOfWordCount.insert(std::pair<std::string, int>("third", 5)); // Create a map iterator and point to beginning of map std::map<std::string, int>::iterator it = mapOfWordCount.begin(); // Iterate over a map using std::for_each and Lambda function std::for_each(mapOfWordCount.begin(), mapOfWordCount.end(), [](std::pair<std::string, int> element){ // Accessing KEY from element std::string word = element.first; // Accessing VALUE from element. int count = element.second; std::cout<<word<<" :: "<<count<<std::endl; }); return 0; }
Output:
first :: 1 second :: 2 third :: 3
Above example is using c++11 feature. So, to compile it on linux use following command,
g++ -std=c++11 example.cpp
Leave a Reply
You must be logged in to post a comment.