<algorithm> sort custom condition
Ok, so I tried using sorting for the element vector, so the size of the two additive elements is <= 2d. So here is my attempt:
struct item{
long number;
long size;
};
// d is global variable.
bool check(const item& x, const item& y)
{
return ((x.size + y.size) <= (2 * d));
}
// Items is a vector of item.
sort(items.begin(), items.end(), check);
What am I doing wrong or even impossible to sort using this condition?
+3
3 answers
is it even impossible to sort using such a condition?
No. The comparator sortmust satisfy the criteria of strict weak ordering , which your explicitly does not do (for example, it is not non-reflective).
+5
sort().
STL . "" , :
- (A, B) == false AND A!= B, (B, A) true.
- if check (A, B) == false AND check (B, C) == false AND A, B, C are different, then check (A, C) returns false.
A good idea to use STL sort () is, given your list of S elements and the order you want:
- If the order of elements in S is changed, the order of output should remain unchanged.
- The output is unique.
- All items in the output order have some relation, which is a strict partial order relation .
If so, then you can probably write a validation function to work for you :)
0