Topic: iterators invalidated by vector::erase
Author: James Kuyper <kuyper@wizard.net>
Date: 1998/05/05 Raw View
Hankel O'Fung wrote:
...
> 2) Sequence points and prefix/postfix ++/-- are also being discussed in the
> thread
> Re: foo++ vs. ++foo (was Re: var::x)
> in this newsgroup. But I can't find the term "sequence point" in ARM,
> Stroustrup or Lippman. Is it in CD2 or FDIS? Are there any books or online
> resources containing detailed discussions on this topic?
See section 1.8 of CD2 for an explanation of sequence points,
particularly sections 17-19.
> 3) Concerning my question in the original post: who is correct? Musser wrote:
>
> "The erase function ... leaves VALID those referring to
> preceding positions or TO THE ERASED POSITION"
> but Plauger wrote:
> "iterators and references become INVALID only FROM
> THE FIRST ELEMENT erased through the end of the sequence."
CD2 section 23.2.4.3 specifies that vector<T>::erase()
| Invalidates all the iterators and references after the point of the
| erase.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Bradd W. Szonye" <bradds@concentric.net>
Date: 1998/04/29 Raw View
Hankel O'Fung wrote:
>
> "iterators and references become invalid only from the first element
> erased through the end of the sequence",
>
> meaning that j would become invalid after such an erasure as
> vector1.erase(j). But then why would vector1.erase(j--) work if j is
> decremented after the erasure?
The iterator 'j' should be decremented before the actual erasure.
Calling a function is a sequence point; therefore, the side-effects of
'j--' (ie., decrementing the iterator) should be complete before the
call of vector::erase().
--
Bradd W. Szonye
bradds@concentric.net
http://www.concentric.net/~Bradds
My reply address is correct as-is. The courtesy of providing a correct
reply address is more important to me than time spent deleting spam.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Hankel O'Fung" <hkfung@ust.hk>
Date: 1998/05/01 Raw View
Bradd W. Szonye wrote:
> Hankel O'Fung wrote:
> >
> > meaning that j would become invalid after such an erasure as
> > vector1.erase(j). But then why would vector1.erase(j--) work if j is
> > decremented after the erasure?
>
> The iterator 'j' should be decremented before the actual erasure.
> Calling a function is a sequence point; therefore, the side-effects of
> 'j--' (ie., decrementing the iterator) should be complete before the
> call of vector::erase()
Thanks, Bradd, but I'm still not sure if I fully understand what you mean or
not, coz I'm not that smart :-(
1) Now it seems that, NO MATTER the erase function will invalidate an
iterator j referring to a to-be-erased item (say, vector1[10]) or not, the
statement
vector1.erase(j--);
will (i) erase vector1[10], (ii) cause j to point to vector1[9], and (iii)
perhaps invalidate all other iterators originally pointing to v[10] or
beyond, am I correct?
2) Sequence points and prefix/postfix ++/-- are also being discussed in the
thread
Re: foo++ vs. ++foo (was Re: var::x)
in this newsgroup. But I can't find the term "sequence point" in ARM,
Stroustrup or Lippman. Is it in CD2 or FDIS? Are there any books or online
resources containing detailed discussions on this topic?
3) Concerning my question in the original post: who is correct? Musser wrote:
"The erase function ... leaves VALID those referring to
preceding positions or TO THE ERASED POSITION"
but Plauger wrote:
"iterators and references become INVALID only FROM
THE FIRST ELEMENT erased through the end of the sequence."
Regards,
Hankel
--
IEEM Dept, HKUST, Clear Water Bay, Kowloon, Hong Kong
voice (852) 2358 7103 fax (852) 2358 0062
http://home.ust.hk/~hkfung
"For as long as I can remember, I've been searching for some reason
why we're here -- what are we doing here, who are we? If this is a
chance to find out even just a little part of that answer, I think
it's worth a human life, don't you?" - Ellie Arroway
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Hankel O'Fung" <hkfung@ust.hk>
Date: 1998/04/28 Raw View
Dear all,
The following is abridged from Musser's STL Tutorial and Reference
Guide, First Printing, p.133, where j is a vector<char>::iterator :
"The erase function ... leaves valid those referring to preceding
positions or to the erased position. That's why the line
vector1.erase(j--);
works correctly, but the similar code
vector1.erase(j++);
wouldn't work, because j would be invalid after the erasure."
However, in Plauger's Dinkum C/C++ Library Reference
(http://www.dinkumware.com/htm_cpl/index.html), it reads, under the item
vector::erase,
"iterators and references become invalid only from the first element
erased through the end of the sequence",
meaning that j would become invalid after such an erasure as
vector1.erase(j). But then why would vector1.erase(j--) work if j is
decremented after the erasure?
Is there any descrepancy between Musser's and Plauger's description? If
yes, then who's right? Thanks in advance ;-)
Hankel
--
IEEM Dept, HKUST, Clear Water Bay, Kowloon, Hong Kong
voice (852) 2358 7103 fax (852) 2358 0062
http://home.ust.hk/~hkfung
"For as long as I can remember, I've been searching for some reason
why we're here -- what are we doing here, who are we? If this is a
chance to find out even just a little part of that answer, I think
it's worth a human life, don't you?" - Ellie Arroway
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]