Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't C++ take care of iterators when insertion is done in a vector after capacity is reached?

I wrote this small code just to see how an iterator actually gets invalidated and does not point to changed location of a vector after its capacity is reached.

Here the size of vector and capacity is both 5 initially . After that I inserted a few other elements in vector and does not re-initialize my iterator to point to myvector.begin(). This lead to a junk value of 49 in my output after maximum size of vector is : 1073741823 when printing elements of vector again.

My question is why C++ does not make point iterator again to a valid myvector.begin() after all elements are copied into new location?
This can also lead to some behavior which can be hard to debug. I know a safe way to work would be to always reinitialize the iterator just before using it.

  #include<iostream>
  #include<vector>
  #include<stdio.h>

  using namespace std;

  int main()
  {
    vector<int> myvector;
    vector<int>::iterator it;
    int myarray[]= {100,200,300,400};
    myvector.insert(it,500);
    it=myvector.begin();
    myvector.insert(it,myarray,myarray+4);
    it=myvector.begin();
    for(;it!=myvector.end();++it)
    cout <<*it<<endl;
    cout <<"size of vector is :" << myvector.size() <<"\n"; 
    cout <<"capacity of vector is : " << myvector.capacity()<<"\n";  
    cout <<"maximum size of vector is : " << myvector.max_size()<<"\n"; 
    myvector.push_back(600);
    for(;it!=myvector.end();++it)
    cout <<*it<<endl;
  }
  Output of program :-
  100
  200
  300
  400
  500
  size of vector is :5
  capacity of vector is : 5
  maximum size of vector is : 1073741823
  49
  100
  200
  300
  400
  500
  600
like image 367
Invictus Avatar asked Apr 30 '12 17:04

Invictus


People also ask

What happens to iterator after insert?

For std::vector , all iterators are invalidated after calling insert if it causes the vector's size() to exceed its capacity() (i.e. it must reallocate). Inserting into a vector will change where the end is...

Why are iterators invalidated?

An Iterator becomes invalidate when the container it points to changes its shape internally i.e. move elements from one location to another and the initial iterator still points to old invalid location. Iterator invalidation in vector happens when, An element is inserted to vector at any location.

What invalidates an iterator?

When the container to which an Iterator points changes shape internally, i.e. when elements are moved from one position to another, and the initial iterator still points to the old invalid location, then it is called Iterator invalidation. One should be careful while using iterators in C++.

Can we use iterator in vector?

Use an iteratorAn iterator can be generated to traverse through a vector. vector<int>::iterator iter; An iterator is used as a pointer to iterate through a sequence such as a string or vector . The pointer can then be incremented to access the next element in the sequence.


2 Answers

Because it's impractical, and probably impossible.

Is the vector supposed to keep a list of all its iterators and them modify all of them as soon as an invalide-triggering method is called?

like image 143
Luchian Grigore Avatar answered Nov 07 '22 04:11

Luchian Grigore


The iterator is not tied to the vector in any meaningful way (how could it be implemented as a pointer if it were?). The vector doesn't know about the iterator. It is your job to not use an invalid iterator.

So you propose adding a ton of complexity to the vector class for... what purpose exactly? How does this solve problems in the real world where we know that doing such a thing is a bad idea?

like image 21
Ed S. Avatar answered Nov 07 '22 03:11

Ed S.