是std :: pair< int,std :: string>订购明确? [英] Is std::pair<int, std::string> ordering well-defined?
本文介绍了是std :: pair< int,std :: string>订购明确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
看来我可以排序std :: vector< std :: pair< int,std :: string>>,它会根据int值排序。这是一个明确的事情吗? std :: pair是否有基于它的元素的默认排序?
It seems that I can sort an std::vector<std::pair<int, std::string>>, and it will sort based on the int value. Is this a well defined thing to do? Does std::pair have a default ordering based on it's elements?
推荐答案
std :: pair
使用词典比较:它将基于第一个元素进行比较。如果第一个元素的值相等,它将根据第二个元素进行比较。
std::pair
uses lexicographic comparison: It will compare based on the first element. If the values of the first elements are equal, it will then compare based on the second element.
C ++ 03标准中的定义(第20.2.2节)是:
The definition in the C++03 standard (section 20.2.2) is:
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).
这篇关于是std :: pair< int,std :: string>订购明确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文