To erase specifice element in the Vector

Hi,
Dear all,

On each iteration in Vector , i want to deduce one element and delete it. then pass remaining vector to my function.

so, problem is this. it is not erasing the specific vector
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
typedef std::vector < int > nodeList;
nodeList temp;
nodeList neighborX = this->computeNeighbors(x);

for (nodeList::iterator iter = neighborX.begin(); iter != neighborX.end(); ++iter)
{
int y = *iter;
temp = neighborX;

neighborX.erase(iter); // deduce first variable as y and delete it from the list.

graph = this->computeIndependence(ds, graph, x, y, neighborX, condVariables, sep);

neighborX = temp;

}



But it is giving an error.
1
2
3
4
5

error: program: c:\.....\....\.....\vector line 117

Expression : ("this->_Mycont != NULL",0)



i tried to find an error but i am fail,

if i remove this line then no problem

neighborX.erase(iter);

so please suggest me an other way to remove an element. thanks in advance.
Erasing from STL containers invalidates any iterators associated with them. That's why .erase() always has a return value that is an iterator to the next position after the one you deleted. Your erase line should look like

iter = neighborX.erase(iter);

Question though, why are you using a vector when you do so much erasing of elements not at the end? That's going to be painfully slow.
What do you mean by "deduce"?

When you use deque::erase(), it invalidates iter. If you want it to point to the element following the one erased, you must assign it to the result:
iter = foo.erase( iter );


A very important thing to remember is: don't modify lists (vector/deque/etc) during traversal.
http://www.parashift.com/c++-faq-lite/serialization.html#faq-36.12

Hope this helps.

[edit] Man, get called away for a sec... <g>
Last edited on
But he's making a copy of it, then erasing an element, passing that to a function, then copying back the original vector. It's still pretty shacky.
Thanks dear for your reply,

Actually i want to extract one item on each iteration and then i want to pass the remaining vector (n-1) to my function.

so, for this i first copy orignal vector in a temp.
then extract the specified element.
and then pass it to the function.
after that again assign the orignal values from the temp.

i will be thankful to you if you reply little bit in more detail. your opinion will be helpful for me.

thanks again.
This seems to work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <vector>
using namespace std;

typedef std::vector< int > List;

void show( List& list ) {
    for( List::iterator i = list.begin(); i != list.end(); ++i ) {
        cout << *i << ' ';
    }
    cout << endl;
}

int main() {
    List n;

    for( int i = 1; i <= 5; ++i )
        n.push_back( i );

    for( size_t i = 0; i < n.size(); ++i ) {
        List temp( n );
        temp.erase( temp.begin() + i );
        show( temp );
    }
}

Last edited on
Thanks Hammurabi, it is solved.
Topic archived. No new replies allowed.