利為匯wordpress教程廈門關(guān)鍵詞seo排名網(wǎng)站
文章目錄
- 1. 基本語法
- 1.1 decalaration
- 默認獲取值
- 引用&
- 自動類型推導(auto)
- 1.2 container
- 數(shù)組
- STL容器
- 初始化列表
- 自定義類型
- 返回容器的函數(shù)
- 2. 其他示例
- 2.1 遍歷數(shù)組
- 2.2 遍歷vector,并修改元素
- 2.3 使用常量引用遍歷,防止容器中的值被誤修改
- 3. 小結(jié)
C++11 引入了基于范圍的for循環(huán),自動迭代一個范圍對象中的每個元素,而無需顯式地使用循環(huán)變量或索引,是一種簡化數(shù)組遍歷、簡化容器遍歷的語法糖。
語法糖,“錦上添花”的意思,并不會引入新的功能或者改變語言的核心功能,只是讓代碼更容易理解、編寫或維護。提供簡潔語法,同時不影響性能。
1. 基本語法
for(declaration : container)
{
//循環(huán)體
}
1.1 decalaration
默認獲取值
是一個變量,依次獲取范圍中的每個元素的值,即遍歷過程中每個元素將自己拷貝一份給decalaration
,適合用于小型元素,例如 int
等,對于較大較復雜的對象,會有資源開銷。
例如:
for(int val:vec)
{
// val 是 vec 中元素的副本
}
引用&
可以通過引用&
獲取范圍中的每個元素,避免復制,適用于較大的數(shù)據(jù)類型,例如:
for(string& str : vec)
{
// 遍歷過程中,str 是 vec 中元素的引用
}
自動類型推導(auto)
通過auto
關(guān)鍵字,讓編譯器自動推導元素的類型。配合引用&
,可以自動處理復雜類型的推導。例如:
for(auto& elem: vec)
{
// auto& 推導出 vec 中元素的類型,通過引用獲取元素
}
1.2 container
container,是被遍歷的范圍對象,必須支持支持begin() 和end()函數(shù),這樣才能讓 for 循環(huán)知道從哪里開始和結(jié)束遍歷。常見的范圍對象有數(shù)組、STL容器、初始化列表,或者返回類型為容器的函數(shù)。
數(shù)組
int arr[] = {1,2,3,4,5};
for(int val:arr)
{cout<<val<<" ";
}
STL容器
例如 std::vector、std::list、std::map 等標準容器
#include <iostream>
#include <vector>
using namespace std;int main()
{vector<int>vec = { 1,2,3,4,5 };for (int i : vec){cout << i << " ";}cout << endl;return 0;
}
//輸出1 2 3 4 5
初始化列表
int main()
{for (int i : { 1, 2, 3, 4, 5 }){cout << i << " ";}cout << endl;return 0;
}
//輸出1 2 3 4 5
自定義類型
只要自定義類型提供了begin()和end()
函數(shù),就可以被基于范圍的for
循環(huán)遍歷。例如:
class CustomContainer
{
public:int* begin() { return &data[0]; }int* end() { return &data[sizeof(data)/sizeof(data[0])]; }// 計算的是數(shù)組的長度(即 10)
private:int data[10] = { 1,2,3,4,5 };// 數(shù)組自動填充剩余部分為0
};
int main()
{CustomContainer c;for (auto i : c)cout << i << " ";// 輸出 1 2 3 4 5 0 0 0 0 0return 0;
}
在C++容器(如數(shù)組、vector)等中,end()返回的不是最后一個元素的迭代器,而是指向最后一個元素下一個位置的迭代器,這個位置不是有效的元素,僅僅用于表達結(jié)束和終點。
這是C++標準庫設(shè)計的常見模式,稱為半開區(qū)間。范圍的起點是包含的,而終點是不包含的,包含begin()所指向的元素,不包含end()所指向的位置上的元素。
返回容器的函數(shù)
如果一個函數(shù)返回容器或可迭代對象,可以直接將函數(shù)調(diào)用作為范圍對象是用。例如
#include <iostream>
#include <vector>
using namespace std;vector<int> getNumbers()
{return { 1,2,3,4,5,6 };
}
int main()
{ for(int i:getNumbers())cout << i << " ";// 輸出 1 2 3 4 5 6return 0;
}
2. 其他示例
2.1 遍歷數(shù)組
int main()
{int arr[] = { 1,2,3,4,5,6,7,8,9,10 };for(int i:arr)cout << i <<" "; //輸出1 2 3 4 5 6 7 8 9 10return 0;
}
2.2 遍歷vector,并修改元素
#include <iostream>
#include <vector>
using namespace std;int main()
{vector<int> vec = { 1,2,3,4,5,6 };for (int& i : vec){i *= 2;}for (int i : vec)cout << i << " "; //輸出2 4 6 8 10 12return 0;
}
2.3 使用常量引用遍歷,防止容器中的值被誤修改
#include <iostream>
#include <vector>
using namespace std;int main()
{vector<string> words = { "Hello","World" };for (const auto& w : words){cout << w << " ";//輸出:Hello World}return 0;
}
3. 小結(jié)
基于范圍的 for 循環(huán)背后的實現(xiàn)依賴于兩個函數(shù):
begin():指向容器或數(shù)組的第一個元素
end():指向容器或數(shù)組的末尾元素(不含)
假設(shè)我們有如下代碼:
#include <iostream>
#include <vector>
#include <string>
using namespace std;int main()
{vector<string> words = { "Hello","World","C++" };for (auto& w : words){cout<<w<<" ";}cout << endl; return 0;
}
等價于
#include <iostream>
#include <vector>
#include <string>
using namespace std;int main()
{vector<string> words = { "Hello","World","C++" };for (auto w = words.begin(); w != words.end(); w++){cout << *w << " ";}cout << endl;return 0;
}