The operator<
is overloaded for pair
so you can sort a vector of pairs just like any other vector. If you need descending order, you have two options - either sort and then call std::reverse
to reverse the result or provide predicate for the sorting.
You can also make use of std::greater
:
#include
#include
#include
using namespace std;
int main() {
vector > a;
a.push_back(make_pair(1, 2));
a.push_back(make_pair(2, 3));
sort(a.begin(), a.end(), greater >());
return 0;
}