Modern C++ is amazing with many great features. In C++, STL Standard Template Library has many algorithms for some operations like searching, counting, and manipulation of ranges and their elements. In this post you can learn C++ feature called std::map
that you can map pair elements with an appropriate C++ IDE and compiler. One of its powerful modifiers of std::map
is insert()
. In this post we will explain how to use the C++ std::map
insert()
function.
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 found in the C++ Standard Template Library (STL) that stores key-value pairs with unique keys. Each member has a unique key value and a mapped value. Two or more mapped values cannot have the same key values – the key needs to be unique. When using std::map
, key values are sorted by using the comparison function compare. 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>> >; } |
Is there a simple Example of how to use Insert in std::map in Modern C++?
The insert method of std::map
can be used as given in this syntax below,
1 2 3 |
std::pair<iterator, bool> insert( const value_type& value ); |
This allows us to 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 below:
1 2 3 |
std::map< std::string, int > mp; |
if you want, you can directly declare members of this map as below,
1 2 3 |
std::map< std::string, int > mp{ {"Jim", 182}, {"David", 185}, {"Ian", 183} }; |
Is there a complete example of how to use Insert in std::map in Modern C++?
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 23 24 25 |
#include <iostream> #include <map> #include <tchar.h> #include <string> int _tmain(int argc, _TCHAR* argv[]) { std::map< std::string, int > mp; auto i = mp.insert( { "Jim", 182 } ); i = mp.insert( { "David", 185 } ); i = mp.insert( { "Ian", 183 } ); for (const auto& [key, val] : mp) { std::cout << key << "," << val << " | "; } system("pause"); return 0; } |
Here are a few more examples of how to use Insert in std::map in Modern C++
std::pair
can be used inside the insert method as below,
1 2 3 |
i = mp.insert( std::pair{ "Yilmaz", 172 } ); |
you can add multiple pairs as below,
1 2 3 |
mp.insert( { { "Yilmaz", 172 }, { "Ata", 178 } }); |
you can create a new map by using insert as below too,
1 2 3 |
std::map< std::string, int > mp2 = mp.insert( std::begin(mp), std::end(mp) ); |
std:map
is a very strong sorted associative container, it uses iterators like begin()
and end()
, and in addition to insert()
it has other modifiers like clear()
, erase()
, swap()
, etc.
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.