std::pair std::sort, , . . , , std::pair<int,int> " ", , std::sort . , .
#include <iostream>
#include <vector>
int main()
{
std::vector<std::pair<int,int> > v = {
{1, 3},
{5, 6},
{2, 3},
{12, 5}
};
struct {
bool operator()(const std::pair<int,int> &a, const std::pair<int,int> &b) const
{
return ( abs(a.first-a.second) > abs(b.first-b.second));
}
} differenceIsGreater;
std::sort(v.begin(), v.end(), differenceIsGreater);
for (auto a : v) {
std::cout << "(" << a.first << "," << a.second << ")" << std::endl;
}
return 0;
}
:
(12,5)
(1,3)
(5,6)
(2,3)