Explain how the loop from page 345 that used the return from insert to add elements to a list would work if we inserted into a vector instead
belongs to book: C++ Primer|Stanley B.Lippman, Josee Lajoie, Barbara E.Moo|5th Edition| Chapter number:9| Question number:21
All Answers
total answers (1)
The member function
`insert(p, t)`
has the same effect on both`list`
and`vector`
containers, but the cost is different. Inserting an element into`list`
is cheap, while inserting an element into`vector`
will cause all the elements after the newly inserted element be moved. Thus every time the`while`
loop body is excuted, all elements in the`vector`
are moved backward, and new element is inserted in the front of the`vector`
.