stable_partition algorithm doesn't work correctly !

Jun 11, 2017 at 4:29am
i know that partition algorithm partitions the container into 2 parts by the predicate argument without sorting each part;
but stable_partition sorts each part.
but in this example it doesn't sort the first part correctly and doesn't sort the the whole second part.
1
2
3
4
vector<int> vec = { 9,10,1,2,3,4,7,5,4,6,4,2,8 };
stable_partition(vec.begin(), vec.end(), [](const int i) {
		return i <= 5;
});


the result is : part #1 : 1 2 3 4 5 4 4 2 part #2 : 9 10 7 6 8

do i understand correctly or what ?
Jun 11, 2017 at 4:42am
> stable_partition sorts each part.

No, it does not sort the elements. But it preserves the relative order of the elements.

9, 10, 1, 2, 3, 4, 7, 5, 4, 6, 4, 2, 8

part #1 : 1 2 3 4 5 4 4 2 (relative order is preserved)

part #2 : 9 10 7 6 8 (relative order is preserved)
Jun 11, 2017 at 5:05am
thanks, iam sorry i thought that preserve means sort :) i didn't translate it. lazy :D
Topic archived. No new replies allowed.