Sorting a pair of vectors by the difference of paired elements

Is there any way in C ++ that sorts me into vector pairs based on the difference in the values ​​of the pair. As an example, suppose I have 4 pairs

1 3, 
5 6, 
2 3,
12 5,

therefore the differences in pairs are 2 1 1 7 if I sort in descending order. The sorted vector will be,

12 5,
1 3,
5 6,
2 3,

I hope you understand what my problem is. Is there a way to sort items this way?

I tried this way of sorting elements based on the first or second element. But that is not my problem. My problem is that I need sorting based on difference.

bool sortinrev(const pair<int,int> &a, const pair<int,int> &b){
    return(a.first > b.first) ;
}


int main()
{
    vector< pair <int,int> > pq;
    for(int i=1; i<=4; i++){
        int x,y;
        cin >> x >> y;

        pq.push_back(make_pair(x,y));
    }

    sort(pq.begin(), pq.end(), sortinrev);

    for(int i=0; i<4; i++){
        cout << pq[i].first << " " << pq[i].second << endl;
    }


    return 0;
}
+4
source share
3 answers

If your container

std::vector<std::pair<int, int>> data;

you can sort it like

std::sort(std::begin(data),
          std::end(data),
          [](std::pair<int, int> const& lhs, std::pair<int, int> const& rhs)
          {
              return std::abs(lhs.first - lhs.second) < std::abs(rhs.first - rhs.second);
          });

, < > .

+8

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}
    };

    // sort using a custom function object
    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)
+2

std:: sort , .

template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );

, lambda ( ) , tcompares , .

cppreference.com:

comp     -   comparison function object (i.e. an object that satisfies the     requirements of Compare) which returns ​true if the first argument is less than (i.e. is ordered before) the second. 
The signature of the comparison function should be equivalent to the following:

bool cmp(const Type1 &a, const Type2 &b);

The signature does not need to have const &, but the function object must not modify the objects passed to it.
The types Type1 and Type2 must be such that an object of type RandomIt can be dereferenced and then implicitly converted to both of them. ​

,

sort(begin(myvect), end(myvect), [](auto p) { /* comparison code */ });

( ++ 14, , )

+1
source

Source: https://habr.com/ru/post/1683691/


All Articles