How to Convert a Vector of Pairs to a Map in C++? Last Updated : 29 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a vector of pairs can be converted to a map. This can be useful when you want to create a map from a vector of pairs, where each pair contains a key and a value. In this article, we will learn how to convert a vector of pairs to a map in C++. For Example, Input:myVector = {{1, “one”}, {2, “two”}, {3, “three”}};Output:Map is : {1: “one”, 2 : “two”, 3 : “three”}Vector of Pairs to Map in C++To convert a std::vector of std::pair to std::map, we can use the range constructor of std::map that takes two iterators, one pointing to the beginning and the other to the end of the vector. C++ Program to Convert a Vector of Pairs to a MapThe below example demonstrates how we can create a map of vectors and insert the key-value pairs in it in C++. C++ // C++ Program to illustrate how to Create a Map of Vectors // Using Vector #include <iostream> #include <map> #include <vector> using namespace std; int main() { // Creating a vector of pairs vector<pair<int, string> > vec = { { 1, "one" }, { 2, "two" }, { 3, "three" } }; // Converting the vector of pairs to a map map<int, string> myMap(vec.begin(), vec.end()); // Printing the map for (auto& pair : myMap) { cout << pair.first << " => " << pair.second << endl; } return 0; } Output1 => one 2 => two 3 => three Time Complexity: O(N logN), where N is the size of vector.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Convert a Vector of Pairs to a Map in C++? R rohitpant4532 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector cpp-map cpp-pair CPP Examples +3 More Practice Tags : CPPSTL Similar Reads How to Convert Map to a Vector of Pairs in C++? In C++, map containers allow us to store the data in key-value pairs. There might be some cases where you want to convert an entire map into a vector of pairs. In this article, we will learn how to convert a map into a vector of pairs. Example Input: map<int,string>mp ={ {1,"one"}, {2,"two"}, 2 min read How to Convert a Map of Set of Pairs in C++? In C++, a map can be converted to a set of pairs. This can be useful when you want to create a set of pairs from a map, where each pair contains a key and a value. In this article, we will learn how to convert a map to a set of pairs in C++. Example Input: myMap = {{1, âoneâ}, {2, âtwoâ}, {3, âthree 2 min read How to convert a Vector to Set in C++ This article shows how to convert a vector to a set in C++. Example: Input: vector = {1, 2, 3, 1, 1} Output: set = {1, 2, 3} Input: vector = {1, 2, 3, 3, 5}Output: set = {1, 2, 3, 5} Below are the various ways to do the required conversion: Method 1: Naive SolutionGet the vector to be converted.Crea 4 min read How to Create a Vector of Pairs in C++? In C++, std::pair is the data type that stores the data as keys and values. On the other hand, std::vector is an STL container that stores the collection of data of similar type in the contiguous memory location. In this article, we will learn how to combine these two to create a vector of pairs in 5 min read How to Create a Vector of Vectors of Pairs in C++? In C++ STL, we can nest different containers into each other at any number of levels. On such container is the Vector of Vectors of Pairs i.e. 2D vector of pairs. In this article, we will learn how to create and traverse a vector of vector of pairs in C++ STL. Vector of Vectors of Pairs in C++ The 2 3 min read Like