The basic concept of std::map

The basic concept of the std::map template class

  • It stores key-value pair (like a lookup table)
  • An iterator in the map refers to the type pair, which is another template class in STL.
Alex has an age of 23
Billy has an age of 28
Cyril has an age of 27
David has an age of 32
Eason has an age of 0

.find() method

it returns an iterator that points to the element found or the end of the map if it is not found.

Eason is found.

Custom Object as Map Values

Consider the following class and main function.

Key Sorting in Maps

The map will always sort the values in the order of keys. Consider the following code and result to see the order of inserting elements and printing order during iteration.

8: Billy has an age of 28 
17: Alex has an age of 23
23: Cyril has an age of 27


I have mentioned some basic methods and how to use the std::map in some general ways. In the part where I describe a parameterless constructor, it actually involves concept like shallow copy which I will further talk about it before going to talk about unordered_map or multimap.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kevin Cheung

Kevin Cheung

𝑾𝒉𝒂𝒕 𝑰 𝒄𝒂𝒏𝒏𝒐𝒕 𝒄𝒓𝒆𝒂𝒕𝒆, 𝑰 𝒅𝒐 𝒏𝒐𝒕 𝒖𝒏𝒅𝒆𝒓𝒔𝒕𝒂𝒏𝒅. . An ordinary developer records his life and learning progress.