Sunday, January 8, 2012

stl insert iterator

Insert Iterators

The first example of iterator adapters are insert iterators, or inserters. Inserters are used to let algorithms operate in insert mode rather than in overwrite mode. In particular, they solve the problem of algorithms that write to a destination that does not have enough room: They let the destination grow accordingly. Insert iterators redefine their interface internally as follows:
  • If you assign a value to their actual element, they insert that value into the collection to which they belong. Three different insert iterators have different abilities with regard to where the elements are inserted — at the front, at the end, or at a given position.
  • A call to step forward is a no-op.
Example:


This example uses all three predefined insert iterators:

1. Back inserters:
Back inserters insert the elements at the back of their container (appends them) by calling push_back(). For example, with the following statement, all elements of coll1
are appended into coll2:

copy (coll1.begin(), coll1.end(), //source
back_inserter(coll2)); //destination
Of course, back inserters can be used only for containers that provide push_back() as a member function. In the C++ standard library, these containers are vector, deque, and list.

2. Front inserters
Front inserters insert the elements at the front of their container by calling push_front(). For example, with the following statement, all elements of coll1 are inserted into coll3:
copy (coll1.begin(), coll1.end(), //source
front_inserter(coll3)) ; //destination
Note that this kind of insertion reverses the order of the inserted elements. If you insert 1 at the front and then 2 at the front, the 1 is after the 2. Front inserters can be used only for containers that provide push_front() as a member function. In the C++ standard library, these containers are deque and list.

3. General inserters
A general inserter, also called simply an inserter, inserts elements directly in front of the position that is passed as the second argument of its initialization. It calls the insert() member function with the new value and the new position as arguments. Note that all predefined containers have such an insert() member function. This is the only predefined inserter for associative containers.
But wait a moment. I said that you can't specify the position of a new element in an associative container because the positions of the elements depend on their values. The solution is simple: 
For associative containers, the position is taken as a hint to start the search for the correct position. If the position is not correct, however, the timing may be worse than if there was no hint.
Table: lists the functionality of insert iterators


Expression
Kind of Inserter
back_inserter (container)
Appends in the same order by using push_back()
front_inserter (container)
Inserts at the front in reverse order by using push_front()
inserter (container ,pos)
Inserts at pos (in the same order) by using insert()

No comments:

Post a Comment