It seems that I can sort a 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 its elements?
It seems that I can sort a 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 its elements?
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.
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).