DEV Community

Lalit Kumar
Lalit Kumar

Posted on

How to iterate through a map in C++?

We are going to discuss three methods of iterating through a map. So in this article, we are going to take a look at all these three methods and we will be learning how to iterate through a map. Assume we have a map of string. std::map mapofwordcount;


title: "originally posted here 👇"

canonical_url: https://kodlogs.com/blog/400/how-to-iterate-through-a-map-in-c

Methods

  • Using STL iterator
  • Using range-based for loop
  • Using for_each and lambda function

Description of all methods

STL iterator

Create an iterator of the map and initialize it at the beginning of the map like this

std::map<std::string, int>::iterator it = mapOfWordCount.begin();

Now use the increment operator to iterate over the map till it reaches the end of the map. The map will be storing elements in std::pair format internally; it means each iterator object points to an address of pair.

#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;
}
Enter fullscreen mode Exit fullscreen mode

Range-based for loop

In CPP there is a range-based for loop, we can also use that for loop to iterate. In this method, we have to type less than the previous method.

#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;
}
Enter fullscreen mode Exit fullscreen mode

Using std::for_each and lambda function

We can also use this method to iterate over the map. These functions will iterate on each map entry and call back provided by us. Lambda function will receive each map entry in pair.

#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;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)