解析C++中的for循環(huán)以及基于范圍的for語句使用
for循環(huán)語句
重復執(zhí)行語句,直到條件變?yōu)?false。
語法
for ( init-expression ; cond-expression ; loop-expression ) statement;
備注
使用 for 語句可構建必須執(zhí)行指定次數的循環(huán)。
for 語句包括三個可選部分,如下表所示。
for 循環(huán)元素
下面的示例將顯示使用 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 循環(huán)外部的標記語句)執(zhí)行時,for 循環(huán)將終止。 for 循環(huán)中的 continue 語句僅終止當前迭代。
如果忽略 cond-expression,則認為其為 true,for 循環(huán)在 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 循環(huán)和 C++ 標準
C++ 標準中提到,for 循環(huán)中聲明的變量將在 for 循環(huán)結束后超出范圍。例如:
for (int i = 0 ; i < 5 ; i++) {
// do something
}
// i is now out of scope under /Za or /Zc:forScope
默認情況下,在 /Ze 下,for 循環(huán)中聲明的變量在 for 循環(huán)的封閉范圍終止前保持在范圍內。
/Zc:forScope 無需指定 /Za 即可啟用 for 循環(huán)中聲明的變量的標準行為。
也可以使用 for 循環(huán)的范圍差異,重新聲明 /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 循環(huán)中聲明的變量的標準行為,后者要求 for 循環(huán)中聲明的變量在循環(huán)完畢后超出范圍。在 for 循環(huán)中聲明變量后,編譯器會在內部將其提升為 for 循環(huán)封閉范圍中的局部變量,即使存在同名的局部變量也會如此。
基于范圍的 for 語句
語句 statement 按順序反復執(zhí)行語句 expression 中的每個元素。
語法
for ( for-range-declaration : expression ) statement
備注
使用基于范圍的 for 語句構造一個必須執(zhí)行的循環(huán)范圍,可以定義為任意一個循環(huán)訪問,例如 std::vector,或者其他任意用 begin() 和 end()定義的范圍。命名在 for-range-declaration 語句是屬于 for 的,不能在 expression 或 statement中再次聲明。請注意 自動 關鍵字是在 for-range-declaration 中部分語句的首選。
這段代碼展示了如何使用 for 范圍的循環(huán)來遍歷數組和向量:
// 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 循環(huán)終止于 statement 執(zhí)行完成: break, return,或者 goto 轉到一個語句外的 for 循環(huán) continue 與語句終止當前 for 循環(huán)的迭代。
記住這些關于范圍 for 的事實
自動識別數組。
識別那些有 .begin() 和 .end() 的容器。
使用基于自變量的查找 begin() 和 end() 。
您可能感興趣的文章
- 04-02c語言沒有round函數 round c語言
- 01-10數據結構課程設計- 解析最少換車次數的問題詳解
- 01-10深入理解C++中常見的關鍵字含義
- 01-10使用C++實現全排列算法的方法詳解
- 01-10深入Main函數中的參數argc,argv的使用詳解
- 01-10深入解析最長公共子串
- 01-10c++中inline的用法分析
- 01-10如何尋找數組中的第二大數
- 01-10用C++實現DBSCAN聚類算法
- 01-10全排列算法的非遞歸實現與遞歸實現的方法(C++)


閱讀排行
本欄相關
- 04-02c語言函數調用后清空內存 c語言調用
- 04-02func函數+在C語言 func函數在c語言中
- 04-02c語言的正則匹配函數 c語言正則表達
- 04-02c語言用函數寫分段 用c語言表示分段
- 04-02c語言中對數函數的表達式 c語言中對
- 04-02c語言編寫函數冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數 round c語言
- 04-02c語言分段函數怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數 c語言中怎
- 04-02c語言調用函數求fibo C語言調用函數求
隨機閱讀
- 08-05dedecms(織夢)副欄目數量限制代碼修改
- 01-11ajax實現頁面的局部加載
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery


