Article View: comp.std.c++
Article #2536Modifying STL containers via an iterator
From: Thomas Maeder
Date: Mon, 13 Nov 1995 07:43
Date: Mon, 13 Nov 1995 07:43
42 lines
1510 bytes
1510 bytes
> Does the STL define the case where a container is modified while
> iterating through it?
Depends on the type of container.
> Example: (forgive me if the syntax isn't perfect...)
>
> class X x;
>
> for (X::iterator ix=x.begin(); ix != x.end(); ix++) {
> ix.erase();
> }
>
I quote from the documentation to the free HP implementation (somebody
please correct if this doesn't match the draft):
vector:
erase invalidates all the iterators and references after the point of
the erase.
Thus your code won't work for vectors since ix always references the
element after the point of the erase.
list:
erase invalidates only the iterators and references to the erased
elements.
Your code works.
deque:
erase [and pop] invalidate all the iterators and refrecnces to the
deque.
Your code doesn't work.
Nothing is said for the associative containers, though.
Thomas
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<9511122227.AA22118@eniac.glue.ch>Path:
arc1-ca.pugleaf.net!tcsi.tcs.com!agate!agate!MattReferences:
<47l8qe$9fk@bcarh8ab.bnr.ca>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!tcsi.tcs.com!agate!agate!Matt From: maeder@glue.CH (Thomas Maeder) Newsgroups: comp.std.c++ Subject: Modifying STL containers via an iterator Date: 13 Nov 1995 07:43:34 GMT Organization: <none> Approved: Matt@Austern Message-ID: <9511122227.AA22118@eniac.glue.ch> References: <47l8qe$9fk@bcarh8ab.bnr.ca> NNTP-Posting-Host: austern.hip.berkeley.edu