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 (94) [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).