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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With