bool custome_compare(const pair<int, int>& p1, const pair<int, int>& p2){
if (p1.first > p1.second || p1.second > p1.first) return true;
else return false;
}
int main()
{
pair<int, int> arr[4];
arr[0].first = 4, arr[0].second = 10;
arr[1].first = 7, arr[1].second = 6;
arr[2].first = 3, arr[2].second = 8;
arr[3].first = 9, arr[3].second = 1;
sort(arr, arr + 4 , custome_compare);
//---------------------------------------
return 0;
}
我的目标是根据较大的值对数组进行排序。
我不在乎更大的值是对中的第一个或第二个元素。
例如我有这对:
4,10
7,6
3,8
9,1
排序后:
4,10
9,1
3,8
7,6
所以我不是基于第一个或第二个排序我基于两者排序。
如何编辑此比较功能来完成此任务?
提前致谢。
听起来您想比较两对的最大值。
bool custom_compare(const pair<int, int>& p1, const pair<int, int>& p2){
return std::max(p1.first, p1.second) < std::max(p2.first, p2.second);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句