How to Use Iterator Inserters
In our earlier implementation of filter(), we assigned each element of the source container that matches the predicate test into the target container:
while (( first = find_if( first, last, bind2nd( pred, val ))) != last ) *at++ = *first++;
This requires that the target container be large enough to hold each assigned value. filter() has no way of knowing whether, after each increment, at continues to address a valid container slot. It is up to the programmer to ensure that the target container pointed to by at is large enough. In our test program, we ensure that by defining the target container to be the same size as the source container:
int ia[ elem_size ] = { 12, 8, 43, 0, 6, 21, 3, 7 }; vector<int> ivec( ia, ia+elem_size ); int ia2[ elem_size ]; vector<int> ivec2( elem_size );
The problem with this solution is that, in most cases, the target container is too large. An alternative approach is to define an empty container and expand it as needed through element insertion. Unfortunately, filter() is currently implemented to assign into an existing container slot. If we reimplement filter() to do insertion, what happens to our existing programs using the assignment implementation of filter()? Moreover, what sort of insertion should we provide?
The generic algorithms that copy elements, such as copy(), copy_backwards(), remove_copy(), replace_copy(), unique_copy(), and so on, are similar in implementation to filter(). Each is passed an iterator that marks the position within a container to begin copying. With each element copy, the value is assigned and the iterator is incremented. Each copy requires that we guarantee that the target container is of a sufficient size to hold the set of assigned elements. With these algorithms, we don't have the option of reimplementing them.
Does this mean that we must always pass in a fixed-size container for those algorithms? That's hardly in the spirit of the STL. Instead, the standard library provides three insertion adapters. These adapters allow us to override a container's assignment operator.
back_inserter() causes the container's push_back() operator to be invoked in place of the assignment operator. This is the preferred inserter for vectors. The argument to back_inserter is the container:
vector<int> result_vec; unique_copy( ivec.begin(), ivec.end(), back_inserter( result_vec ));
inserter() causes the container's insert() operation to be invoked. inserter() takes two arguments: the container and an iterator into the container indicating the position at which insertion should begin. For a vector, we would write the following:
vector<string> svec_res; unique_copy( svec.begin(), svec.end(), inserter( svec_res, svec_res.end() ));
front_inserter() causes the container's push_front() operator to be invoked. This inserter can be used only with the list and deque containers:
list<int> ilist_clone; copy( ilist.begin(), ilist.end(), front_inserter( ilist_clone ));
To use these adapters, we must include the iterator header file:
#include <iterator>
These adapters, however, cannot be used with a built-in array. The built-in array provides no support for element insertion. Here is a reimplementation of the program making use of a back_inserter for the vector use of filter():
int main() { const int elem_size = 8; int ia[ elem_size ] = { 12, 8, 43, 0, 6, 21, 3, 7 }; vector<int> ivec( ia, ia+elem_size ); // built-in arrays do not support insertion ... int ia2[ elem_size ]; vector<int> ivec2; cout << "filtering integer array for values less than 8\n"; filter( ia, ia+elem_size, ia2, elem_size, less<int>() ); cout << "filtering integer vector for values greater than 8\n"; filter( ivec.begin(), ivec.end(), back_inserter( ivec2 ), elem_size, greater<int>() ); }
filter() assigns each element in turn to the target vectorin this case, ivec2. In this example, we have not initialized ivec2 to an element size, so an assignment would result in a run-time failure. By passing in ivec2 using an inserter adapter, we turn the element assignment into an insertion. Because insertion into a vector is efficient only at the back, we choose to use a back_inserter.