views:

2256

answers:

7

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.

+1  A: 

A map stores a pair -- a key and a value. Which part do you want to copy? Or, do you want to copy both to two distinct vectors?

I want to copy both. Once that's done, 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());
dirkgently
I want to copy both. Once that's done, I need to figure out how to sort the vector by the *second* value in the pair.
Jack BeNimble
j_random_hacker
+4  A: 

If you're using a std::map, it's already sorted by the key. Just create an iterator and iterate over the map from begin() to end() and you're done.

If you'd like to sort by something other than the map key, you can use the same iterator and push a copy of each element onto your vector as you iterate over the map.

Spire
May be even simpler: You can specify a comparator in a map's CTor.
mxp
+10  A: 
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>.

wilhelmtell
This one seems to be more elegant and simple
Ram
+1  A: 

Assuming you want to copy the key and the 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());
Andrew Shepherd
That last one is incorrect. Maybe you mean v.insert(v.rbegin().base(), m.begin(), m.end()); ?
wilhelmtell
whihelmtell - Why do you think it's incorrect? I just tried it - it works fine.
Andrew Shepherd
Come to think of it: v.rebegin().base() returns the same iterator as v.end()
Andrew Shepherd
It should be OK if v.size() >= 1. The range is inserted from the element _before_ the position iterator.
Functastic
It's OK even if the vector is empty. In this case v.insert(v.end(), m_begin(). m.end()) is identical tov.insert(v.begin(), m_begin(). m.end()), because v.begin() == v.end().
Andrew Shepherd
I think I misread that last line. Ignore my comment above. :)
wilhelmtell
Thank you for optimal syntax snippets (for example the v.assign line).
+5  A: 

This should do what you want:

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>

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;
}
CTT
+2  A: 

If your purpose is just to sort by the type instead of the key, you might want to look at Boost::Bimap. It lets you access both parts of the map pair as keys. Presumably you could iterate over it in order of the second key just as easily as the first.

Michael Kohne
A: 

You can use a different map (or set) and use transform to do the sorting as you 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 might be better just to use a boost multi-index container.

Richard Corden