back_inserter, front_inserter and inserter Iterator Adapters

Line 28 creates the array results2 with the number of elements in a1 and a2. Using the merge algorithm requires that the sequence where the results are stored be at least the size of the sequences being merged. If you do not want to allocate the number of elements for the resulting sequence before the merge operation, you can use the following statements:

vector< int > results2; merge( a1.begin(), a1.end(), a2.begin(), a2.end(),    back_inserter( results2 ) );

The argument back_inserter(results2) uses function template back_inserter (header <iterator>) for the vector results2. A back_inserter calls the container’s default push_back function to insert ...

Get C++ How to Program, Ninth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.