The operator<
is overloaded for pair<int,int>
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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<pair<int, int> > a;
a.push_back(make_pair(1, 2));
a.push_back(make_pair(2, 3));
sort(a.begin(), a.end(), greater<pair<int,int> >());
return 0;
}