Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between partition() and remove() functions in C++

Tags:

c++

What is the difference between the partition() and remove() functions in C++?

The remove doesn't actually remove any elements of the containers but puts the 'removed' elements at the beginning of the sequence of elements and partition does the same thing as well.

like image 210
David Willis Avatar asked Aug 19 '10 11:08

David Willis


1 Answers

remove [...] puts the 'removed' elements at the beginning of the sequence

What? No. Both remove_if and partition put the "good" elements first. partition puts the "bad" elements after that, whereas remove_if does not specify what comes after it -- it might be the bad elements, but it might also be copies of any (either good or bad) elements.

For example, if you partition 1 2 3 4 5 on even, you might get 2 4 5 3 1 (note that each element occurs exactly once), whereas if you remove_if the odd elements, you might get 2 4 3 4 5 (note the duplicates).

like image 157
fredoverflow Avatar answered Sep 23 '22 02:09

fredoverflow