Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question on invalidation of iterators on vector<T>

Reply
Thread Tools

question on invalidation of iterators on vector<T>

 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      08-19-2008
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
 
Reply With Quote
 
 
 
 
joseph cook
Guest
Posts: n/a
 
      08-19-2008
On Aug 19, 3:22*am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote:
> Suppose I have


> 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 ?


This is generally a good idea to assume such, but if vector.size()
does not exceed the vector.capacity() before you did the insert, then
the iterator will still be valid. (You can ensure the capacity() is
at least a certain size, by calling vector.reserve(size) up front
somewhee.

>
> 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 ?


No, erase will only invalidate all iterators after 'it'.

>
> 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 ?


Assuming "it" didn't point to begin()

> Or equivalently, should I say,
>
> vector<int>::iterator nit = c.erase(it);
> --nit;


Yes, this is better, although be sure "it" doesn't point to begin here
too. Why do you want to point to the element just before the one that
was deleted? Generally, you just want "nit = c.erase(it)" to get you
to the next element. Id you are trying to walk through a vector in
reverse order, consider a reverse_iterator

Joe Cook
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
vector modifiers and invalidation of iterators subramanian100in@yahoo.com, India C++ 8 07-27-2010 04:37 PM
invalidation of iterators on deque<T>:insert() subramanian100in@yahoo.com, India C++ 2 09-30-2008 11:13 AM
invalidation of iterators on deque subramanian100in@yahoo.com, India C++ 0 05-20-2008 05:59 AM
std::list<>::splice invalidation of iterators Ben Pfaff C++ 2 02-01-2008 03:03 AM
Object in list invalidation question matt.elkins@gmail.com C++ 1 01-25-2008 07:32 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57