代码之家  ›  专栏  ›  技术社区  ›  Jack BeNimble

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

  •  18
  • Jack BeNimble  · 技术社区  · 16 年前

    这个问题已经有了答案:

    C++将一对从地图复制到向量的最好方法是什么?我这样做是为了随后对向量进行排序。

    7 回复  |  直到 16 年前
        1
  •  20
  •   jmbpiano T.E.D.    9 年前

    这应该是您想要的:

    #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;
    }
    
        2
  •  26
  •   wilhelmtell    16 年前
    vector<pair<K,V> > v(m.begin(), m.end());
    

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

    copy() 是在 <algorithm> .

        3
  •  6
  •   Spire    16 年前

    如果您使用的是std::map,那么它已经按键排序了。只需创建一个迭代器,并从begin()到end()迭代映射,就可以完成了。

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

        4
  •  2
  •   Andrew Shepherd    16 年前

    假设要复制键和值:

    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());
    
        5
  •  2
  •   dirkgently    16 年前

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

    我想两个都复制。完成后,我需要找出如何根据 第二 成对的值。

    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());
    
        6
  •  2
  •   Michael Kohne    16 年前

    如果您的目的只是按类型而不是键进行排序,那么您可能需要查看 Boost::Bimap . 它允许您作为键访问地图对的两个部分。假设您可以按照第二个键的顺序迭代它,就像第一个键一样容易。

        7
  •  0
  •   Richard Corden    16 年前

    您可以使用不同的映射(或集合)并在插入时使用转换进行排序:

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

    我忘了补充一点,如果你需要经常这样做,那么最好只是使用一个助推器。 multi-index 容器。

    推荐文章