![]() |
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"}, {3,"three"}} Convert Map to a Vector of PairsTo convert a map to a vector of pairs, we can use the std::copy method provided by the C++ Standard Template Library (STL) to copy the elements from the map to a vector of pairs. We can use the back_inserter() function to make sure that the elements are inserted at the end of the vector. C++ Program to Convert Map to Vector of Pairs Using std::copy MethodC++
Output
Vector Elements: (1, a ) (2, b ) (3, c ) Time complexity: O(N), to traverse the whole map and copy values to vector using the copy method. |
Reffered: https://www.geeksforgeeks.org
C++ |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |