PDA

View Full Version : Some problem with deleting tringles using vector iterator



Lee_Jennifer_82
01-11-2013, 04:55 PM
I wrote the following code. I want to delete some triangle from tempContainerOne which is described as a vector of triangles.

I can compilewithout error, but when run the program, it fails. WOuld any body help me identify the problem? Thanks.

Here is the code:

for(TriContainer::iterator cur_b = tempContainerOne->begin(); cur_b != tempContainerOne->end(); cur_b++){

Triangle& tri_2 = (*cur_b);
for(VerticeSet::iterator cur_a = startStripping.begin(); cur_a != startStripping.end(); cur_a++){

if ( (tri_2.vert[0] == (*cur_a)) || (tri_2.vert[1] == (*cur_a)) || (tri_2.vert[2] == (*cur_a)))

tempContainerOne->erase(cur_b);
}
}

tonyo_au
01-11-2013, 08:00 PM
AFAIK the iterator cur_b is invalid after the erase. If this is a std::vector use a for loop with an unsigned int to control it.

Lee_Jennifer_82
01-11-2013, 10:55 PM
If I write like the following it still shows error, Am I using erase function properly:

for(int i = 0; i < tempContainerOne->size(); i++){

Triangle& tri_2 = (*tempContainerOne)[i];
for(VerticeSet::iterator cur_a = startStripping.begin(); cur_a != startStripping.end(); cur_a++){

if ( (tri_2.vert[0] == (*cur_a)) || (tri_2.vert[1] == (*cur_a)) || (tri_2.vert[2] == (*cur_a)))

tempContainerOne->erase((*tempContainerOne)[i]);
}
}

Lee_Jennifer_82
01-12-2013, 01:09 PM
the following works:

for(VerticeSet::iterator cur_a = current_start.begin(); cur_a != current_start.end(); cur_a++){

for (int i = 0; i < tempContainerOne->size();){
Triangle &tri = (*tempContainerOne)[i];

if ( (tri.vert[0] == (*cur_a)) || (tri.vert[1] == (*cur_a)) || (tri.vert[2] == (*cur_a))) {

tempContainerOne->erase(tempContainerOne->begin() + i);
}
else
i++;
}
}

Dark Photon
01-12-2013, 07:40 PM
Please use
... or ... blocks to surround source code. Preserves the indentation and is easier to read.


for(VerticeSet::iterator cur_a = current_start.begin(); cur_a != current_start.end(); cur_a++){

for (int i = 0; i < tempContainerOne->size(){
Triangle &tri = (*tempContainerOne)[i];

if ( (tri.vert[0] == (*cur_a)) || (tri.vert[1] == (*cur_a)) || (tri.vert[2] == (*cur_a))) {

tempContainerOne->erase(tempContainerOne->begin() + i);
}
else
i++;
}
}