Home > Software engineering >  Sorting a vector of pairs first element increasing, second element decreasing
Sorting a vector of pairs first element increasing, second element decreasing

Time:03-03

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
#define pb push_back
#define ll long long
int main(void)
{
int n, q, w;
cin>>n;
vector< pair <int,int> > a;
for (int i = 0; i < n; i  )
{
    cin>>q>>w;
    a.push_back( make_pair(q,w) );
}
sort(a.begin(), a.end());
for (int i = 0; i < n; i  )
{
    cout<<a[i].first<<" "<<a[i].second<<endl;
}

return 0;
}

I need the vector to be sorted first element increasing and if there are multiple first elements with the same size the second element should be sorted decreasing.

Here's some examples which I have tried (also what output I want):

Input: 7
       1 1
       1 9
       1 3
       5 4
       3 2
       8 8
       5 1

Program output: 1 1
                1 3 
                1 9
                3 2
                5 1
                5 4
                8 8

Output I need: 1 9
               1 3
               1 1
               3 2
               5 4
               5 1
               8 8

CodePudding user response:

std::sort can be passed a comparator to be used to compare elements. Using std::tie is a convenient way of comparing tuples:

std::sort(a.begin(), a.end(),[](const auto& a,const auto& b){
    return std::tie(a.first,b.second) < std::tie(b.first,a.second);
});

Note how the second elements of the pairs are swapped to get them in decreasing order.

CodePudding user response:

Basically, you need to customize the comparator function of the sortfunction.
This can be implemented with a lambda function.

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>


int main() {
    int n, q, w;
    std::cin >> n;
    std::vector<std::pair<int,int>> a;
    for (int i = 0; i < n; i  ) {
        std::cin >> q >> w;
        a.push_back ({q, w});
    }
    auto fsort = [] (const std::pair<int,int>& a, const std::pair<int,int>& b) {
        if (a.first != b.first) return a.first < b.first;
        return a.second > b.second;
    };
    std::sort(a.begin(), a.end(), fsort);
    for (int i = 0; i < n; i  ) {
        std::cout << a[i].first << " " << a[i].second << std::endl;
    }
    return 0;
}

CodePudding user response:

You need to create a separate function in which it compares first elements of two pairs if they aren't equal then it will return which is smaller or else it will return which one is greater.

Like this: TRY IT ONLINE

#include <iostream>
#include <vector>
#include <algorithm>

bool pair_sorting(const std::pair<int, int>& p1, const std::pair<int, int> &p2)
{
    return (p1.first != p2.first) ?  p1.first < p2.first : p1.second > p2.second;
}

void sort_pairs(std::vector<std::pair<int, int>> &vec)
{
    std::sort(vec.begin(), vec.end(), pair_sorting);
}

int main(void)
{
    int n, q, w;
    std::cin >> n;
    std::vector<std::pair<int, int>> a;
    for (std::size_t i = 0; i < n; i  )
    {
        std::cin >> q >> w;
        a.push_back(std::make_pair(q, w));
    }
    sort_pairs(a);
    std::cout << "Sorted:\n";

    for (std::size_t i = 0; i < a.size(); i  )
    {
        std::cout << a[i].first << " " << a[i].second << "\n";
    }
    return 0;
}
  • Related