banner



How To Iterate Over A Map C++

C/C++ Programming

C is i of the most bones 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. Information technology supports procedural oriented programming and is extensively used in variety of applications while being car contained.

C++  was adult by Bjarne Stroustrup at Bell Labs, information technology supports Object-Oriented Programming (OOP) and serves every bit a special purpose programming language.

Despite having remarkable similarities and extensive compatibility of C++ with C linguistic communication, C++ is however considered a safer and well-structured programming language than C because of its object-oriented nature.

In this article, we volition exist 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 manner using key-value pairs. The cardinal for each value in a C++ Map is always unique. Central 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:

  1. Using While Loop.
  2. Using Traditional For Loop.
  3. Using Range-Based For Loop.

i. C++ While Loop

Before starting iteration, we demand a defined C++ map construction. Then allow's define a C++ map structure with the proper name of demoMap and make full it with some key value pairs. Later on, nosotros will iterate through it to run into 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, cord> demoMap = {{1, "Ant",},                                 {ii, "Elephant",},                                 {3, "Lion",},                                 {4, "Fox",},                                 {5, "Zebra",},                                 {half-dozen, "Bear",}};       machine iter = demoMap.begin();     while (iter != demoMap.end()) {         cout << "[" << iter->offset << ","                     << iter->second << "]\n";         ++iter;     }     cout << endl;     render 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:

iterate through map C++

two. 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 construction for beginners, simply iterating maps using for loop is arguably the bad arroyo when information technology comes to lawmaking 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 = {{i, "Pismire",},                                 {ii, "Elephant",},                                 {iii, "Lion",},                                 {iv, "Play a trick on",},                                 {five, "Zebra",},                                 {half-dozen, "Carry",}};       for (machine iter = demoMap.begin(); iter != demoMap.end(); ++iter){         cout << "[" << iter->first << ","                     << iter->second << "]\due north";     }     cout << endl;     return 0; }                      

Pay close attention to the looping structure. The loop is initialized with the begin() method  which means the kickoff element of the map, the conditional statement checks if the iterator has reached to the end of map past comparing each element to the concluding element using finish() , finally the iterator variable iter is beingness incremented after every successful loop execution.

However, the output volition be the aforementioned,

iterate through map C++

3. C++ Range based for Loop

Range-based loops take been the common choice for C++ programmers for a while. If your compiler supports C++11 version, then you should recall no more virtually traditional cumbersome loops and appreciate the elegance of the following instance:

            #include <iostream> #include <map>   using std::cout; using std::cin; using std::endl; using std::map; using std::cord;   int chief() {     map<int, string> demoMap = {{one, "Ant",},                                 {2, "Elephant",},                                 {3, "Lion",},                                 {4, "Fox",},                                 {five, "Zebra",},                                 {6, "Bear",}};       for (const auto &item : demoMap) {         cout << "[" << particular.first << "," << item.second << "]\due north";     }     cout << endl;     render 0; }                      

This blazon of range based for loop will have 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, post-obit the approach the variable will keep belongings new elements from map every time the loop initiates a new iteration, until the map reaches to an end.

iterate through map C++

Conclusion

In this article, we discussed more often than not 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 3 methods to iterate over maps which are using a while loop, using a traditional for loop, and using a range based for loop. Nosotros also analyzed the readability of each coding style and then far the while loop approach has been the best amongst all.

Source: https://www.codeleaks.io/iterate-through-maps-in-c-programming/

0 Response to "How To Iterate Over A Map C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel