C++C++17Learn C++

This Is How To Use std::map In Modern C++

This Is How To Use stdmap In Modern C++

Modern C++ is amazingly powerful with many great features to help you write programs more easily with fewer errors. In C++, the Standard Template Library or STL has many algorithms for operations like searching, counting, and manipulation of ranges and their elements. In this Learn C++ post we show you a feature called std::map that you can use to pair elements.

What is std::map in Modern C++?

std::map is a sorted associative container in the C++ Standard Template Library (STL) that stores key-value pairs with unique keys matched to a value. Each member of the container has a unique key value and a mapped value. Two mapped values cannot have the same key values. When using std::map, key values are sorted by using the comparison function compare.

Here is the syntax for the std::map (Since C++17);

How to use std::map in Modern C++? Example 1

Let’s assume we have string and integer values in our map. If you want to make string values key values that will be sorted, you can use std::map as shown below.

If you want, you can directly declare members of this map as shown below.

Or you can declare in the lines of program, here is a full example:

and the output will be as follows,

How to use std::map in Modern C++? Example 2

Now, if you want to make integer values as key values that will be sorted, you can use std::map in the following way:

If you want, you can directly declare members of this map as shown below:

Or you can declare in the lines of program, here is a full example:

and the output will be as follows,

The std:map function is a very strong sorted associative container. It uses iterators like begin() and end(), it has modifiers like clear(), insert(), erase(), swap().

This Is How To Use std::map In Modern C++ - the C++ Builder Logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Learn C++Templates

Learn How To Eliminate Pointers In Templates in C++

C++C++11C++14C++17Learn C++Templates

Learn To Export And Import Template Members From A DLL Or A Package In C++

C++C++11C++14C++17Learn C++Templates

What Is A Function Template In C++?

C++C++11C++14C++17Learn C++Templates

What Is A Class Template In C++?