C++ Iterators === [ISO C++ N4861 Post-Prague 2020 C++ working draft](https://github.com/cplusplus/draft/releases) 平常大家在撰寫C++的時候,會使用迭代器(Iterator)操作各式各樣在標準函式庫的資料結構,例如: 遍歷vector ```Clike= std::vector<int> example_vec; vector<int>::iterator example_vec_begin = example_vec.begin(); vector<int>::iterator example_vec_end = example_vec.end(); while(example_vec_begin!=example_vec_end) { *example_vec_begin = 1; cout << *example_vec_begin; example_vec_begin++; } ``` 但對於Iterator其實有分為幾個種類。每種操作的方式都不太一樣。這也是這一篇我主要想要探討和釐清的觀念。(因為我也不懂,所以找資料學習)。 --- 在ISO C++是這麼定義的: > 迭代器(Iterator)是指針的一般化,它允許C++以統一的方式使用不同的資料結構(容器)。 也就是Iterator就類似C Language的pointer,但是他操作不同的資料結構有統一的方法。 因此可以這樣操作: ```Clike= cout << *example_vec_begin; ``` * ISO C++ 27.2.1 說明了iterator的輸入和輸出: > An input iterator _i_ supports the expression _*i_, resulting in a value of some object type T, called the value typeof the iterator. > An output iterator _i_ has a non-empty set of types that are writable to the iterator; for each such type T, the expression _*i_ = _o_ is valid where _o_ is a value of type T. An iterator _i_ for which the expression(_*i_) --- * ISO C++ 27.2.1定義了幾種C++會使用到的iterator: > ![](https://i.imgur.com/F3ic5NX.png) * 分別為: 1. input iterator, [ISO C++ 23.3.4.9 Concept input_iterator] 2. output iterator, [ISO C++ 23.3.4.10 Concept Output_iterator] 3. forward iterator, [ISO C++ 23.3.4.11 Concept Forward_iterator] 4. bidirectional iterator [ISO C++ 23.3.4.12 Concept Bidirectional_iterator] 5. random access iterator [ISO C++ 23.3.4.13 Concept Random_access_iterator] 6. contiguous iterator [ISO C++ 23.3.4.14 Concept contiguous_iterator] --- * input iterator: > The input_iterator concept defines requirements for a type whose referenced values can be read and which can be both pre- and post-incremented. input_iterator概念定義了對一種類型的要求,該類型的參考值可以讀取,並且可以在讀取前和讀取後遞增。 > ISO C++ 23.3.5.2 Input iterators 定義了一些合法的用法: ``` a != b *a a->m ++r (void)r++ *r++ ``` * output_iterator: > The output_iteratorconcept defines requirements for a type that can be used to write values, and which can be both pre- and post-incremented. output_iterator概念定義了對可用於寫入值的類型的要求,並且該類型可以先增加後再增加。 > ISO C++ 23.3.5.3 Output iterators 定義了一些合法的用法: ``` *r = o ++r r++ *r++ = o ``` * forward iterator: > The forward_iterator concept adds copyability, equality comparison, and the multi-pass guarantee. forward_iterator概念增加了可複製性,相等比較和多次通過保證。 > ISO C++ 23.3.5.4 forward_iterator 定義了一些合法的用法: ``` r++ *r++ ``` 特別的是(input iterator和output_iterator不支援): > _a_ == _b_ implies ++ _a_ == _b_ ++ and _X_ is a pointer type or the expression (void)++_X_(_a_), _*a_ is equivalent to the expression _*a_. 在forward_iterator * bidirectional_iterator: > The bidirectional_iterator concept adds the ability to move an iterator backward as well as forward. bidirectional_iterator概念增加了向後和向前移動迭代器的功能。 > ISO C++ 23.3.5.5 bidirectional_iterator 定義了一些合法的用法: ``` --r r-- *r-- ``` * random_access_iterator: > The random_access_iterator concept adds support for constant-time advancement with+=,+,-=, and-, as well as the computation of distance in constant time with-. Random access iterators also support array notation via subscripting. random_access_iterator概念增加了對使用+ =,+,-=和-進行連續時間前進的支持,以及使用-進行連續時間的距離計算。Random access iterators還通過下標([])支持陣列符號。 ``` r += n a + n n + a r -= n a - n b - a a[n] a < b a > b a >= b a <= b ```