quertion about std::sort cmp function

Apr 23, 2022 at 7:57am
When I define a vector<int> contains one hundred 0s and sort with the code below, an exception "invalid comparator" throws.

1
2
3
4
5
vector<int> v(100, 0);
sort(v.begin(), v.end(), [](const int& a, const int& b)
	{
		return a >= b;
	});


Howerver, if I use " return a > b ", it will execute well.
Thanks very much if some could help me.
Last edited on Apr 23, 2022 at 8:00am
Apr 23, 2022 at 8:07am
The comparator should return true if the first argument should be ordered before the second argument otherwise it should return false. That means equal elements (and comparing an element with itself) should return false which is why >= is wrong.
Last edited on Apr 23, 2022 at 8:10am
Apr 23, 2022 at 8:22am
Thanks a lot. Your answer really benefits me.
Topic archived. No new replies allowed.