Insert elements into a map in C++
This post will discuss how to insert elements into a map in C++.
1. Using std::map::insert
The standard solution to insert new elements into a map is using the std::map::insert function. It inserts the specified key-value pair into the map only if the key already doesn’t exist. If the key already exists in the map, the element is not inserted.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <map> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'} }; map.insert(std::pair<int, char>(4, 'D')); // or map.insert(std::map<int, char>::value_type(5, 'E')); for (const auto &entry: map) { std::cout << "{" << entry.first << ", " << entry.second << "}" << std::endl; } return 0; } |
Output:
{1, A}
{2, B}
{3, C}
{4, D}
{5, E}
This is functionally equivalent to the following code that uses std::make_pair to produce a std::pair<> via template deduction.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <map> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'} }; map.insert(std::make_pair(4, 'D')); for (const auto &entry: map) { std::cout << "{" << entry.first << ", " << entry.second << "}" << std::endl; } return 0; } |
Output:
{1, A}
{2, B}
{3, C}
{4, D}
Or, even shorter, using the list initialization syntax:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <map> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'} }; map.insert({4, 'D'}); for (const auto &entry: map) { std::cout << "{" << entry.first << ", " << entry.second << "}" << std::endl; } return 0; } |
Output:
{1, A}
{2, B}
{3, C}
{4, D}
2. Using operator[]
The std::map::insert function does not insert the specified key-value pair into the map if the key already exists. To overwrite the old value for an existing key, we can use operator[].
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <map> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'} }; map[1] = 'D'; for (const auto &entry: map) { std::cout << "{" << entry.first << ", " << entry.second << "}" << std::endl; } return 0; } |
Output:
{1, D}
{2, B}
{3, C}
With C++17, std::map introduced the std::map::insert_or_assign insertion function. It is similar to operator[], but returns more information and does not require default-constructibility of the mapped type.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <map> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'} }; map.insert_or_assign(1, 'D'); for (const auto &entry: map) { std::cout << "{" << entry.first << ", " << entry.second << "}" << std::endl; } return 0; } |
Output:
{1, D}
{2, B}
{3, C}
3. Using std::map::emplace
The std::map::emplace function inserts a new element into the map container if the key is not already present. It can be used as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <map> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'} }; map.emplace(1, 'D'); for (const auto &entry: map) { std::cout << "{" << entry.first << ", " << entry.second << "}" << std::endl; } return 0; } |
Output:
{1, A}
{2, B}
{3, C}
{4, D}
With C++17, std::map introduced the std::map::try_emplace function. It behaves like std::map::emplace, but does not move from rvalue arguments when no insertion happens (key already exist in the map). For example,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <map> int main() { std::map<int, char> map = { {1, 'A'}, {2, 'B'}, {3, 'C'} }; map.try_emplace(1, 'D'); for (const auto &entry: map) { std::cout << "{" << entry.first << ", " << entry.second << "}" << std::endl; } return 0; } |
Output:
{1, A}
{2, B}
{3, C}
That’s all about inserting elements into a map in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)