How to Add Multiple Key-Value Pairs to a Map in C++? Last Updated : 05 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a map is a container that stores elements in key-value pairs, where each key is unique and associated with a value. In this article, we will learn how to add multiple key-value pairs to a map in C++. Example: Input: myMap = { {1, "one"} }; Elements to be insert = { {2, "two"}, {3, "three"}} Output: myMap = {{1, "one"}, {2, "two"}, {3, "three"}}Map Multiple Key-Value Pairs in C++To map multiple key-value pairs to a std::map in C++, we can use the std::map::insert function that inserts key-value pairs into the map. The insert function can take a pair, a range of pairs, or an initializer list of pairs. Here, we will insert the range of pairs defined in the initializer list. C++ Program to Map Multiple Key-Value Pairs C++ // C++ Program to illustrate how to insert multiple key // value pairs in a map #include <iostream> #include <map> using namespace std; int main() { // Creating a map map<int, string> myMap = { { 1, "one" } }; // Adding multiple key-value pairs to the map myMap.insert({ { 2, "two" }, { 3, "three" } }); // Printing the map after adding the key-value pairs cout << "Map after adding multiple key-value pairs:" << endl; for (const auto& pair : myMap) { cout << pair.first << " => " << pair.second << endl; } return 0; } OutputMap after adding multiple key-value pairs: 1 => one 2 => two 3 => three Time Complexity: O(M logN), where M is the number of new pairs and N is the size of the map.Auxiliary Space: O(M) Comment More infoAdvertise with us Next Article How to Add Multiple Key-Value Pairs to a Map in C++? S skaftafh Follow Improve Article Tags : C++ Programs C++ STL cpp-map CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Delete Multiple Key-Value Pairs from a Map in C++? In C++, a map container stores the collection of data in the form of a key and a value pair and this data is sorted on the basis of the key. In this article, we will learn how to delete multiple key-value pairs from a map in C++ STL. Example: Input: myMap = {{âappleâ, 1}, {âbananaâ, 2}, {âcherryâ, 3 2 min read How To Delete Multiple Key-Value Pairs From Multimap in C++? In C++, a multimap is a container that stores elements where each element has a key value and a mapped value. Unlike maps, multimaps allow multiple key-value pairs with the same key. In this article, we will learn how to delete multiple key-value pairs from a multimap in C++. Example Input: myMultim 2 min read How To Insert Multiple Key-Value Pairs Into a Multimap in C++? In C++, a multimap is similar to a map that stores the data in the key-value format and it also allows us to store duplicate keys for the same value. In this article, we will learn how to insert multiple Key-Value pairs efficiently into a multimap. Example: Input: multi_map = {{"Manas","Singing" }, 2 min read How to Replace a Key-Value Pair in a Map in C++? A map in C++ is a part of the Standard Template Library (STL) that allows the user to store data in key-value pairs where each key in a map must be unique. In this article, we will learn how to replace a specific key-value pair in a Map in C++. Example: Input : map<int, string> mp={{1,"John"}, 2 min read How to Remove a Key-Value Pair from Map in C++? In C++, a map is an associative container that stores elements in key-value pairs, where the key values are unique. In this article, we will learn how to remove a specific key-value pair from a map in C++. Example: Input : myMap = {{1, 10}, {3, 30}, {2, 20}, {4, 40}};key = 2Output :Map After Removal 2 min read Like