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);
}
}
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.
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]);
}
}
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++;
}
}
Please use [noparse]
...
or
...
[/noparse] 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++;
}
}