<iterator>
header
迭代器定義
一個迭代器可以是指向一定範圍內的數組或者容器中的元素,具有在一定範圍內數組進行迭代的操作集合(至少具有增量 ++ 能力和 dereference *
運算符)。
迭代器最明顯的形式是指針。指針可以指向數組中的元素,並可以使用運算符 ++
迭代它們,但是也可能是其他種類的迭代器。例如,每個容器類型(例如list)具有一個特定的迭代器類型,旨在迭代其元素。
請注意,雖然指針是迭代器的一種形式,但並非所有迭代器都具有相同的指針功能;
迭代器類別
迭代器根據他們的功能分爲五類:
Input 和 output 迭代器是最有限的迭代器類型:它們可以執行順序單通道輸入或輸出操作。
Forward iterators 具有 input iterators 所有功能,並且如果它們不是常量迭代器也是 output iterators 的功能, 儘管它們僅限於迭代範圍(前向)的一個方向. 所有 standard containers 至少支持前向迭代器類型。
Bidirectional iterators 就像 forward iterators 但也可以向後迭代。
Random-access iterators 實現了 bidirectional iterators 所有的功能, 並且還能夠非順序訪問範圍: 通過將迭代值應用於迭代器而不迭代其間的所有元素,可以直接訪問遠程元素。這些迭代器具有與標準指針類似的功能(指針是此類別的迭代器)。
迭代器類別的屬性:
類別 | 特性 | 表達 | |||
---|---|---|---|---|---|
所有類別 | copy-constructible, copy-assignable and destructible | X b(a); |
|||
Can be incremented | ++a |
||||
Random Access | Bidirectional | Forward | Input | Supports equality/inequality comparisons | a == b |
Can be dereferenced as an rvalue | *a a->m |
||||
Output | Can be dereferenced as an lvalue (only for mutable iterator types) |
*a = t *a++ = t |
|||
default-constructible | X a; X() |
||||
Multi-pass: neither dereferencing nor incrementing affects dereferenceability | { b=a; *a++; *b; } |
||||
Can be decremented | --a a-- *a-- |
||||
Supports arithmetic operators + and - | a + n n + a a - n a - b |
||||
Supports inequality comparisons (<, >, <= and >=) between iterators | a < b a > b a <= b a >= b |
||||
Supports compound assignment operations += and -= | a += n a -= n |
||||
Supports offset dereference operator ([]) | a[n] |
其中 X 是迭代器類型,a 和 b 是此迭代器類型的對象,t 是迭代器類型指向的類型的對象,n 是整數值。
有關更多詳細信息,請參閱 input iterator, output iterator, forward iterator, bidirectional iterator 和 random-access iterator.
功能
迭代器操作:
迭代器向前推進
返回兩個迭代器之間的距離
將迭代器返回到開頭(增長方向:begin -> end)
將迭代器返回到結尾
獲取前一個元素的迭代器
獲取下一個元素的迭代器
迭代器生成:
從尾部插入元素
從首部插入元素
從指定位置插入一段元素
Construct move iterator (function template )
類
迭代器基類
迭代器特徵
預定義的迭代器
反轉迭代器
移動迭代器
後插入迭代器
前插入迭代器
插入迭代器
Istream迭代器
Ostream迭代器
輸入流緩衝區迭代器
輸出流緩衝區迭代器
類別標籤
輸入迭代器類別
輸出迭代器類別
轉發迭代器類
雙向迭代器類
隨機訪問迭代器類