Topic: Why is mylist.end() valid after mylist.erase(mylist.begin(), mylist.end())?
Author: Ed Howland <ehowland@thegrid.net>
Date: 1998/02/18 Raw View
Probably a simple question: Is the iterator .end() valid after an erase
of the entire list or any sub part that includes the .end()? I don't
have enough of an understading of the erase operator, but this seems to
work:
list<int> mylist;
mylist.insert(mylist.end(), 1); mylist.insert(mylist.end(), 2);
mylist.insert(mylist.end(), 3);
mylist.erase(mylist.begin(), mylist.end());
mylist.insert(mylist.end(), 4);
Or for that matter, what about the .begin() iterator after such an
erase?
Can someone refer me to more complete documentation on the erase()
method and what it means when it says that iterators are invalid?
Thanks
Ed
--
Ed Howland MSPS, ACM Member
ehowland@thegrid.net
Proud to part of the 27% who still disapprove
"Never bet on evil", J.C.
[ 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: Ron Crane <ron@spam-not.srsys.com>
Date: 1998/02/19 Raw View
> Probably a simple question: Is the iterator .end() valid after an erase
> of the entire list or any sub part that includes the .end()?
> ...
> Or for that matter, what about the .begin() iterator after such an
> erase?
begin() and end() are always valid. begin() refers to the element at the
container's beginning, and end() to one position beyond the element at its
end. If the container is empty, begin() == end ().
For more information on begin(), end(), iterators, and the STL in general,
see http://www.sgi.com/Technology/STL.
Peace,
Ron
[ 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: Edward Diener <eddielee@abraxis.com>
Date: 1998/02/19 Raw View
The end() function always points to one past the end of your container. If
you try to access a contained value there you will have a problem but if you
use end() to insert items its perfectly alright since your insertion will
start at that point. In other words you can reference the "pointer" value
there but not the contained object that it points to since it never points to
anything.
Ed Howland wrote:
> Probably a simple question: Is the iterator .end() valid after an erase
> of the entire list or any sub part that includes the .end()? I don't
> have enough of an understading of the erase operator, but this seems to
> work:
>
> list<int> mylist;
>
> mylist.insert(mylist.end(), 1); mylist.insert(mylist.end(), 2);
> mylist.insert(mylist.end(), 3);
>
> mylist.erase(mylist.begin(), mylist.end());
>
> mylist.insert(mylist.end(), 4);
>
> Or for that matter, what about the .begin() iterator after such an
> erase?
>
> Can someone refer me to more complete documentation on the erase()
> method and what it means when it says that iterators are invalid?
>
> Thanks
> Ed
>
> --
> Ed Howland MSPS, ACM Member
> ehowland@thegrid.net
> Proud to part of the 27% who still disapprove
> "Never bet on evil", J.C.
[ 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 ]