[英]Sorting std::map based on the data (map->vector->sort)
我想根據數據(第二個字段)對一個std::map
進行排序。 但是,第二個字段本身是一個結構,我想根據其元素之一進行排序。 根據此處建議的內容及其參考 ,決定將地圖復制到向量中,然后使用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 : ')'
我錯過了什么?
一個錯誤是descent_rep
不是類模板,因此您需要替換
descend_rep<deltaPair>()
通過
descend_rep()
您也應該使descend_rep
的bool operator()
const
,因為比較其操作數不會改變其狀態。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.