C++ Language/Std/Stl/Iterators/InsertIterators

From Wikibooks, open books for an open world
< C++ Language‎ | Std‎ | Stl‎ | Iterators
Jump to navigation Jump to search

Algorithm std::copy() has already been written by STL to call operators = and ++ on both source and destination iterators (overwriting destination items with data copied from source items). As such, that algorithm assumes the destination collection must already have at least as many items as the source collection.

A std::back_insert_iterator<> "insert-iterator" is intermediary code which allows that same implementation of std::copy() to be used with an empty destination collection. That insert-iterator achieves this by re-defining the effect of operators = and ++. Operator = now has the effect of veciDesti.push_back(), and operator ++ now does nothing.

Additional information about insert-iterators (includes interactive examples)