C++如何将地图复制到向量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/684475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:43:46  来源:igfitidea点击:

C++ how to copy a map to a vector

c++collectionsvectormap

提问by Hyman BeNimble

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.

C++ 中将一对从映射复制到向量的最佳方法是什么?我这样做是为了随后可以对向量进行排序。

回答by wilhelmtell

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>.

copy()<algorithm>.

回答by CTT

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;
}

回答by Spire

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.

如果您使用的是 std::map,则它已按键排序。只需创建一个迭代器并从 begin() 到 end() 遍历地图,就完成了。

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.

如果您想按地图键以外的其他内容排序,您可以使用相同的迭代器,并在遍历地图时将每个元素的副本推送到您的向量上。

回答by Michael Kohne

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.

如果您的目的只是按类型而不是键排序,您可能需要查看Boost::Bimap。它允许您将映射对的两个部分作为键访问。据推测,您可以像第一个键一样轻松地按照第二个键的顺序对其进行迭代。

回答by dirkgently

A mapstores 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?

Amap存储一对——一个键和一个值。您要复制哪个部分?或者,您想将两者都复制到两个不同的vectors 吗?

I want to copy both. Once that's done, I need to figure out how to sort the vector by the secondvalue 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());

回答by Andrew Shepherd

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());

回答by Richard Corden

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-indexcontainer.

我忘了补充一点,如果您需要经常这样做,那么使用 boost多索引容器可能会更好。