Search code examples
c++visual-c++for-loopvisual-c++-6termination

Does the termination condition of a 'for loop' refresh in VC++ 6?


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


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.