简体   繁体   中英

Overloading custom sort comparison function in C++

I want to sort pairs in a vector of pairs according to a certain standards so i overloaded a sort function with a function named sortPair , but I don't know what is the appropriate parameters that I should pass to the function .

#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;

bool sortPair (pair < int , int > &x , pair < int , int > &y)
{
    if ( x.second % 2 == 0 && y.second % 2 == 0 && x.first == y.first )
    {
        if ( x.second > y.second )
        {
            return y.second < x.second ;
        }
        else
        {
            return x.second < y.second ;
        }
    }

    if ( x.second % 2 != 0 && y.second != 0 && x.first == y.first )
    {
        if( x.second > y.second )
        {
            return y.second < x.second ;
        }
        else
        {
            return x.second < y.second ;
        }
    }

    if ( x.second % 2 == 0 && y.second != 0 && x.first == y.first )
    {
        return y.second < x.second ;
    }

    if ( x.second % 2 != 0 && y.second == 0 && x.first == y.first)
    {
        return x.second < y.second ;
    }
}

int main()
{
    int t = 1;
    while ( t -- )
    {
        int n , m , x;
        cin>> n >> m ;
        vector < pair < int , int > > u ;

        for ( int i=0 ; i<n ; i++)
        {
            u.push_back(make_pair(x%m,x));

        }

        for ( int i=0 ; i<n ; i++)
        {

            sort(u.begin(),u.end(),sortPair(u.at(i),u.at(i+1)));

        }

        cout<<endl;
        cout<<n<<" "<<m<<endl;

        for (auto& e : u)
        {
            cout << e.first << "\n";
        }
    }

    return 0;
}

Ideone link

Its hard to understand what you want to achieve, but take a look at this function:

#include <algorithm>
#include <functional>
#include <array>
#include <iostream>

int main()
{
    std::array<int, 10> s = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; 

    // sort using the default operator<
    std::sort(s.begin(), s.end());
    for (int a : s) {
        std::cout << a << " ";
    }   
    std::cout << '\n';

    // sort using a standard library compare function object
    std::sort(s.begin(), s.end(), std::greater<int>());
    for (int a : s) {
        std::cout << a << " ";
    }   
    std::cout << '\n';

    // sort using a custom function object
    struct {
        bool operator()(int a, int b)
        {   
            return a < b;
        }   
    } customLess;
    std::sort(s.begin(), s.end(), customLess);
    for (int a : s) {
        std::cout << a << " ";
    }   
    std::cout << '\n';

    // sort using a lambda expression 
    std::sort(s.begin(), s.end(), [](int a, int b) {
        return b < a;   
    });
    for (int a : s) {
        std::cout << a << " ";
    } 
    std::cout << '\n';
}

Those are the basic usage of custom compare function in std::sort.

It looks wrong:

sort(u.begin(),u.end(),sortPair(u.at(i),u.at(i+1)));

You dont have to pass these two variables manually. The usage should be:

sort(u.begin(),u.end(),sortPair));

Moreover you are missing the const& in your compare function.

Change the function signature to take const& .

bool sortPair (pair < int , int > const& x , pair < int , int > const& y) 

and then use it as:

sort(u.begin(), u.end(), sortPair);

Having said that, I noticed that sortPair does not have a return statement before the end of the function. That leads to undefined behavior. Make sure you add

return true;

or

return false;

I couldn't tell which is appropriate in your function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM