sequential container insert() function

  • Thread starter subramanian100in
  • Start date
S

subramanian100in

Suppose I have a vector<T> or deque<T> for some type T.
Suppose vector<T>::iterator iter is an iterator.

Then consider the operation
vector<T>::iterator nit = container.insert(iter, T());

After the insertion, iter will be invalidated for both vector<T>,
deque<T>.

However for list<T>, the same operation under g++ 3.4.3, does not seem
to invalidate iter. From this, can it be concluded that list<T> does
not invalidate iterators after insert operation ? What does the
standard say regarding this ?

Kindly clarify.

Thanks
V.Subramanian
 
J

James Kanze

Suppose I have a vector<T> or deque<T> for some type T.
Suppose vector<T>::iterator iter is an iterator.
Then consider the operation
vector<T>::iterator nit = container.insert(iter, T());
After the insertion, iter will be invalidated for both vector<T>,
deque<T>.
However for list<T>, the same operation under g++ 3.4.3, does
not seem to invalidate iter. From this, can it be concluded
that list<T> does not invalidate iterators after insert
operation ? What does the standard say regarding this ?

That some operations invalidate iterators, and others don't, and
that which operations invalidate which iterators depends on the
container. Each container has its own set of rules.

In the case of insert: insert never invalidates iterators in
std::list, nor any of the associative containers. In
std::vector, it always invalidates iterators after the element
inserted, and will invalidate all iterators if the capacity is
increased. In std::deque, it invalidates all iterators.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top