基于数据对map std :: map进行排序(map-> vector-> sort)

时间:2013-06-29 08:24:25

标签: c++ sorting map vector

我想根据数据(第二个字段)对std::map进行排序。然而,第二个字段本身是一个结构,我想根据其中一个元素进行排序。根据建议的here及其reference,决定将地图复制到矢量,然后使用std::sort。这是类实现

#include <iostream>
#include <vector>
#include <map>
#include <utility>

class foo() {
    foo() {}
    void bar()
    {
      aDeltaMap theDeltaMap;
      // insert some elements to theDeltaMap
      aDeltaVector theDeltaVec( theDeltaMap.begin(), theDeltaMap.end() );
      std::sort(theDeltaVec.begin(), theDeltaVec.end(), descend_rep<deltaPair>() );   //ERROR
    }
private:
    typedef struct entry {
      entry( int r, int mc ) : rep(r), missCounter(mc) {}
      int rep;
      int missCounter;
    } aDeltaEntry;

    typedef std::map< int, aDeltaEntry > aDeltaMap;
    typedef std::pair< int, aDeltaEntry > deltaPair;
    typedef std::vector< deltaPair > aDeltaVector;
    struct descend_rep
      : std::binary_function<deltaPair,deltaPair,bool>
      {
         inline bool operator()(const deltaPair& lhs, const deltaPair& rhs) { return lhs.second.rep > rhs.second.rep; };
      };
 };

在排序功能行中,我收到此错误

 error C2275: illegal use of this type as an expression
 error C2059: syntax error : ')'

我错过了什么?

1 个答案:

答案 0 :(得分:1)

一个错误是descent_rep不是类模板,因此您需要替换

descend_rep<deltaPair>()

通过

descend_rep()

您也应该descend_rep bool operator() const,因为比较其操作数不会改变其状态。

相关问题