I am having a strange issue when using the erase() function on a std:vector. I use the following code:
int count = 0;
for (int itr=0; itr<b.size(); ++itr) {
if (b[count].notEmpty = false) {
b.erase(b.begin()+count);
--count;
}
++count;
}
However, for some reason there are no elements actually getting erased from b. b is declared elsewhere as follows:
vector<block_data> b;
Where block_data is a structure, and contains the boolean value notEmpty. Some of b’s elements are properly being assigned with notEmpty = false earlier in the code, so I am not sure why they aren’t getting erased. Is it an error with the syntax, or something else?
There’s nothing wrong with your use of
erase. The problem is an assignment inside theifcondition:This sets
b[count].notEmptytofalse, then returnsfalse. This will cause the inner-body of the if-statement to never run.Change it to
or event
And you should be good to go.