Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
complex
exception
functional
iterator
limits
locale
memory
new
numeric
stdexcept
typeinfo
utility
valarray
iterator
advance
back_inserter
distance
front_inserter
inserter
iterator
iterator_traits
iterator categories:
BidirectionalIterator
ForwardIterator
InputIterator
OutputIterator
RandomAccessIterator
predefined iterators:
back_insert_iterator
front_insert_iterator
insert_iterator
istreambuf_iterator
istream_iterator
ostreambuf_iterator
ostream_iterator
reverse_iterator


front_insert_iterator

class template
<iterator>
template <class Container> class front_insert_iterator;

Front insert iterator

Front insert iterators are a special output iterator class designed to allow algorithms that usually overwrite elements (such as copy) to instead insert new elements at the front of the container.

The container must have member push_front defined (such as standard containers deque and list).

Using the assignment operator on the front_insert_iterator, even when dereferenced, causes the container to insert a new element at its beginning. The other typical operators of an output iterator are also defined for front_insert_iterator but have no effect.

It is defined with an identical operation to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template <class Container>
  class front_insert_iterator :
    public iterator<output_iterator_tag,void,void,void,void>
{
protected:
  Container* container;
public:
  typedef Container container_type;
  explicit front_insert_iterator (Container& x) : container(&x) {}
  front_insert_iterator<Container>& operator= (typename Container::const_reference value)
    { container->push_front(value); return *this; }
  front_insert_iterator<Container>& operator* ()
    { return *this; }
  front_insert_iterator<Container>& operator++ ()
    { return *this; }
  front_insert_iterator<Container> operator++ (int)
    { return *this; }
};


The library provides a function, called front_inserter, that automatically generates a front_insert_iterator class from a container.

Member functions

constructor
front_insert_iterator objects are constructed from a container.
operator=
Inserts a new element in the container, initializing its value to a copy of the argument.
operator*
Does nothing. Returns a reference to the object.
operator++
Does nothing. Returns a reference to the object.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// front_insert_iterator example
#include <iostream>
#include <iterator>
#include <deque>
using namespace std;
int main () {
  deque<int> firstdeque, seconddeque;
  for (int i=1; i<=5; i++)
  { firstdeque.push_back(i); seconddeque.push_back(i*10); }
  front_insert_iterator< deque<int> > front_it (firstdeque);
  copy (seconddeque.begin(),seconddeque.end(),front_it);
  deque<int>::iterator it;
  for ( it = firstdeque.begin(); it!= firstdeque.end(); ++it )
	  cout << *it << " ";
  cout << endl;
  return 0;
}


Output:

50 40 30 20 10 1 2 3 4 5

See also