S
subramanian100in
Suppose I have
vector<int> c;
for (int i = 0; i < 10; ++i)
c.push_back(i);
vector<int>::iterator it = find(c.begin(), c.end(), 5);
If I do,
c.insert(c.begin(), 10);
then it can cause increase in container size in which case
reallocation happens and so the insertion operation can POTENTIALLY
invalidate all iterators into the vector.
How to know whether all the iterators into a vector are invalidated
after insertion ? Or should we safely always assume that all iterators
into a vector will become invalid after insertion ?
Now consider
c.erase(it--);
Can reallocation happen due to the shrinkage of vector size by means
of erasure ? If this is true, then erase also can invalidate all
iterators into a vector. Am I correct ?
Also, is it correct to say 'it--' in the argument passed (ie postfix
decrement on the iterator argument passed) to erase function as done
above ? Will it be a valid iterator after erase ?
Or equivalently, should I say,
vector<int>::iterator nit = c.erase(it);
--nit;
Kindly clarify
Thanks
V.Subramanian
vector<int> c;
for (int i = 0; i < 10; ++i)
c.push_back(i);
vector<int>::iterator it = find(c.begin(), c.end(), 5);
If I do,
c.insert(c.begin(), 10);
then it can cause increase in container size in which case
reallocation happens and so the insertion operation can POTENTIALLY
invalidate all iterators into the vector.
How to know whether all the iterators into a vector are invalidated
after insertion ? Or should we safely always assume that all iterators
into a vector will become invalid after insertion ?
Now consider
c.erase(it--);
Can reallocation happen due to the shrinkage of vector size by means
of erasure ? If this is true, then erase also can invalidate all
iterators into a vector. Am I correct ?
Also, is it correct to say 'it--' in the argument passed (ie postfix
decrement on the iterator argument passed) to erase function as done
above ? Will it be a valid iterator after erase ?
Or equivalently, should I say,
vector<int>::iterator nit = c.erase(it);
--nit;
Kindly clarify
Thanks
V.Subramanian