Print Map in C++

Print Map in C++

Maps in C++ store the data in form of key and value pairs. Each value is identified by a key and no two keys are the same.

Maps are widely used in problems that involve searching large data such as a database, cache applications, data compression, etc.

This article discusses the two different types of maps in C++ and the methods to print map in C++.

Map vs Unordered Map

There are two different types of maps found in C++. These types are different in the implementation as well as in the performance.

  • The map is defined in the “map” header file and stores the key-value pairs in the sorted order by keys (increasing by default).
  • It is internally implemented using the self-balancing binary search tree such as the red-black tree.
  • On the other hand, the unordered map is found in the “unordered_map” header file.
  • It has no specific ordering and is implemented using the hash table.
  • The time complexity of insertion, deletion, and searching operation in O(log n) in the case of the map.
  • While the unordered map has the O(1) average-case time complexity of these operations.

Print Map in C++ Using Iterator

An iterator is similar to a pointer that points to a specific memory location of the associated container.

You can traverse and print a map as well as an unordered map using iterators in a loop. You can define an iterator as given below.

Iterator for map:

Iterator for unordered map:

Let us see the code to print a map.

Notice the use of make_pair() function. It takes two values and creates a key-value pair for inserting them into the map.

Also notice the begin() and end() function. They return the starting and ending iterator respectively.

Output:

1 Name
2 Age
3 Class
4 Section

Notice that the output is sorted by keys even though the code inserted the data in a different order.

Note that you can use the auto keyword to define the iterator starting from C++11.

To make the compiler automatically detect the type of the variable you can use the auto keyword. Hence you do not require the lengthy declaration.

You will see the use of the auto keyword in the following section.

Print Map in C++ Using Range-Based for Loop

The range-based for loop makes it easier to traverse a container like a map. It automatically traverses all the items sequentially, therefore, eliminating all the clutter.

You can traverse the map and unordered map in a range-based loop by using the auto variable as well as the custom iterator.

Let us see the code that prints the content of an unordered map using the auto variable and a range-based for loop.

Output:

3 Class
4 Section
1 Name
2 Age

Notice the order of keys in the output. Since we are using the unordered map, the keys are ordered randomly.

Print Map in C++ by Overloading the ostream Insertion Operator (<<)

The ostream_iterator is an output iterator. It can write the content of a container to an output stream.

You can overload the insertion operator (<<) of this class to print the map in C++. This function is defined in the iterator header file.

  • The overloading operator accepts a map of generic type (or the type of your map).
  • It then writes the content of the map to a stream by looping through the map.
    • Note that the code uses a pair to store each data point of the map. This is because of the fact that maps store data as key-value pairs.
  • Finally, it returns the stream. At this point, the stream has all the data of the map written to it.

Let us see the code to print the content of the map using this method.

Note that the code uses the ‘template’ that allows the function to take any generic data type values.

Do not forget the const keyword otherwise the code with throw a compilation error.

Output:

1 Name
2 Age
3 Class
4 Section

Print Map in C++17 Using Structure Binding and Range-Based for Loop

C++17 facilitates using the range-based for loops with structure binding. It means you can further cut the clutter and simply define the data holding variables as the loop variables.

Let us see the code.

Output:

1 Name
2 Age
3 Class
4 Section

Print Map in C++17 Using for_each() Function

The for_each() function applies a pre-defined function to all the data values in the given range. To use this function you must include the algorithm header file in your code.

The definition of the for_each() is given below.

Note that you must define a function that takes a key-value pair and prints it. You can then pass the starting and ending iterators along with your custom function to for_each().

  • The custom function takes a pair from the map as an argument.
  • It prints the first (key) and second (value) data from the pair.

Let us see the code.

Warning: Do not forget the const keyword in the fun function. Omitting it will result in a compilation error.

Output:

1 Name
2 Age
3 Class
4 Section

Conclusion

When it comes to printing the data of a map, the unordered map behaves similarly to the map. Thus you can the method interchangeably for both types of maps.

This is all about how to print Map in C++.

Hope you have enjoyed reading the article. Stay tuned for more such articles. Happy learning!

References

  1. https://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/
  2. https://www.cplusplus.com/reference/algorithm/for_each/

Was this post helpful?

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++ConclusionWas this post helpful? 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, […]

  • 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 ListConclusionWas this post helpful? This article discusses methods of initializing an array in constructor in C++. Let us first discuss arrays […]

  • 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++ConclusionWas this post helpful? The files are used to store the data permanently on a disk. This mitigates […]

  • 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() FunctionConclusionWas this post helpful? An […]

  • 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() FunctionConclusionWas this post helpful? This article discusses the vector and how to print vector in […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.