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 stdmap 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 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17C++20Introduction to C++Learn C++

When Is The CMath Mathematical Special Functions Header Used in Modern C++?

C++C++17C++20Introduction to C++Learn C++

What Are The CMath Mathematical Special Functions in Modern C++?

C++C++17C++20Learn C++

What Are The New Rules For Auto Deduction In C++ 17?

C++C++14C++17Learn C++Syntax

What Are The New Overloads For Ranges In C++ 14?