How To Iterate Through A Map C++
C/C++ Programming
C is one of the almost basic programming linguistic communication that we tin 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 linguistic communication at the Bell Laboratories. It supports procedural oriented programming and is extensively used in variety of applications while being machine independent.
C++ was developed past Bjarne Stroustrup at Bong Labs, it supports Object-Oriented Programming (OOP) and serves equally 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 linguistic communication than C because of its object-oriented nature.
In this commodity, we volition be looking at some possible ways to iterate through map C++/C.
Iterate through Maps C++/ C
C++ Maps are the associated containers that shop elements in a mapped fashion using central-value pairs. The key for each value in a C++ Map is always unique. Fundamental in C++ map can be used for performing various operations such equally sorting.
Nosotros volition now be looking at three means 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 divers C++ map structure. So let'due south ascertain a C++ map structure with the proper name of demoMap and fill it with some central value pairs. After on, we will iterate through it to run across the results using a while loop.
#include <iostream> #include <map> using std::map; using std::cord; using std::cout; using std::endl; using std::cin; int main() { map<int, string> demoMap = {{1, "Pismire",}, {2, "Elephant",}, {3, "Lion",}, {4, "Fox",}, {5, "Zebra",}, {6, "Bear",}}; auto iter = demoMap.begin(); while (iter != demoMap.end()) { cout << "[" << iter->start << "," << iter->2d << "]\n"; ++iter; } cout << endl; return 0; }
The demoMap.begin() points out towards the first entry in map which is existence 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.finish() .
On successful execution, you should run across the following output:
2. C++ TRADITIONAL For Loop
At present let'south do the aforementioned program with a different arroyo, this time with a for loop. For loop is considered the almost 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 chief() { map<int, string> demoMap = {{ane, "Ant",}, {two, "Elephant",}, {3, "Lion",}, {4, "Fox",}, {5, "Zebra",}, {6, "Bear",}}; for (automobile iter = demoMap.brainstorm(); iter != demoMap.end(); ++iter){ cout << "[" << iter->first << "," << iter->2d << "]\n"; } cout << endl; render 0; }
Pay shut attention to the looping construction. 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 concluding chemical element using terminate() , finally the iterator variable iter is beingness incremented after every successful loop execution.
Nevertheless, 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 capeesh 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, cord> demoMap = {{1, "Ant",}, {2, "Elephant",}, {3, "Lion",}, {4, "Flim-flam",}, {5, "Zebra",}, {6, "Bear",}}; for (const auto &item : demoMap) { cout << "[" << detail.start << "," << item.2d << "]\northward"; } cout << endl; render 0; }
This blazon of range based for loop will have as many iterations every bit the number of elements in demoMap . Each detail element in an iteration will exist 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 most C and C++ programming languages and the type of applications that they are used in besides, how maps tin 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 as well analyzed the readability of each coding mode and then far the while loop approach has been the best among all.
Source: https://www.codeleaks.io/iterate-through-maps-in-c-programming/
0 Response to "How To Iterate Through A Map C++"
Post a Comment