Re: erase function in vector
Jun wrote:
Hello,
I've a vector of integer 4 4 4 5 9
suppose I deleted 5, i got 4 4 4 9
and sort by decreasing number ordering : 9 4 4 4
but when i output *(vector.end()), it aways show 9
But vector.end() still works when during loop.
v.end() does not point to a valid object, so looking at what it points
to gives unpredictable results.
vector<int> v;
// initialize
vector<int>::iterator ir = v.begin();
while(ir != v.end()){
if(*ir == 5)
ir = v.erase(ir);
else
++ir;
}
cout << *v.end() << endl;
sort(v.begin(),v.end(), sortbySize);
cout << *v.end() << endl;
std::copy(v.begin(),v.end(), ostream_iterator<int>(cout, "\t"));
The iterator and the value which it's represented is not much ... ?
very strange ...
Not strange at all. v.end() is a past-the-end iterator. It doesn't point
to a valid object. Don't dereference it.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of
"The Standard C++ Library Extensions: a Tutorial and Reference"
(www.petebecker.com/tr1book)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]