自C++ 11起,在C++中添加了基于范围的for循环。它在一个范围内执行for循环。用作与传统for循环等效的可读性更高的for循环,可在一定范围的值上运行,例如容器中的所有元素。
句法 :
for ( range_declaration : range_expression )
loop_statement
Parameters :
range_declaration :
a declaration of a named variable, whose type is the
type of the element of the sequence represented by
range_expression, or a reference to that type.
Often uses the auto specifier for automatic type
deduction.
range_expression :
any expression that represents a suitable sequence
or a braced-init-list.
loop_statement :
any statement, typically a compound statement, which
is the body of the loop.
C++实现:
// Illustration of range-for loop
// using CPP code
#include
#include
#include
输出:
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
In loop In loop In loop In loop In loop In loop
G e e k s
{1, 1}
{2, 2}
{3, 3}
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。