Q:

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

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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`.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now