Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ how to copy a map to a vector [duplicate]

Tags:

What's the best way in C++ to copy a pair from a map to vector? I'm doing this so I can subsequently sort the vector.

like image 772
Jack BeNimble Avatar asked Mar 26 '09 04:03

Jack BeNimble


People also ask

Can C++ copy maps?

Starting with C++17, we can use the std::map::merge member function to copy content from a map to another map. This is the recommended way to copy the contents of a map to an existing map in C++17 and above. That's all about copying entries of a map to another map in C++.

How do you copy vector vectors?

You just use its copy constructor or assignment operator: std::vector<std::vector<int> > vec; std::vector<std::vector<int> > copy_of_vec = vec; Yes, it's really that simple once you get rid of all the pointers.


1 Answers

vector<pair<K,V> > v(m.begin(), m.end());

or

vector<pair<K,V> > v(m.size());
copy(m.begin(), m.end(), v.begin());

copy() is in <algorithm>.

like image 149
wilhelmtell Avatar answered Sep 24 '22 18:09

wilhelmtell