Help Center

Local Navigation

Writing efficient loops

If your container is likely to contain more than one element, assign the size to a local variable.

If the order in which you iterate through items is not important, you can iterate backward to avoid the extra local variable on the stack and to make the comparison faster.

Code sample

int size = vector.size();
for( int i = 0; i < size; ++i ) {
for( int i = vector.size() - 1; i >= 0; --i ) {


Was this information helpful? Send us your comments.