![]() |
VOOZH | about |
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”}
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.
The below example demonstrates how we can create a map of vectors and insert the key-value pairs in it in C++.
1 => one 2 => two 3 => three
Time Complexity: O(N logN), where N is the size of vector.
Auxiliary Space: O(N)