C++:iterator 頭文件詳解

<iterator>


header

迭代器定義

一個迭代器可以是指向一定範圍內的數組或者容器中的元素,具有在一定範圍內數組進行迭代的操作集合(至少具有增量 ++ 能力和 dereference *運算符)。

迭代器最明顯的形式是指針。指針可以指向數組中的元素,並可以使用運算符 ++ 迭代它們,但是也可能是其他種類的迭代器。例如,每個容器類型(例如list)具有一個特定的迭代器類型,旨在迭代其元素。

請注意,雖然指針是迭代器的一種形式,但並非所有迭代器都具有相同的指針功能;


迭代器類別

迭代器根據他們的功能分爲五類:

Input 和 output 迭代器是最有限的迭代器類型:它們可以執行順序單通道輸入或輸出操作。

Forward iterators 具有 input iterators 所有功能,並且如果它們不是常量迭代器也是 output iterators 的功能, 儘管它們僅限於迭代範圍(前向)的一個方向. 所有 standard containers 至少支持前向迭代器類型。

Bidirectional iterators 就像 forward iterators 但也可以向後迭代。

Random-access iterators 實現了 bidirectional iterators 所有的功能, 並且還能夠非順序訪問範圍: 通過將迭代值應用於迭代器而不迭代其間的所有元素,可以直接訪問遠程元素。這些迭代器具有與標準指針類似的功能(指針是此類別的迭代器)。


迭代器類別的屬性:

類別 特性 表達
所有類別 copy-constructiblecopy-assignable and destructible X b(a);
b = a;
Can be incremented ++a
a++
Random Access Bidirectional Forward Input Supports equality/inequality comparisons a == b
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 iteratoroutput iteratorforward iteratorbidirectional iterator 和 random-access iterator.


功能

迭代器操作:

advance

迭代器向前推進

distance

返回兩個迭代器之間的距離

begin 

將迭代器返回到開頭(增長方向:begin -> end)

end 

將迭代器返回到結尾

prev 

獲取前一個元素的迭代器

next 

獲取下一個元素的迭代器


迭代器生成:

back_inserter

從尾部插入元素

front_inserter

從首部插入元素

inserter

從指定位置插入一段元素

make_move_iterator 

Construct move iterator (function template )


iterator

迭代器基類

iterator_traits

迭代器特徵


預定義的迭代器

reverse_iterator

反轉迭代器

move_iterator 

移動迭代器

back_insert_iterator

後插入迭代器

front_insert_iterator

前插入迭代器

insert_iterator

插入迭代器

istream_iterator

Istream迭代器

ostream_iterator

Ostream迭代器

istreambuf_iterator

輸入流緩衝區迭代器

ostreambuf_iterator

輸出流緩衝區迭代器


類別標籤

input_iterator_tag

輸入迭代器類別

output_iterator_tag

輸出迭代器類別

forward_iterator_tag

轉發迭代器類

bidirectional_iterator_tag

雙向迭代器類

random_access_iterator_tag

隨機訪問迭代器類

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章