【发布时间】:2016-01-27 12:04:14
【问题描述】:
在代码sort(A.begin(),A.end()); 中,A 定义为vector<pair<int,pair<int,int>>> A;。
如果我调用sort方法,那么排序的依据是什么?
【问题讨论】:
-
会按照
std::pair::operator <进行排序,详情请见here。
在代码sort(A.begin(),A.end()); 中,A 定义为vector<pair<int,pair<int,int>>> A;。
如果我调用sort方法,那么排序的依据是什么?
【问题讨论】:
std::pair::operator <进行排序,详情请见here。
它将使用operator < 与std::pair、specified here 进行比较。
它首先在 first 元素上按字典顺序比较元素,如果它们相等,则在 second 元素上进行比较。
考虑到这里pair<int,pair<int,int>> 的复杂类型,给std::sort 算法一个自定义比较器可能是一个更好的主意。确保提供的函子满足Compare 概念的要求,即严格的弱排序。
【讨论】: