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.

382989 (1) [Avatar] Offline
The text explaining Listing 2.9 states

"we know that the number of names is the same as the number of females, so we can immediately create the names vector to be of the needed size to store the result and use names.begin() as the destination iterator instead of relying on std::back_inserter. This way, we will remove the potential memory reallocations needed for vector resizing."

However, using this method calls the default constructor to string n times. And then on transform calls the copy (or perhaps the move) constructor another n times.

If we would simply default construct the vector and then call reserve we avoid the first set of n constructor calls and we can just rely on back_inserter not having to do any dynamic memory allocations because reserve was called.
Ivan Cukic (104) [Avatar] Offline
I agree. I wanted to keep things a bit simpler - the main purpose was that for transform, we know the number of results, but you are right, .reserve would probably be faster (I'd have to benchmark to verify though).