最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解析C++中的for循環(huán)以及基于范圍的for語句使用

 更新時間:2016年01月16日 17:01:05   投稿:goldensun  
這篇文章主要介紹了解析C++中的for循環(huán)以及基于范圍的for語句使用,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下

for循環(huán)語句

重復(fù)執(zhí)行語句,直到條件變?yōu)?false。

語法

for ( init-expression ; cond-expression ; loop-expression ) 
  statement;

備注
使用 for 語句可構(gòu)建必須執(zhí)行指定次數(shù)的循環(huán)。
for 語句包括三個可選部分,如下表所示。
for 循環(huán)元素

2016116165838737.png (878×231)

下面的示例將顯示使用 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


當(dāng) statement 中的 break、return 或 goto(轉(zhuǎn)到 for 循環(huán)外部的標(biāo)記語句)執(zhí)行時,for 循環(huán)將終止。 for 循環(huán)中的 continue 語句僅終止當(dāng)前迭代。
如果忽略 cond-expression,則認(rèn)為其為 true,for 循環(huán)在 statement 中沒有 break、return 或 goto 時不會終止。
雖然 for 語句的三個字段通常用于初始化、測試終止條件和遞增,但并不限于這些用途。例如,下面的代碼將打印數(shù)字 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++ 標(biāo)準(zhǔn)
C++ 標(biāo)準(zhǔn)中提到,for 循環(huán)中聲明的變量將在 for 循環(huán)結(jié)束后超出范圍。例如:


for (int i = 0 ; i < 5 ; i++) {
  // do something
}
// i is now out of scope under /Za or /Zc:forScope

默認(rèn)情況下,在 /Ze 下,for 循環(huán)中聲明的變量在 for 循環(huán)的封閉范圍終止前保持在范圍內(nèi)。
/Zc:forScope 無需指定 /Za 即可啟用 for 循環(huán)中聲明的變量的標(biāo)準(zhǔ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)中聲明的變量的標(biāo)準(zhǔn)行為,后者要求 for 循環(huán)中聲明的變量在循環(huán)完畢后超出范圍。在 for 循環(huán)中聲明變量后,編譯器會在內(nèi)部將其提升為 for 循環(huán)封閉范圍中的局部變量,即使存在同名的局部變量也會如此。

基于范圍的 for 語句
語句 statement 按順序反復(fù)執(zhí)行語句 expression 中的每個元素。
語法

  for ( for-range-declaration : expression )
statement 

備注
使用基于范圍的 for 語句構(gòu)造一個必須執(zhí)行的循環(huán)范圍,可以定義為任意一個循環(huán)訪問,例如 std::vector,或者其他任意用 begin() 和 end()定義的范圍。命名在 for-range-declaration 語句是屬于 for 的,不能在 expression 或 statement中再次聲明。請注意 自動 關(guān)鍵字是在 for-range-declaration 中部分語句的首選。
這段代碼展示了如何使用 for 范圍的循環(huán)來遍歷數(shù)組和向量:

// 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 轉(zhuǎn)到一個語句外的 for 循環(huán) continue 與語句終止當(dāng)前 for 循環(huán)的迭代。
記住這些關(guān)于范圍 for 的事實
自動識別數(shù)組。
識別那些有 .begin() 和 .end() 的容器。
使用基于自變量的查找 begin() 和 end() 。

相關(guān)文章

  • C++?system()函數(shù)的常用用法(全網(wǎng)最新)

    C++?system()函數(shù)的常用用法(全網(wǎng)最新)

    system()用于從C?/C++程序調(diào)用操作系統(tǒng)命令,這里給大家講解下C++?system()函數(shù)的常用用法,感興趣的朋友跟隨小編一起看看吧
    2023-01-01
  • C語言統(tǒng)計輸入字符各個字母出現(xiàn)頻率的解題思路

    C語言統(tǒng)計輸入字符各個字母出現(xiàn)頻率的解題思路

    這篇文章主要介紹了C語言統(tǒng)計輸入字符各個字母出現(xiàn)頻率的解題思路,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2015-08-08
  • C語言實現(xiàn)自動給QQ好友發(fā)窗口抖動

    C語言實現(xiàn)自動給QQ好友發(fā)窗口抖動

    這篇文章主要介紹了C語言實現(xiàn)自動給QQ好友發(fā)窗口抖動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • C語言實現(xiàn)雙人反彈球游戲

    C語言實現(xiàn)雙人反彈球游戲

    這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)雙人反彈球游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • C語言版二值圖像統(tǒng)計連通區(qū)域

    C語言版二值圖像統(tǒng)計連通區(qū)域

    這篇文章主要為大家詳細(xì)介紹了C語言版二值圖像統(tǒng)計連通區(qū)域的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • C++ 智能指針的模擬實現(xiàn)實例

    C++ 智能指針的模擬實現(xiàn)實例

    這篇文章主要介紹了C++ 智能指針的模擬實現(xiàn)實例的相關(guān)資料,智能指針是一個類,它把普通指針封裝起來,能實現(xiàn)和普通指針同樣的功能。,需要的朋友可以參考下
    2017-07-07
  • C++雙目運(yùn)算符+=的重載詳解

    C++雙目運(yùn)算符+=的重載詳解

    這篇文章主要介紹了詳解C++編程中的雙目運(yùn)算符重載,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-09-09
  • C語言?柔性數(shù)組的使用詳解

    C語言?柔性數(shù)組的使用詳解

    柔性數(shù)組(Flexible?Array)是引入的一個新特性,它允許你在定義結(jié)構(gòu)體時創(chuàng)建一個空數(shù)組,而這個數(shù)組的大小可以在程序運(yùn)行的過程中根據(jù)你的需求進(jìn)行更改特別注意的一點(diǎn)是:這個空數(shù)組必須聲明為結(jié)構(gòu)體的最后一個成員,并且還要求這樣的結(jié)構(gòu)體至少包含一個其他類型的成員
    2022-03-03
  • c++ qsort 與sort 對結(jié)構(gòu)體排序?qū)嵗a

    c++ qsort 與sort 對結(jié)構(gòu)體排序?qū)嵗a

    這篇文章主要介紹了c++ qsort 與sort 對結(jié)構(gòu)體排序?qū)嵗a,幫助大家更好的理解和學(xué)習(xí)c++,感興趣的朋友可以了解下
    2020-11-11
  • C++使用數(shù)組來實現(xiàn)哈夫曼樹

    C++使用數(shù)組來實現(xiàn)哈夫曼樹

    給定N個權(quán)值作為N個葉子結(jié)點(diǎn),構(gòu)造一棵二叉樹,若該樹的帶權(quán)路徑長度達(dá)到最小,稱這樣的二叉樹為最優(yōu)二叉樹,也稱為哈夫曼樹(Huffman?Tree)。哈夫曼樹是帶權(quán)路徑長度最短的樹,權(quán)值較大的結(jié)點(diǎn)離根較近
    2022-05-05

最新評論

井陉县| 镇沅| 收藏| 双流县| 保德县| 上蔡县| 油尖旺区| 政和县| 伽师县| 河津市| 额济纳旗| 醴陵市| 方城县| 潍坊市| 石棉县| 富蕴县| 绿春县| 蒲城县| 子长县| 常宁市| 通河县| 鄯善县| 临武县| 嘉荫县| 阿拉善左旗| 广西| 楚雄市| 滨州市| 天津市| 怀化市| 乌审旗| 富源县| 赣榆县| 个旧市| 宁陵县| 丰宁| 桑植县| 融水| 凤台县| 南溪县| 新闻|