Table of Contents
C/C++ Programming
C is one of the most basic programming language that we can count on to develop system software like operating systems to complex application programs like Oracle database, Git, and many more.
Dennis Ritchie developed C language at the Bell Laboratories. It supports procedural oriented programming and is extensively used in variety of applications while being machine independent.
C++ was developed by Bjarne Stroustrup at Bell Labs, it supports Object-Oriented Programming (OOP) and serves as a special purpose programming language.
Despite having remarkable similarities and extensive compatibility of C++ with C language, C++ is still considered a safer and well-structured programming language than C because of its object-oriented nature.
In this article, we will be looking at some possible ways to iterate through map C++/C.
Iterate through Maps C++/ C
C++ Maps are the associated containers that store elements in a mapped fashion using key-value pairs. The key for each value in a C++ Map is always unique. Key in C++ map can be used for performing various operations such as sorting.
We will now be looking at three ways to iterate through maps C++, those are:
- Using While Loop.
- Using Traditional For Loop.
- Using Range-Based For Loop.
1. C++ While Loop
Before starting iteration, we need a defined C++ map structure. So let’s define a C++ map structure with the name of demoMap and fill it with some key value pairs. Later on, we will iterate through it to see the results using a while loop.
#include <iostream>
#include <map>
using std::map;
using std::string;
using std::cout;
using std::endl;
using std::cin;
int main() {
map<int, string> demoMap = {{1, "Ant",},
{2, "Elephant",},
{3, "Lion",},
{4, "Fox",},
{5, "Zebra",},
{6, "Bear",}};
auto iter = demoMap.begin();
while (iter != demoMap.end()) {
cout << "[" << iter->first << ","
<< iter->second << "]\n";
++iter;
}
cout << endl;
return 0;
}
The demoMap.begin() points out towards the first entry in map which is being stored in the iterator variable iter. The while loop while continue execution until the iterator variable iter becomes equal to the last entry of the map which is being fetched using demoMap.end().
On successful execution, you should see the following output:

2. C++ TRADITIONAL For Loop
Now let’s practice the same program with a different approach, this time with a for loop. For loop is considered the most handy iterative structure for beginners, but iterating maps using for loop is arguably the bad approach when it comes to code readability.
#include <iostream>
#include <map>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::map;
int main() {
map<int, string> demoMap = {{1, "Ant",},
{2, "Elephant",},
{3, "Lion",},
{4, "Fox",},
{5, "Zebra",},
{6, "Bear",}};
for (auto iter = demoMap.begin(); iter != demoMap.end(); ++iter){
cout << "[" << iter->first << ","
<< iter->second << "]\n";
}
cout << endl;
return 0;
}
Pay close attention to the looping structure. The loop is initialized with the begin() method which means the first element of the map, the conditional statement checks if the iterator has reached to the end of map by comparing each element to the last element using end(), finally the iterator variable iter is being incremented after every successful loop execution.
However, the output will be the same,

3. C++ Range based for Loop
Range-based loops have been the common choice for C++ programmers for a while. If your compiler supports C++11 version, then you should think no more about traditional cumbersome loops and appreciate the elegance of the following example:
#include <iostream>
#include <map>
using std::cout;
using std::cin;
using std::endl;
using std::map;
using std::string;
int main() {
map<int, string> demoMap = {{1, "Ant",},
{2, "Elephant",},
{3, "Lion",},
{4, "Fox",},
{5, "Zebra",},
{6, "Bear",}};
for (const auto &item : demoMap) {
cout << "[" << item.first << "," << item.second << "]\n";
}
cout << endl;
return 0;
}
This type of range based for loop will take as many iterations as the number of elements in demoMap. Each particular element in an iteration will be picked and assigned to the item variable, following the approach the variable will keep holding new elements from map every time the loop initiates a new iteration, until the map reaches to an end.

Conclusion
In this article, we discussed generally about C and C++ programming languages and the type of applications that they are used in also, how maps can be created in C/C++ and also looked at the three methods to iterate over maps which are using a while loop, using a traditional for loop, and using a range based for loop. We also analyzed the readability of each coding style and so far the while loop approach has been the best among all.