Iterate through map in C++

Iterate through map in C++

There are multiple ways to iterate through map in C++. Here we will discuss five methods that can be used to to iterate through map in C++.

Using range-based for loop to to iterate through map in C++

This method is the first and the common choice for iterating over map elements. This method is supported in C++11 version and if your compiler supports it, then it is one
of the easiest ways to iterate.

Note: We use ‘auto’ type specifier as it is recommended for readability and we don’t have to mention the data types of key-value pairs every time.

Code:

Output:

{1,Java}
{2,Python}
{3,C++}
{4,Javascript}

Using range-based for loop to iterate through key-value pairs of map

in C++
This method was defined in the C++17 version for better flexibility for iteration in the containers. The main benefit of using this method is the easy access of key-value pairs in the map structure. It helps in ensuring better readability as well.
Code:

Output:

{1, Java}
{2, Python}
{3, C++}
{4, Javascript}

Using for_each and lambda function to iterate through map in C++

In this method, we use the standard template library algorithm for_each to iterate through the map in C++. This loop will iterate on each element of the map and will call the callback provided. We can use a lambda function as callback here. The lambda function will get each element in a pair.
Code:

Output:

{1, Java}
{2, Python}
{3, C++}
{4, Javascript}

Using simple ‘for loop’ using begin() and end() methods to iterate through map in C++

We can iterate through map by using the traditional for loop. We will use begin() and end() method to point at the beginning and ending positions respectively. This method will come at last position when it comes to readability.
Code:

Output:

{1, Java}
{2, Python}
{3, C++}
{4, Javascript}

Using standard template library iterator and while loop to iterate through map in C++

We declare a header ‘’ to create an iterator of the map and then it will be initialized to the start position of the map. Then we can iterate over a map by incrementing the value of the iterator until it reaches the last position of the map.

Note: Map stores elements in a pair format, so each iterator points to the address of the pair.

Code:

Output:

{1, Java}
{2, Python}
{3, C++}
{4, Javascript}

Conclusion

In this article, we discussed five different ways to to iterate through map in C++. Some methods are supported by the latest versions of C++ only.
Happy Learning!

Related Posts

  • 19 May

    Get Type of Object in C++

    Table of ContentsWhy Should We Determine the Type of Object in C++: Dynamic DispatchingGet Type of Object in C++Conclusion C++ is an object-oriented language where we often interact with objects of different types. The object interaction becomes more interesting when we add runtime polymorphism to it. The runtime polymorphism, also known as, dynamic dispatch of […]

  • 19 May

    Convert Vector to Array in C++

    Table of ContentsVectors and Arrays in C++Convert Vector to Arrays in C++Using the for loop to convert vector to array in C++Using the copy() function to convert vector to array in C++Using the transform() function to convert vector to array in C++Using the data() function to convert vector to array in C++Using the & operator […]

  • How to initialize an array in Constructor in C++
    23 April

    How to initialize an Array in Constructor in C++

    Table of ContentsArraysConstructors of a ClassWays to initialize an array in Constructor in C++Initialize an array in Constructor With std::fill()Initialize an array in Constructor Using for LoopInitialize an array in Constructor Using Member Initializer ListConclusion This article discusses methods of initializing an array in constructor in C++. Let us first discuss arrays and constructors in […]

  • Write String to file in C++
    31 March

    Write String to File in C++

    Table of ContentsUsing the fstream Library to write String to File in C++Using the Insertion Operator (<<) With the fstream ObjectUsing the write() Function of the fstream LibraryUsing the fwrite() Function to write String to File in C++Conclusion The files are used to store the data permanently on a disk. This mitigates the problem of […]

  • Print Array in C++
    23 March

    Print Array in C++

    Table of ContentsIntroduction of ArraysDifferent Methods to Print an Array in C++print array in C++ by Traversing Indicesprint array in C++ Using for_each() Functionprint array in C++ Using Range-Based for Loopprint array in C++ Using Iteratorsprint array Using ostream_iterator() Function and copy() Functionprint array Using C++17 copy() Function and make_ostream_joiner() FunctionConclusion An array is a […]

  • Print Vector C++
    23 March

    Print Vector in C++

    Table of ContentsPrint Vector in C++ Using IndicesPrint Vector in C++ Using Range-Based for LoopPrint Vector in C++ Using for_each() FunctionPrint Vector in C++ Using IteratorPrint Vector in C++ by Overloading the Insertion Operator (<<)Print Vector in C++ Using copy() FunctionConclusion This article discusses the vector and how to print vector in C++. Vectors are […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.