std::remove question

Hi guys, considering the code below :

1
2
3
4
5
6
7
8
9
10
std::vector<SphereMarker>::const_iterator it = markerList.begin();
while(it != markerList.end()){
    if(std::find(current.begin(), current.end(), *it) != current.end()){
        //point already part of sequence so remove from vector
        markerList.erase(std::remove(markerList.begin(), markerList.end(), *it), markerList.end());
    }
    else{
    //do something else
    }
}


When an element is erased "it" points to the next element. Can someone explain to me where "it" gets it's new value assigned?
Last edited on
The erase operation returns a value. Did you look at the interface for the container function? You need to assign it a new value which is the return value of the erase function.
http://cplusplus.com/reference/stl/vector/erase/

To be honest with you, that is a strange loop. I'm not sure why you have the while loop there. Since I don't know what is in the else path, I am not sure what you are trying to do. Bottom line is that "you" must assign something to "it" if you want it to have a value. That is not going to happen automatically.
I did check the interface of course and I was assigning a value to it. But this line of code increases "it" somehow magically. No idea why either :o
read http://www.cplusplus.com/reference/algorithm/remove/
"it" should still be equal to markerList.begin(). "it" was not increase, but the container modified.
So basically if the first element is erased then it points to the next element without being manually increased.

Thanks for the answers guys.
Topic archived. No new replies allowed.