Question

for (int i = 0 ; i < stlVector.size() ; i++)
{ 
    if (i == 10)
    { 
        stlVector.erase(stlVector.begin() + 5 )
    }
}

Does the termination condition part "stlVector.size()" take "stlVector.erase(...)" into consideration? In other word does stlVector.size() refresh for every loop iteration? I can't test it right now, so i posted a question here.

Thx in advance!

Best regards,

zhengtonic

Was it helpful?

Solution

Just to be clear, don't think of it in terms of the loop refreshing anything. Every time the condition is checked (at the start of each time through the loop), the size() method is called on the stlVector variable, and the current size of the vector is returned.

The erase() method reduces the size of the vector, so the next time size() is called, the returned value will be smaller.

OTHER TIPS

Yes it does!

stlVector.size () // is called for evey iteration

Thus for every loop you'll have the test "i < stlVector.size ()" re-evaluate!

Yes, the test is performed, with side effects, for each loop.

A for loop is merely a nice convention - a for loop is easily decomposed as a while loop:

for (int i = 0 ; i < stlVector.size() ; i++)
{ 
    if (i == 10)
    { 
        stlVector.erase(stlVector.begin() + 5 )
    }
}

Becomes:

int i = 0 ;

while(i < stlVector.size())
{ 
    if (i == 10)
    { 
        stlVector.erase(stlVector.begin() + 5 )
    }
    i++;
}

-Adam

Yes, it does, but don't do this! If you want to remove elements from a vector, do it inside another loop. You are deleting elements after the i index in this case: nothing guarantees that the stlVector[i+5] element exists. If you remove the i-th element from the vector, your count is broken because you can jump elements without checking them.

The most safe way of doing this is storing references for the elements on the stlVector you want to delete on another vector, and then iterate on this auxiliar vector doing stlVector.erase(auxVector[i]).

I expect the code you provided is just "fantasy code" (as one commenter put it) to give a concrete example of the type of thing you're trying to do.

However just in case it's not: the loop you gave will skip over the 12th element (i.e. the element originally in stlVector[11]) because when examining stlVector[10] you delete an earlier element, causing all later elements to shunt forward one position, but you still increment i at the end of the loop. So the next iteration will look at stlVector[11] which is actually the element that was originally in stlVector[12]. To remedy this, you need to --i after the call to erase().

Always reevaluate sure!

Also, to clarify a bit, since you asked if it's done this way "in VC++ 6".

The "continue condition" is re-evaluate on every loop in EVERY version of C, C++, C# and Java.

If any complier does not generate code which does that, it is broken, and must be avoided.

As others have said, yes the condition is re-evaluated each time through the loop. That's why a common performance optimization is:

int saveSize = someExpensiveComputation();

for (int i = 0 ; i < saveSize ; i++)
{ 
    foo(i);
}

where the loop conditional is at all expensive to compute, instead of

for (int i = 0 ; i < someExpensiveComputation(); i++)
{ 
    foo(i);
}

Where the expensive computation is needlessly done each iteration through the loop.

Yes it reduces the size. More information is here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top