std::pairs comparison operators compare pairs lexicographically, it first compares the first elements, then the second elements if the first elements are equal.
Here is an example of using std::vector<std::pair<int, int>> and std::sort.
Using std::sort
that way uses std::pair
's operator <
, which, as said above, compares the pairs lexicographically.
UPDATE: Here is an example using std::stable_sort and a custom comparison function that compares only the first element.
By using std::stable_sort, you are guaranteed that the relative order of equal elements are preserved. That is, even if the first elements of the std::pairs
are equal, the original relative order is still retained.