C++11 —— 基於區間(range)的 for 迴圈
The range-based for statement
0. <algorithm> 中的 for_each
for_each 的第三個引數接受一個函式物件;
void do_cout(int n){
cout << n << endl;
}
int main() {
int arr[] = {1, 2, 3};
std::for_each(arr, arr+3, do_cout);
return 0;
}
1. 統一容器的迭代方法
int main(int, char**){
int arr[] = {1, 2, 3};
for (auto e: arr){
cout << e << endl;
}
return 0;
}
2. 意外之喜:避免迴圈變數的誤寫
for (int i = 0; i < arr.size(); ++i)
for (int j = 0; j < i; ++i) // ⇒ 應該是 ++j
使用基於區間的 for 迴圈便可避免這種情況的發生。
相關文章
- Go 關閉chanel & chanel的range迴圈Go
- PyThon range()函式中for迴圈用法Python函式
- 【基礎題】【for迴圈】二重迴圈
- Go 處理大陣列:使用 for range 還是 for 迴圈?Go陣列
- 探討兩種迴圈表示方法的區別,while迴圈與for迴圈的小總結While
- 基於迴圈佇列的BFS的原理及實現佇列
- 對於 JavaScript 中迴圈之間的技術差異概述JavaScript
- JavaScript的map迴圈、forEach迴圈、filter迴圈、reduce迴圈、reduceRight迴圈JavaScriptFilter
- 讓自定義的容器,也能基於範圍迴圈
- while + else 使用,while死迴圈與while的巢狀,for迴圈基本使用,range關鍵字,for的迴圈補充(break、continue、else) ,for迴圈的巢狀,基本資料型別及內建方法While巢狀資料型別
- 當型迴圈和直到型迴圈有哪些區別
- C#程式設計基礎第七課:C#中的基本迴圈語句:while迴圈、do-while迴圈、for迴圈、foreach迴圈的使用C#程式設計While
- python 基礎 迴圈Python
- 基於區塊鏈技術的FDF智慧合約迴圈互助遊戲開發實現區塊鏈遊戲開發
- 關於一個迴圈請求與迴圈計時器的問題
- Python基礎(07):迴圈Python
- react基礎之-- 列表迴圈React
- 指令碼迴圈基礎(2)指令碼
- 12-JavaScript基礎-for迴圈JavaScript
- JAVA基礎--增強for迴圈Java
- 【js迴圈語句】for與while的區別JSWhile
- Kotlin中 until和..的區別(迴圈)Kotlin
- C語言——迴圈結構(for迴圈,while迴圈,do-while迴圈)C語言While
- Vue.js 元件 – 元件間的迴圈引用示例Vue.js元件
- iOS 關於NSTimer的迴圈引用iOS
- while read line 與for迴圈的區別While
- 【Python基礎】for迴圈語句Python
- Python基礎教程06 - 迴圈Python
- 基於C++11的執行緒池實現C++執行緒
- 迴圈中的非同步&&迴圈中的閉包非同步
- while迴圈補充、for迴圈、range關鍵字、內建方法之整型、內建方法之浮點型、內建方法之字串While字串
- for迴圈的理解
- let const快取for迴圈的中間變數快取變數
- Swift - 使用 Protocol 避免框架之間迴圈引用SwiftProtocol框架
- 時間遞增迴圈執行指令碼指令碼
- Java入門學習-學習if & else,for迴圈,foreach迴圈,while迴圈的用法。JavaWhile
- Python中for迴圈和while迴圈有什麼區別?Python入門教程PythonWhile
- JS中幾種迴圈的區別與相同點JS