在谷歌上搜索了一段时间之后,我找不到具体的答案。如何反向排序带有自定义类型的c++ STL对
pair <long long, pair< int, int > > p[MAX];发布于 2015-07-29 19:01:31
#include<bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 5;
typedif pair <long long, pair< int, int > > pll;
pll p[MAX];
//custom sort function can be customized
bool SortByWeight(pll x, pll y){
return x.first > y.first;
}
int main(){
//enter size
cin>>szie;
//Ex: p = [(10101, (2,3)), (129334, (4, 7))....]
sort(p, p+size, SortByWeight)
}自定义函数可以自定义自定义类型。对于类的ex,我们可以使用class_obj.variable_name等。
发布于 2015-07-29 19:14:34
std::pair自己提供了一个成员级的operator < (以及operator >),所以您需要做的就是让std::sort使用operator >。
std::sort(p, p + MAX, std::greater<pair <long long, pair< int, int > >>());这里有更多的文档:std:成对、std:分类、std::更大。
https://stackoverflow.com/questions/31708988
复制相似问题