解析C++中的for輪回和基於規模的for語句應用。本站提示廣大學習愛好者:(解析C++中的for輪回和基於規模的for語句應用)文章只能為提供參考,不一定能成為您想要的結果。以下是解析C++中的for輪回和基於規模的for語句應用正文
for輪回語句
反復履行語句,直到前提變成 false。
語法
for ( init-expression ; cond-expression ; loop-expression ) statement;
備注
應用 for 語句可構建必需履行指定次數的輪回。
for 語句包含三個可選部門,以下表所示。
for 輪回元素
上面的示例將顯示應用 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 語句僅終止以後迭代。
假如疏忽 cond-expression,則以為其為 true,for 輪回在 statement 中沒有 break、return 或 goto 時不會終止。
固然 for 語句的三個字段平日用於初始化、測試終止前提和遞增,但其實不限於這些用處。例如,上面的代碼將打印數字 0 至 4。在這類情形下,statement 是 null 語句:
#include <iostream>
using namespace std;
int main()
{
int i;
for( i = 0; i < 5; cout << i << '\n', i++){
;
}
}
for 輪回和 C++ 尺度
C++ 尺度中提到,for 輪回中聲明的變量將在 for 輪回停止後超越規模。例如:
for (int i = 0 ; i < 5 ; i++) {
// do something
}
// i is now out of scope under /Za or /Zc:forScope
默許情形下,在 /Ze 下,for 輪回中聲明的變量在 for 輪回的關閉規模終止前堅持在規模內。
/Zc:forScope 無需指定 /Za 便可啟用 for 輪回中聲明的變量的尺度行動。
也能夠應用 for 輪回的規模差別,從新聲明 /Ze 下的變量,以下所示:
// 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 語句
語句 statement 按次序重復履行語句 expression 中的每一個元素。
語法
for ( for-range-declaration : expression ) statement
備注
應用基於規模的 for 語句結構一個必需履行的輪回規模,可以界說為隨意率性一個輪回拜訪,例如 std::vector,或許其他隨意率性用 begin() 和 end()界說的規模。定名在 for-range-declaration 語句是屬於 for 的,不克不及在 expression 或 statement中再次聲明。請留意 主動 症結字是在 for-range-declaration 中部門語句的首選。
這段代碼展現了若何應用 for 規模的輪回來遍歷數組和向量:
// 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 輪回的迭代。
記住這些關於規模 for 的現實
主動辨認數組。
辨認那些有 .begin() 和 .end() 的容器。
應用基於自變量的查找 begin() 和 end() 。