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

  • Trim String in C++
    19 July

    Trim String in C++

    Table of ContentsUsing Boost string algorithms to trim string in C++Using find_first_not_of() and find_last_not_of()` to trim strings in C++Using find_if() to trim the string in C++Using stringstream to trim the strings in C++Using a customized function to trim the strings in C++Conclusion When we take an input from an user, strings can have unwanted whitespaces […]

  • JSON parser in C++
    19 July

    JSON Parser in C++

    Table of ContentsSimple JSON Parser in C++ using JsonCpp libraryConclusion In this post, we will see about JSON parser in C++. There is no native support for JSON in C++. We can use a number of libraries that provide support for JSON in C++. We will use JsonCpp to parse JSON files in C++ which […]

  • Enum to String in C++
    19 July

    Convert enum to string in C++

    Table of ContentsUsing stringify macro method to convert enum to String in C++Using const char* Array to convert enum to String in C++Using a custom-defined function to convert enum to String in C++Conclusion There are a variety of methods to convert enum to String in C++. In this article, we will discuss some of the […]

  • C++ random number between 0 and 1
    11 June

    C++ random number between 0 and 1

    Table of ContentsUsing the rand() function to generate random number between 0 and 1 in C++Using the std::uniform_real_distribution() function to generate random number between 0 and 1 in C++ Random numbers are frequently used in programming for test case generation, cryptography, and other related purposes. In this tutorial, we will generate random number between 0 […]

  • String contains C++
    07 June

    Check if string contains substring in C++

    Table of ContentsUsing find() function to check if string contains substring in C++.Using strstr() method to check if string contains substring in C++Using boost library to check if string contains substring in C++Using custom functions to check if string contains substring in C++Conclusion There are different ways to check if string contains substring in C++. […]

  • Convert float to String in C++
    24 May

    Convert float to String in C++

    Learn about how to convert float to String in C++ in different wayds.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.