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