VOOZH about

URL: https://www.geeksforgeeks.org/cpp/how-to-convert-vector-of-pairs-to-map-in-cpp/

⇱ How to Convert a Vector of Pairs to a Map in C++? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Convert a Vector of Pairs to a Map in C++?

Last Updated : 23 Jul, 2025

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 Map

The below example demonstrates how we can create a map of vectors and insert the key-value pairs in it in C++.


Output
1 => one
2 => two
3 => three

Time Complexity: O(N logN), where N is the size of vector.
Auxiliary Space: O(N)



Comment