Re: using copy with list<string> and list<cstring*>

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Igor Tandetnik (itandetnik_at_mvps.org)
Date: 08/03/04


Date: Tue, 3 Aug 2004 13:05:27 -0400


"Jason Winnebeck" <gillius@mail.rit.nspam.edu> wrote in message
news:upwKfKXeEHA.592@TK2MSFTNGP11.phx.gbl
>> With vector, it's probably safest to keep indexes into the master
>> vector. Pointers and iterators into the vector are easily
>> invalidated by just about any modifictations. Indexes are more
>> stable and predictable.
>
> Indexes invalidate at exactly the same time that all iterators
> invalidate. Pointers invalidate at exactly the same time that
> iterators invalidate for vectors. So technically they are all
> equally bad. But in some algorithms it will work fine, where you are
> ensured that the vector cannot change.

Indexes do not get invalidated, they still refer to something, in a
predictable way. You can adjust them to bring them back into order. E.g.
if you delete an element from the vector, you can subtract 1 from all
indexes referring to the elements beyond the deleted one. But if you
store pointers or iterators, you cannot adjust them - you need to throw
them away and repopulate with new ones. In some algorithms, adjustments
of indexes is more efficient than essentially regenerating your view
from scratch every time.

Of course if the vector is read-only, all three work equally well.

-- 
With best wishes,
    Igor Tandetnik
"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


Relevant Pages

  • Re: using copy with list<string> and list<cstring*>
    ... Instead of copying the strings I think he wants to store ... Pointers and iterators into the vector are easily invalidated by ... Indexes invalidate at exactly the same time that all iterators ...
    (microsoft.public.vc.stl)
  • Re: Storing a tree in a std::list<>
    ... >>invalidate any iterators to elements of the list. ... > Hmmm, references too. ... But yes, technically not pointers. ...
    (comp.lang.cpp)
  • Re: pointer to an elment in a stl vector
    ... it is blessed by the standard. ... >> If the vector increases in size could it invalidate that pointer? ... > This will invalidate all iterators and pointers derived from ... And of course also pointers not derived from iterators. ...
    (comp.lang.cpp)
  • Re: Effective STL Item 4 (size() vs. empty())
    ... I intentionally ignored the unequal allocators case. ... splice is a closely related issue. ... to be able to keep iterators to specific items. ... > container as an argument to a library function shall not invalidate iterators ...
    (comp.lang.cpp)
  • Re: std::vector : begin, end and insert - Using Objects instead of ints
    ... vector< CPoint> vec; ... lists, which only have iterators. ... "overloaded pointers", and it is better to say that iterators are "an ... iterator approach to a loop. ...
    (microsoft.public.vc.mfc)