Insert iterators

To make it possible to deal with insertion in the same way as writing into an array, special kind of iterator adaptors, called insert iterators, are provided in the library. With regular iterator classes,

while (first != last) *result++ = *first++;
causes a range [first, last) to be copied into a range starting with result. The same code with result being an insert iterator will insert corresponding elements into the container. This device allows all of the copying algorithms in the library to work in the insert mode instead of the regular overwrite mode.

back_insert_iterator Output Iterator
front_insert_iterator Output Iterator
insert_iterator Output Iterator

back_insert_iterator (Container& x) Constructor on back_insert_iterator
* Operator on back_insert_iterator
++ Operator on back_insert_iterator

 

front_insert_iterator (Container& x) Constructor on front_insert_iterator
* Operator on front_insert_iterator
++ Operator on front_insert_iterator

 

insert_iterator (Container& x, Container::iterator i) Constructor on insert_iterator
* Operator on insert_iterator
++ Operator on insert_iterator

back_insert_iterator<Container> back_inserter (Container& x) Function
insert_iterator<Container> inserter (Container& x, Iterator i) Function
front_insert_iterator<Container> front_inserter (Container& x) Function

An insert iterator is constructed from a container and possibly one of its iterators pointing to where insertion takes place if it is neither at the beginning nor at the end of the container. Insert iterators satisfy the requirements of output iterators. operator* returns the insert iterator itself. The assignment operator=(const T& x) is defined on insert iterators to allow writing into them, it inserts x right before where the insert iterator is pointing. In other words, an insert iterator is like a cursor pointing into the container where the insertion takes place. back_insert_iterator inserts elements at the end of a container, front_insert_iterator inserts elements at the beginning of a container, and insert_iterator inserts elements where the iterator points to in a container. back_inserter, front_inserter, and inserter are three functions making the insert iterators out of a container.

template 















class back_insert_iterator : public output_iterator {
protected:
    Container& container;
public:
    back_insert_iterator(Container& x) : container(x) {}
    back_insert_iterator&
        operator=(const Container::value_type& value) {
            container.push_back(value);
            return *this;
    }
    back_insert_iterator& operator*() { return *this; }
    back_insert_iterator& operator++() { return *this; }
    back_insert_iterator& operator++(int) { return *this; }
};

template 







back_insert_iterator back_inserter(Container& x) {
    return back_insert_iterator(x);
}

template 







class front_insert_iterator : public output_iterator {
protected:
    Container& container;

public:
    front_insert_iterator(Container& x) : container(x) {}
    front_insert_iterator&
        operator=(const Container::value_type& value) {
            container.push_front(value);
            return *this;
    }
    front_insert_iterator& operator*() { return *this; }
    front_insert_iterator& operator++() { return *this; }
    front_insert_iterator& operator++(int) { return *this; }
};

template 







front_insert_iterator front_inserter(Container& x) {
    return front_insert_iterator(x);
}

template 







class insert_iterator : public output_iterator {
protected:
    Container& container; Container::iterator iter;

public:
    insert_iterator(Container& x, Container::iterator i)
    : container(x), iter(i) {}
    insert_iterator&
        operator=(const Container::value_type& value) {
            iter = container.insert(iter, value);
            ++iter;
            return *this;
    }
    insert_iterator& operator*() { return *this; }
    insert_iterator& operator++() { return *this; }
    insert_iterator& operator++(int) { return *this; }
};

template 







insert_iterator inserter(Container& x, Iterator i) {
    return insert_iterator(x, Container::iterator(i));
}