Lake_Lagunita
Lake_Lagunita

Reputation: 553

insert_iterator VS back_insert_iterator

I am now learning C++ STL iterator, and there's the code snippet which teaches back_insert_iterator :

int main(int argc, const char * argv[]) {
vector<int> original;

//vector<int> original = {100, 99, 98}; /* Initially empty */

/* Create a back_insert_iterator that inserts values into myVector. */

back_insert_iterator< vector<int> > itr(original);

for (int i = 0; i < 10; ++i) {
    *itr = i + 1; // "Write" to the back_insert_iterator, appending the value.
    ++itr; }
vector<int> destination;

reverse_copy(original.begin(), original.end(),
             back_insert_iterator< vector<int> >(destination));

reverse_copy(original.begin(), original.end(),
             insert_iterator< vector<int> >(destination));

/* Print the vector contents; this displays 0 1 2 3 4 5 6 7 8 9 */ copy(destination.begin(), destination.end(), ostream_iterator<int>(cout, " "));

cout << endl;

Then I want to change the back_insert_iterator to insert_iterator and see what will happen, so I replaced this line of code:

reverse_copy(original.begin(), original.end(),
             back_insert_iterator< vector<int> >(destination));

with the following code:

reverse_copy(original.begin(), original.end(),
             insert_iterator< vector<int> >(destination));

However, my IDE reported that " no matching conversion for functional-style cast from 'vector' to 'insert_iterator< vector >'

I couldn't understand why this happens as I think that insert_iterator should be a "father-like" iterator of back_insert_iterator, if back_insert_iterator can be applied, then insert_iterator can be applied similarly?

Upvotes: 2

Views: 500

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

Iterator adapter insert_iterator has only the following constructor

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

As you see it has two parameters and the second parameter sets the position in the container where new elements will be inserted.

So you have to write

reverse_copy(original.begin(), original.end(),
             insert_iterator< vector<int> >(destination, destination.end()));

The assignment operator of the iterator adapter

insert_iterator<Container>&
operator=(const typename Container::value_type& value);

performs the following action

iter = container->insert(iter, value);
++iter;

that is it uses container's method insert that in turn has two parameters.

The difference compared with iterators front_insert_iterator and back_insert_iterator is that for these two iterator adapters the position where new elements will be inserted is known while for the iterator adapter insert_iterator you have to specify the position of the insertion yourself.

Upvotes: 4

Lake_Lagunita
Lake_Lagunita

Reputation: 553

The format of insert_iterator should be specifying both the container and the container's begin iterator as parameters as follows: reverse_copy(original.begin(), original.end(), insert_iterator< vector<int> >(destination, destination.begin()));

Upvotes: 0

Related Questions