for(auto& entity : memoryManager.getItems()) entity->update(mFrameTime);
If memoryManager contains 1000 items, does memoryManager.getItems()
get called 1000 times or only one at the beginning of the loop?
Does the compiler run any optimization with -O2 (or -O3)?
(memoryManager.getItems()
returns a std::vector<Entity*>&
)
Syntax. The for loop consists of three optional expressions, followed by a code block: initialization - This expression runs before the execution of the first loop, and is usually used to create a counter. condition - This expression is checked each time before the loop runs.
Range-based for loop in C++ Range-based for loop in C++ is added since C++ 11. It executes a for loop over a range. Used as a more readable equivalent to the traditional for loop operating over a range of values, such as all elements in a container.
while Loops ( Condition-Controlled Loops )While loops check for the stopping condition first, and may not execute the body of the loop at all if the condition is initially false.
It is only evaluated once. The standard defines a range-based for
statement as equivalent to:
{ auto && __range = range-init; for ( auto __begin = begin-expr, __end = end-expr; __begin != __end; ++__begin ) { for-range-declaration = *__begin; statement } }
where range-init
is the expression (surrounded by parentheses) or braced-init-list after the :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With