C++ Iterate map

In this article, we will discuss 4 different ways to iterate map in c++.

Iterate map using iterator

First declare a iterator of suitable type and initialize it to the beginning of the map.

map<int,int>::iterator itr = Map.begin();

After that, use a while to iterate over the map. Keep incrementing iterator until iterator reaches the end of the map.
itr->first is used to access the Key.
itr->second is used to access the Value.

Iterator is a pointer that’s why ‘->’ is used instead of ‘.’ (dot operator).

while(itr!=Map.end()){
	cout<<"Key: "<<itr->first<<" Value: "<<itr->second<<endl;
	itr++;
}

Example,

#include<iostream>
#include<map>
#include<iterator>
using namespace std;

void IterateMap(map<int,int> Map){
	map<int,int>::iterator itr = Map.begin();
	while(itr!=Map.end()){
		cout<<"Key: "<<itr->first<<" Value: "<<itr->second<<endl;
		itr++;
	}
	cout<<endl;
}

int main()
{
	map<int,int> Map;
	Map[8] = 3;
	Map[34] = 45;
	Map[20] = 20;
	Map[6] = 4;
	
	IterateMap(Map);
 	   
}
using iterator

Iterate map using ranged based for loop

Range based for loop executes loop over a range. ( C++ 11 and above )

for(auto &v: Map){
	cout<<"Key: "<<v.first<<" Value: "<<v.second<<endl;
}

The for loop iterate over the map and stores the reference of each object in v.
v.first gives us the Key and v.second gives us the Value for the corresponding Key.
v is not a pointer. That’s why we use ‘.’ (dot operator).

We can use ranged based loop in another way. ( C++ 17 )

for(auto &[key,value]: Map){
	cout<<"Key: "<<key<<" Value: "<<value<<endl;
}

The first parameter key stores the Key and the second parameter value stores the Value of the corresponding key.

Example,

#include<iostream>
#include<map>
#include<iterator>
using namespace std;

void IterateMap(map<int,int> Map){
	for(auto &v: Map){
		cout<<"Key: "<<v.first<<" Value: "<<v.second<<endl;
	}
	cout<<endl;
}

int main()
{
	map<int,int> Map;
	Map[10] = 3;
	Map[3] = 4;
	Map[5] = 2;
	Map[6] = 1;
	
	IterateMap(Map);
}
using ranged based for loop

Iterate map using std::for_each

for_each(first, last, func);

std::for_each applies a given function func on all objects in the range [ first , last ) in order.

We add the operations that we want to perform on each key-value pair in a separate function and pass the function as a parameter to std::for_each.

void IterateMap(map<int,int> Map){
	for_each(Map.begin(),Map.end(),[](auto &v){
		cout<<"Key: "<<v.first<<" Value: "<<v.second<<endl;
	});
}

Example,

#include<iostream>
#include<map>
#include<iterator>
#include<algorithm>
using namespace std;

void IterateMap(map<int,int> Map){
	for_each(Map.begin(),Map.end(),[](auto &v){
		cout<<"Key: "<<v.first<<" Value: "<<v.second<<endl;
	});
}

int main()
{
	map<int,int> Map;
	Map[10] = 3;
	Map[3] = 4;
	Map[5] = 2;
	Map[6] = 1;
	Map[15] = 15;
	
	IterateMap(Map);
 	   
}
using for_each

Iterate map using std::transform

transform(first, last, result, func);

std::transform applies the given function func on each object in the range [ first , last ). But in std::transform, func must return a value which is stored at result.

transform(Map.begin(),Map.end(),inserter(Map,Map.end()),[](auto &v){
		cout<<"Key: "<<v.first<<" Value: "<<v.second<<endl;
		return v;
	});
}

std::inserter inserts the value returned by the function in Map. Since the value is already present in the Map, it makes no difference.

Example,

#include<iostream>
#include<map>
#include<iterator>
#include<algorithm>
using namespace std;

void IterateMap(map<int,int> Map){
	transform(Map.begin(),Map.end(),inserter(Map,Map.end()),[](auto &v){
		cout<<"Key: "<<v.first<<" Value: "<<v.second<<endl;
		return v;
	});
}

int main()
{
	map<int,int> Map;
	Map[10] = 3;
	Map[3] = 4;
	Map[6] = 1;
	Map[15] = 15;
	
	IterateMap(Map);
 	   
}
using transform

Leave a Comment

Your email address will not be published.