解析C++中的for循环以及基于范围的for语句使用
for循环语句 重复执行语句,直到条件变为 false。 语法 for ( init-expression ; cond-expression ; loop-expression ) statement; 备注 下面的示例将显示使用 for 语句的不同方法。 #include <iostream> using namespace std; int main() { // The counter variable can be declared in the init-expression. for (int i = 0; i < 2; i++ ){ cout << i; } // Output: 01 // The counter variable can be declared outside the for loop. int i; for (i = 0; i < 2; i++){ cout << i; } // Output: 01 // These for loops are the equivalent of a while loop. i = 0; while (i < 2){ cout << i++; } } // Output: 012 init-expression 和 loop-expression 可以包含以逗号分隔的多个语句。例如: #include <iostream> using namespace std; int main(){ int i,j; for ( i = 5,j = 10 ; i + j < 20; i++,j++ ) { cout << "i + j = " << (i + j) << 'n'; } } // Output: i + j = 15 i + j = 17 i + j = 19
loop-expression 可以递增或递减,或通过其他方式修改。 #include <iostream> using namespace std; int main(){ for (int i = 10; i > 0; i--) { cout << i << ' '; } // Output: 10 9 8 7 6 5 4 3 2 1 for (int i = 10; i < 20; i = i+2) { cout << i << ' '; } // Output: 10 12 14 16 18
当 statement 中的 break、return 或 goto(转到 for 循环外部的标记语句)执行时,for 循环将终止。 for 循环中的 continue 语句仅终止当前迭代。 #include <iostream> using namespace std; int main() { int i; for( i = 0; i < 5; cout << i << 'n',i++){ ; } }
for 循环和 C++ 标准
for (int i = 0 ; i < 5 ; i++) { // do something } // i is now out of scope under /Za or /Zc:forScope 默认情况下,在 /Ze 下,for 循环中声明的变量在 for 循环的封闭范围终止前保持在范围内。 // for_statement5.cpp int main(){ int i = 0; // hidden by var with same name declared in for loop for ( int i = 0 ; i < 3; i++ ) {} for ( int i = 0 ; i < 3; i++ ) {} }
这更类似于 for 循环中声明的变量的标准行为,后者要求 for 循环中声明的变量在循环完毕后超出范围。在 for 循环中声明变量后,编译器会在内部将其提升为 for 循环封闭范围中的局部变量,即使存在同名的局部变量也会如此。 基于范围的 for 语句 for ( for-range-declaration : expression ) statement 备注 // range-based-for.cpp // compile by using: cl /EHsc /nologo /W4 #include <iostream> #include <vector> using namespace std; int main() { // Basic 10-element integer array. int x[10] = { 1,2,3,4,5,6,7,8,9,10 }; // Range-based for loop to iterate through the array. for( int y : x ) { // Access by value using a copy declared as a specific type. // Not preferred. cout << y << " "; } cout << endl; // The auto keyword causes type inference to be used. Preferred. for( auto y : x ) { // Copy of 'x',almost always undesirable cout << y << " "; } cout << endl; for( auto &y : x ) { // Type inference by reference. // Observes and/or modifies in-place. Preferred when modify is needed. cout << y << " "; } cout << endl; for( const auto &y : x ) { // Type inference by reference. // Observes in-place. Preferred when no modify is needed. cout << y << " "; } cout << endl; cout << "end of integer array test" << endl; cout << endl; // Create a vector object that contains 10 elements. vector<double> v; for (int i = 0; i < 10; ++i) { v.push_back(i + 0.14159); } // Range-based for loop to iterate through the vector,observing in-place. for( const auto &j : v ) { cout << j << " "; } cout << endl; cout << "end of vector test" << endl; }
输出如下: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 end of integer array test 0.14159 1.14159 2.14159 3.14159 4.14159 5.14159 6.14159 7.14159 8.14159 9.14159 end of vector test 一个基于 for 循环终止于 statement 执行完成: break, return,或者 goto 转到一个语句外的 for 循环 continue 与语句终止当前 for 循环的迭代。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |