Re: exception when using vector
redf0x napsal:
Hello,everyone:
I have contacted a really unreasonable vector exception when I
used its method erase,is there anything wrong int the following code?
/*sou and dest are both int vector*/
for(vector<int>::iterator bpiter = sou.begin(); bpiter
!= sou.end();
)
{
if((*bpiter) == 5)
(*bpiter) = 3;
des.push_back(*bpiter);
sou.erase(bpiter);
}
platform VS2005
My best regards
When you destroy vector's item via iterator bpiter, iterator becomes
invalid. You should either always get the first (or last) item of
vector (inside of cycle) or (better) walk through all items of vector
and then erase all it's items in one single step.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]