# 【3-6】迭代器 迭代器(Iterator)是 STL(標準模板庫)中好用的工具,它就像是「智慧指標」,可以幫助我們在容器中「逐個」走訪元素,不需要自己寫繁瑣的 index 操作。 目前學到的 STL 容器只有 `vector`,之後會介紹更多。 ## 迭代器(Iterator) ### 宣告 ```cpp vector<int>::iterator it // 宣告迭代器 it ``` ### 移動 ```cpp it++ // 迭代器向前一格 ++it // 迭代器向前一格 it-- // 迭代器向後一格 --it // 迭代器向後一格 ``` ### 常用迭代器 ```cpp v.begin() // v陣列的起點 v.end() // V陣列的終點 *it // 取得目前指向的值 ``` ### 遍歷陣列 宣告一個迭代器 `it`,從容器 `v` 的 `begin()` 遍歷到 `end()`。 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { vector<int> v={1,2,3,4,5}; for(vector<int>::iterator it=v.begin();it!=v.end();it++){ cout << *it << " "; } return 0; } ``` ### 輸出結果 ``` 1 2 3 4 5 ``` ## auto `auto` 是一種透過編譯器來自動推測變數型態的關鍵字,好處是可以化簡代碼。 剛才的代碼可以被化簡成這個樣子,主要差別就在宣告的方式,因為 `v.begin()` 是一個迭代器,所以編譯器就會自動將 `auto` 視為在宣告迭代器。 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { vector<int> v={1,2,3,4,5}; for(auto it=v.begin();it!=v.end();it++){ cout << *it << " "; } return 0; } ``` ### 輸出結果 ``` 1 2 3 4 5 ``` ## range-based 如果確定要遍歷整個容器,我們可以這樣寫: ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { vector<int> v={1,2,3,4,5}; // 宣告一個容器 v for(auto it : v){ // 宣告一個迭代器 it,自動從頭開始遍歷容器 v cout << *it << " "; } return 0; } ``` ### 輸出結果 ``` 1 2 3 4 5 ``` 好的,我幫你寫一份【3-6】迭代器的 Python 對照版,並且解釋差異和用法,整篇整理如下,方便你複製貼上: --- # 【3-6】迭代器 迭代器(Iterator)是 STL(標準模板庫)中好用的工具,它就像是「智慧指標」,可以幫助我們在容器中「逐個」走訪元素,不需要自己寫繁瑣的 index 操作。 目前學到的 STL 容器只有 `vector`,之後會介紹更多。 ## 迭代器(Iterator) ### 宣告 ```cpp vector<int>::iterator it // 宣告迭代器 it ``` ### 移動 ```cpp it++ // 迭代器向前一格 ++it // 迭代器向前一格 it-- // 迭代器向後一格 --it // 迭代器向後一格 ``` ### 常用迭代器 ```cpp v.begin() // v陣列的起點 v.end() // v陣列的終點 *it // 取得目前指向的值 ``` ### 遍歷陣列 宣告一個迭代器 `it`,從容器 `v` 的 `begin()` 遍歷到 `end()`。 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { vector<int> v={1,2,3,4,5}; for(vector<int>::iterator it=v.begin();it!=v.end();it++){ cout << *it << " "; } return 0; } ``` ### 輸出結果 ``` 1 2 3 4 5 ``` ## auto `auto` 是一種透過編譯器來自動推測變數型態的關鍵字,好處是可以化簡代碼。 剛才的代碼可以被化簡成這個樣子,主要差別就在宣告的方式,因為 `v.begin()` 是一個迭代器,所以編譯器就會自動將 `auto` 視為在宣告迭代器。 ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { vector<int> v={1,2,3,4,5}; for(auto it=v.begin();it!=v.end();it++){ cout << *it << " "; } return 0; } ``` ### 輸出結果 ``` 1 2 3 4 5 ``` ## range-based 如果確定要遍歷整個容器,我們可以這樣寫: ```cpp=1 #include <bits/stdc++.h> using namespace std; int main() { vector<int> v={1,2,3,4,5}; // 宣告一個容器 v for(auto it : v){ // 宣告一個迭代器 it,自動從頭開始遍歷容器 v cout << it << " "; // C++ 這裡其實是直接取得元素本身,不用再解引用 * } return 0; } ``` ### 輸出結果 ``` 1 2 3 4 5 ``` ### Python 對照及說明 Python 也有迭代器(Iterator)這個概念,但用法比較隱式和簡化,不像 C++ 需要手動宣告迭代器或移動指標。 ### 宣告與使用迭代器 Python 內建容器(如 list、tuple、dict 等)本身就是可迭代物件,可以直接用 `for` 迴圈遍歷: ```python v = [1, 2, 3, 4, 5] for it in v: print(it, end=' ') ``` 輸出: ``` 1 2 3 4 5 ``` ### 手動建立迭代器物件 如果想模擬 C++ 明確的迭代器宣告與操作,可以用 `iter()` 取得迭代器物件,再用 `next()` 逐步取出: ```python v = [1, 2, 3, 4, 5] it = iter(v) # 取得迭代器物件 print(next(it)) # 1 print(next(it)) # 2 print(next(it)) # 3 # 也可以用 try-except 捕捉 StopIteration 來避免超出範圍錯誤 try: while True: print(next(it), end=' ') except StopIteration: pass ``` 但原則上,Python 很少去講迭代器,都是直接使用 `for` 迴圈。 --- 聯絡方式:codecodefunny@gmail.com 最後編修時間:2025/07/10 子柚筆