Iterator Invalidation? iVec.erase(it)
-
Hi all, I am having runtime crash with code below int main(int argc, char** argv) { vector iVec; iVec.push_back(0); iVec.push_back(10); iVec.push_back(20); iVec.push_back(30); iVec.push_back(40); iVec.push_back(30); iVec.push_back(60); vector::iterator it; for(it=iVec.begin(); it != iVec.end();++it) { if(*it = 30) { iVec.erase(it); } --it; } return 0; } How can I get all "30" out? Thanks!
Yonggoo
-
Hi all, I am having runtime crash with code below int main(int argc, char** argv) { vector iVec; iVec.push_back(0); iVec.push_back(10); iVec.push_back(20); iVec.push_back(30); iVec.push_back(40); iVec.push_back(30); iVec.push_back(60); vector::iterator it; for(it=iVec.begin(); it != iVec.end();++it) { if(*it = 30) { iVec.erase(it); } --it; } return 0; } How can I get all "30" out? Thanks!
Yonggoo
for(it=iVec.begin(); it != iVec.end();++it) { if(*it = 30) // May be this is the problem { iVec.erase(it); } --it; } the above if line is the culprit if you typed it so in your code also it should be if(*it == 30) // corrected
Tanvon the brain behind ... I Blog here
-
Hi all, I am having runtime crash with code below int main(int argc, char** argv) { vector iVec; iVec.push_back(0); iVec.push_back(10); iVec.push_back(20); iVec.push_back(30); iVec.push_back(40); iVec.push_back(30); iVec.push_back(60); vector::iterator it; for(it=iVec.begin(); it != iVec.end();++it) { if(*it = 30) { iVec.erase(it); } --it; } return 0; } How can I get all "30" out? Thanks!
Yonggoo