The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

ambs (22) [Avatar] Offline
#1
It seems that partition and remove_if does the same (almost). While the first puts the 'interesting' elements in th beginning of the list, the second puts the 'interesting' at the end. If I am reading this correctly, this reference might be relevant.

Also, if I am correct, the next question will be: can we use partition to erase?

people.erase(people.begin(), std::partition(people.begin(), people.end(), is_not_female));


vs

people.erase(std::remove_if(people.begin(), people.end(), is_not_female), people.end());


I know this might be just curiosity of a reader (me), but probably other readers will ask the same.
Ivan Cukic (104) [Avatar] Offline
#2
There is an important difference, even if the algorithms do seem similar - `remove_if` leaves those elements that should be removed in an unspecified state, with the requirement they are destructible.

The `partition` algorithm keeps all the values valid. This means that `remove_if` will be generally be faster than `partition` (it can do moves instead of swaps).

It is an interesting question, but I'd say it is a bit out of the scope for the book. Maybe a blog post about that would be beneficial.

ambs (22) [Avatar] Offline
#3
Understood. That 'elements are kept in a unspecified state' was understood as "the order is not relevant" by me.

And sure, some details are better not to be written in the book.
But in some situations (not this one, specifically) you can have two level of reading with some footnotes (for eager readers).

Looking forward for next chapters smilie