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.
Table of Contents
What is a container in modern C++?
Containers are modern data storage arrays in modern C++ and they are very useful to iterate and search data with their amazing methods and properties.
A container is a holder object that stores data elements (a collection of data objects). They are implemented as a class template to define objects that can be used with modern rules of C++ (The rule of 6), they allow great flexibility in the different data types supported as elements, they can be used with int, float, double, etc. or with struct types, they can be used with other modern types of C++, lambdas and templates. Thus, the developer can create different data sets in memory, these can be static or dynamic, they are safe and optimized well.
What are the basic container types modern C++?
The C++ Standards library defines 4 container types.
- Sequence Containers ( vector, array, deque, list, forward_list )
- Associative Containers ( map, multimap, set, multiset )
- Unordered Associative Containers ( unordered_map, unordered_multimap, unordered_set, unordered_multiset )
- Container Adapters ( stack, queue, priority_queue )
If you want to learn more about these each type, here are some basic details,
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);
1 2 3 4 5 6 7 8 9 |
namespace pmr { template< class Key, class T, class Compare = std::less<Key> > using map = std::map< Key, T, Compare, std::pmr::polymorphic_allocator<std::pair<const Key, T>> >; } |
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.
1 2 3 |
std::map< std::string, int > mp; |
If you want, you can directly declare members of this map as shown below.
1 2 3 |
std::map< std::string, int > mp{ {"Jim", 182}, {"David", 185}, {"Ian", 183} }; |
Or you can declare in the lines of program, here is a full example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <map> #include <string> int main() { std::map< std::string, int > mp; mp["Jim"] = 182; mp["David"] = 185; mp["Ian"] = 183; for (const auto& [key, val] : mp) { std::cout << key << "," << val << " | "; } system("pause"); return 0; } |
and the output will be as follows,
1 2 3 |
David,185 | Ian,183 | Jim,182 | Press any key to continue . . . |
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:
1 2 3 |
std::map< int, std::string > mp; |
If you want, you can directly declare members of this map as shown below:
1 2 3 |
std::map< std::string, int > mp{ {182, "Jim"}, {185, "David"}, {183, "Ian"} }; |
Or you can declare in the lines of program, here is a full example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <map> #include <string> int main() { std::map<int, std::string > mp; mp[182] = "Jim"; mp[185] = "David"; mp[183] = "Ian"; for (const auto& [key, val] : mp) { std::cout << key << "," << val << " | "; } system("pause"); return 0; } |
and the output will be as follows,
1 2 3 |
182,Jim | 183,Ian | 185,David | Press any key to continue . . . |
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()
.
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.