How to Initialize a Map with a Range of Key-Value Pairs in C++? Last Updated : 29 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a map is an associative container that stores elements formed by combining a key and a value. In this article, we will learn how to initialize a map with a range of key-value pairs in C++. Initializing a Map with a Range of Key-Value Pairs in C++To initialize a std::map with a range of key-value pairs, we can use the range constructor of std::map. This constructor takes two iterators, the beginning and the end of a range, and copies the elements in that range into the map. Syntax:std::map<string, string> New_Map(arr.begin(), arr.end());where, arr is the container where key-value pairs are storedNew_Map is the map declaredC++ Program to Initialize a Map with a Range of Key-Value Pairs C++ // C++ Program to illustrate how to initialize a map with a // range of key-value pairs #include <iostream> #include <map> #include <vector> using namespace std; // Driver Code int main() { // Creating an vector of pairs vector<pair<string, int> > arr = { { "apple", 10 }, { "banana", 2 }, { "cherry", 3 } }; // Initializing a map with a range of key-value pairs map<string, int> myMap(arr.begin(), arr.end()); // Displaying the map elements for (auto it = myMap.begin(); it != myMap.end(); ++it) { cout << it->first << " " << it->second << endl; } return 0; } Outputapple 10 banana 2 cherry 3 Time Complexity: O(N log N), where N is the number of elements to be inserted.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Initialize a Map with a Range of Key-Value Pairs in C++? D denzirop9v Follow Improve Article Tags : C++ Programs C++ STL cpp-map CPP Examples +1 More Practice Tags : CPPSTL Similar Reads 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 swap Keys with Values of a Map in C++? Given a map, the task is to swap the Keys of this map with its values, in C++. Examples: Input: map = {'e', 1 }, {'o', 1 }, {'r', 3 }, Output: {1, 'e' }, {1, 'o' }, {3, 'r' }, Method 1: By using vector pair, traverse through the given map push_back the swapped values in the vector pair sort the vect 4 min read How to Initialize Multimap with Default Values in C++? In C++, a multimap is a container provided by the STL library that stores key-value pairs in an ordered manner. Unlike a map, a multimap allows multiple values to be associated with a single key. In this article, we will learn how to initialize a multimap with default values in C++. Initialize Multi 2 min read How to Add Multiple Key-Value Pairs to a Map in C++? 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"}} 2 min read How to Update Value of a Key in Map in C++? In C++, a map is a data structure that stores key-value pairs. In this article, we will learn how to change the value of a key that is already present in the map container in C++. Example Input: map<int, string> mp = {(1, "One"), (2,"Two")} Output: map after Updation: {(1, "One"), (2, "Three") 2 min read Like