Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Some problem with deleting tringles using vector iterator

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    18

    Some problem with deleting tringles using vector iterator

    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);
    }
    }

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    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.

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    18
    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]);
    }
    }

  4. #4
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    18
    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++;
    }
    }

  5. #5
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,183
    Please use [code]...[/code] or [highlight=cpp]...[/highlight] blocks to surround source code. Preserves the indentation and is easier to read.

    Code cpp:
    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++;
          }    
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •