Insert iterators come in very handy when you need to populate a container using an STL algorithm which uses an assignment operator to fill in the data:
int iArr [] = {2, 2, 3, 4, 5, 6, 7};
#include <vector>
vector<int> vecInt;
For example, the following statement will fail because
vecInt is not allocated space and the
unique_copy() algorithm uses this assignment operator:
#include <algorithm>
unique_copy (iArr, iArr+7, vecInt.begin());
Conversely, using
back_inserter() as the third argument causes the container to invoke
push_back() instead of the assignment operator.
#include <iterator>
unique_copy (iArr, iArr+7, back_inserter(vecInt));
The other kind of insert iterator is
front_inserter(). Be sure before using these that the underlying container supports the functions they invoke in place of the assignment operator. For instance,
push_front() in place of the assignment operator, and vector does not support
push_front().