0
votes

I have some problem with sorting my vectors.I want to sort my vectors acording to first element wchich is Number.Can someone explain me what I doing wrong and what this 2 errors means?

I try sort this with compare function and without it and nothing works.

struct Point{
    int x;
    int y;
};

bool compare(int a,int b){
     return a < b;
}

int main()
{
int N,Number,x,y;

cin >> N;
vector<pair<int,Point>> p;
for(int i = 0 ; i < N ; i++){
    cin >> Number >> x >> y;
    pair<int,Point> pom = {Number,{x,y}};
    p.push_back(pom);
    }
//    sort(p.begin(),p.end());
//    sort(p.begin().p.end(),compare);
return 0;
}

I've got two errors but I don't know what means:
1.no match for 'operator<' (operand types are 'const Point' and 'const Point')
|| (!(__y.first < __x.first) && __x.second < __y.second); }
2.body of constexpr function 'constexpr bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) [with _T1 = int; _T2 = Point]' not a return-statement
|| (!(__y.first < __x.first) && __x.second < __y.second); } ^ ^

1
You don't need a comparator for ints, this is already provided in the language. What compiler doesn't know is how to compare two instances of Point class. Or a comparator for std::pair<int, Point>, if you would prefer to use it only in the std::sort function (rather than providing an operator<(Point, Point) for everyone to use)Yksisarvinen
Points do not implement comparison operators since there is no definition of how to compare Points.Strom
Also, you probably want your compare operator to return an int, not a boolean. It's not an Equals operation or a less Than operation. It's < 0 if a < b, == 0 if a==b, and > 0 if a > b.Joseph Larson
@JosephLarson If this function is to be used in std::sort, then it is expected to return true if element a should go before element b, false otherwise.Yksisarvinen
Ah. I should have checked.Joseph Larson

1 Answers

2
votes

Comparator for ints is useless and probably Undefined Behaviour. Compiler knows how to compare two integers, it's part of the language. It also knows how to compare two instances of std::pair. What it doesn't know, is how to compare two instances of Point class, which is defined by you.

Basically, you have two options:

1. Provide operator< for Point

This will let you compare two points easily in any situation later on:

bool operator<(const Point& p1, const Point& p2) {
    if (p1.x == p2.x) 
    {
        return p1.y < p2.y;
    }
    return p1.x < p2.x;
}    

2. Provide an in-place comparator for std::sort This is a quick solution if you only need to compare stuff for sorting purposes.

std::sort(p.begin(), p.end(), [](const auto& p1, const auto& p2) {
    //whatever logic it takes to compare two std::pair<int, Point>
});

Note: Generic lambda (with const auto& as arguments) is a C++14 feature. Lambdas themselves are C++11 feature.

Choice between above depends on the usage. If you just need to sort a vector, of if the logic for sorting is unusual, go for std::sort comparator. If you want to always compare two Points in the same way, go for operator overloading.