C ++ how to copy map to vector

What is the best way in C ++ to copy a pair from a map to a vector? I do this to subsequently sort the vector.

+17
c ++ collections vector map
Mar 26 '09 at 4:00
source share
7 answers

This should do what you want:

#include <iostream> #include <vector> #include <map> #include <algorithm> #include <iterator> using namespace std; bool cmp(const pair<int, int> &p1, const pair<int, int> &p2) { return p1.second < p2.second; } int main() { map<int, int> m; for(int i = 0; i < 10; ++i) m[i] = i * -i; vector<pair<int, int> > v; copy(m.begin(), m.end(), back_inserter(v)); sort(v.begin(), v.end(), cmp); for(int i = 0; i < v.size(); ++i) cout << v[i].first << " : " << v[i].second << endl; return 0; } 
+19
Mar 26 '09 at 4:31
source share
 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> .

+25
Mar 26 '09 at 4:12
source share

If you use std :: map, it is already sorted by key. Just create an iterator and repeat on the map from the beginning () to the end (), and everything will be ready.

If you want to sort something other than a map key, you can use the same iterator and click a copy of each element on your own vector when you iterate over the map.

+6
Mar 26 '09 at 4:05
source share

A map stores a pair - key and value. Which part do you want to copy? Or do you want to copy both two different vector s?

I want to copy both. After that I need to figure out how to sort the vector by the second value in the pair.

 template <class V> struct sort_by_val { bool operator()(V const& l, V const& r) { return // ... } }; vector<pair<K, V> > outv(map.begin(), map.end()); sort(outv.begin(), outv.end(), sort_by_val()); 
+2
Mar 26 '09 at 4:02
source share

Assuming you want to copy the key and value:

 std::map<Foo, Bar> m; // Map gets populated // (...) // Copying it to a new vector via the constructor std::vector<std::pair<Foo, Bar>> v(m.begin(), m.end()); // Copying it to an existing vector, erasing the contents v.assign(m.begin(), m.end()); // Copying it to the back of an existing vector v.insert(v.end(), m.begin(), m.end()); 
+2
Mar 26 '09 at 4:12
source share

If your goal is to sort by type instead of key, you can look at Boost :: Bimap . It allows you to access both parts of a pair of cards in the form of keys. Presumably, you could iterate over it in the order of the second key as easily as the first.

+2
Mar 26 '09 at 10:48
source share

You can use another card (or install) and use the transform to sort on insert:

 #include <map> #include <algorithm> typedef std::map<unsigned int, signed char> MapType1; typedef std::map<MapType1::mapped_type, MapType1::key_type> MapType2; struct SwapPair { MapType2::value_type operator()(MapType1::value_type const & v) { return std::make_pair (v.second, v.first); } }; int main () { MapType1 m1; for(int i = 0; i < 10; ++i) m1[i] = i * -i; MapType2 m2; std::transform (m1.begin () , m1.end () , std::inserter (m2, m2.end ()) , SwapPair ()); } 

I forgot to add that if you need to do this a lot, then it would be better to just use boost multi-index container.

0
Mar 26 '09 at 11:02
source share



All Articles