似乎我可以对a进行排序std::vector<std::pair<int, std::string>>,std::pair它将根据int值进行排序.这是一个定义明确的事情吗?
是否std::vector<std::pair<int, std::string>>有基于它的元素的默认排序?
int*_*jay 54
std::pair使用词典比较:它将根据第一个元素进行比较.如果第一个元素的值相等,则它将基于第二个元素进行比较.
C++ 03标准(第20.2.2节)中的定义是:
template <class T1, class T2>
bool operator<(const pair<T1, T2>& x, const pair<T1, T2>& y);
Returns: x.first < y.first || (!(y.first < x.first) && x.second < y.second).
Run Code Online (Sandbox Code Playgroud)
根据我的C++ 0x标准副本,第20.3.3.26节,std::pair已operator<定义为对于两对x和y,它返回
x.first < y.first || (!(y.first < x.first) && x.second < y.second)
Run Code Online (Sandbox Code Playgroud)
我不确定这是否也是2003标准的一部分.我还应该注意,如果元素本身不是LessThanComparable,则不会编译.