Name

insert_iterator class template — Iterator to insert elements in a container

Synopsis

class insert_iterator :
  public iterator<output_iterator_tag,void,void,void,void>
{
protected:
  Container* container;
  typename Container::iterator iter;
public:
  typedef Container container_type;
  insert_iterator(Container& cont, typename Container::iterator iter);
  insert_iterator<Container>&
    operator=(typename Container::const_reference value);
  insert_iterator<Container>& operator*(  );
  insert_iterator<Container>& operator++(  );
  insert_iterator<Container>& operator++(int);
};

The insert_iterator class template implements an output iterator that stores elements in a container by calling the container’s insert function. The most convenient way to create a insert_iterator object is to use the inserter function template.

The way insert_iterator works seems slightly unconventional, although it is perfectly reasonable for an output iterator: the * operator returns the iterator, not an element of the container. Thus, the expression *iter = value assigns value to the iterator itself. The iterator’s assignment operator adds value to the underlying container by calling the container’s insert function. Thus, the iterator does not maintain any notion of a position, and the increment operator is a no-op.

The following are the member functions of insert_iterator:

insert_iterator (Container& x, typename Container::iterator i)

Initializes the container member with &x and iter with i. Thus, the elements to be inserted ...

Get C++ In a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.