jeudi 31 mars 2016

Delete elements from a vector in C++11 while iterating over it [duplicate]

This question already has an answer here:

My applications requires to iterate over a vector and delete certain elements which doesnt satisfy the requirements. Which is the most correct way? I believe the following way is incorrect.Reason: I am getting segmentation fault.

std::vector<ObjectX> vec1;
//Fill in vec1
std::vector<ObjectX>::iterator itVec1 = vec1.begin();

for(;itVec1 != vec1.end(); ++itVec1) {
   if (Oracle(*itVec1)) vec1.erase(itVec1);
}

Aucun commentaire:

Enregistrer un commentaire