At the end of the following code, is x
always equal to 0
? Or is it an example of undefined behaviour ?
unsigned long int x = 0;
--x;
++x;
Note: C++98 context. For some (good) reason, I can't use C++11.
Why this question ?
I have to go through a container, index by index, removing some elements. I would like to implement it with the following:
MyContainerWithoutIterators c;
//...
for(unsigned long int i = 0; i < c.size(); ++i)
{
if(i_have_to_remove(c[i]))
{
c.removeAtIndex(i);
--i; // counterbalance incrementation of the loop
}
}
Note: I know I can do the following, I just want to know if I can use the previous implementation.
MyContainerWithoutIterators c;
//...
for(unsigned long int i = 0; i < c.size();)
{
if(i_have_to_remove(c[i]))
c.removeAtIndex(i);
else
++i;
}