3

I've been studying stl for the past two weeks and been dealing with alot of vector<T>, deque<T>, and list<T>. All those times I've been using push_back(), push_front(), insert(). Currently though, I've been introduced to "Insert Iterators" which are the following:

  • back_insert_iterator, which is similar to push_back() and does requires the container to have a push_back() function to work
  • front_insert_iterator, which is similar to push_front() and requires the container to have a push_front()
  • insert_iterator, similar insert() and blah blah blah

So I know how to implement all of this. My question is quite simple, what is the difference? Why bother using the Insert Iterators?

Luc Touraille
  • 79,925
  • 15
  • 92
  • 137

1 Answers1

9

Because you can pass them to algorithms, e.g.

std::copy(v1.begin(), v1.end(), std::back_inserter(v2));
Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
  • somes simple enough, the teacher should've just said it explicitly and instead of being an old implicit mercurial laskasjflasjdf. – Joey Arnold Andres May 10 '12 at 00:24
  • 2
    It's always better to make things clear, yes :) The bottom line is that `back_insert_iterator` is "just" an output iterator that calls `push_back` in its assignment operator (e.g. see http://www.cplusplus.com/reference/std/iterator/back_insert_iterator/). If you're interested, I seem to remember Josuttis' book "The C++ Standard Library" having a more detailed explanation of all of this. – Stuart Golodetz May 10 '12 at 00:29
  • 1
    I will buy that book in my next paycheck :) – Joey Arnold Andres May 10 '12 at 00:30
  • There's a new version just out actually :) – Stuart Golodetz May 10 '12 at 00:31