日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL之Iterator(迭代器)

發布時間:2024/4/17 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL之Iterator(迭代器) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

概述

根據迭代器功能的不同,將迭代器分為以下幾類:

Iterator Category Ability Providers
Input iteratorReads forwardistream
Output iteratorWrites forwardostream, inserter
Forward iteratorReads and writes forward?
Bidirectional iterator(雙向迭代器)Reads and writes forward and backwardlist, set, multiset, map, multimap
Random access iteratorReads and writes with random accessvector, deque string, array

?

下面,逐一分析各種迭代器。

?

輸入迭代器

  Input iterators can only step forward element-by-element with read access. Thus, they return values elementwise

  輸入迭代器的操作:

Table?7.2. Operations of Input Iterators

Expression Effect
*iter Provides read access to the actual element
iter ->member Provides read access to a member (if any) of the actual element
++iter Steps forward (returns new position)
iter++ Steps forward (returns old position)
Iter1 == iter2 Returns whether two iterators are equal
Iter1 != iter2 Returns whether two iterators are not equal
TYPE(iter) Copies iterator (copy constructor)

  注意:輸入迭代器只能夠讀取一次元素。幾乎所有其他迭代器都有輸入迭代器的功能。

?

輸出迭代器

  Output iterators are the counterparts of input iterators. They can only step forward with write access. Thus, you can assign new values only element-by-element. You can't use an output iterator to iterate twice over the same range. The goal is to write a value into a "black hole" (whatever that means). So, if you write something for the second time at the same position into the same black hole, it is not guaranteed that you will overwrite a previous value. Table 7.3 lists the valid operations for output iterators. The only valid use of operator * is on the left side of an assignment statement.

?

Table?7.3. Operations of Output Iterators

Expression Effect
*iter = value Writes value to where the iterator refers
++iter Steps forward (returns new position)
iter++ Steps forward (returns old position)
TYPE (iter) Copies iterator (copy constructor)

前向迭代器

  前向迭代器是輸入迭代器和輸出迭代器的組合。它包含輸入迭代器的所有功能和輸出迭代器的大部分功能。

  下面是前向迭代器的操作:

?

Table?7.4. Operations of Forward Iterators

Expression Effect
*iter Provides access to the actual element
iter-> member Provides access to a member of the actual element
++iter Steps forward (returns new position)
iter++ Steps forward (returns old position)
iter1 == iter2 Returns whether two iterators are equal
iter1 != iter2 Returns whether two iterators are not equal
TYPE() Creates iterator (default constructor)
TYPE(iter) Copies iterator (copy constructor)
iter1 = iter2

Assigns an iterator

  Unlike input iterators and output iterators, forward iterators can refer to the same element in the same collection and process the same element more than once.

?

為什么前向迭代器只包含輸出迭代器的大部分功能,而不是全部功能呢?

  對于輸出迭代器,在不檢查是不是隊列的末尾就寫數據是正確的。事實上,是不可以將一個輸出迭代器和一個終端迭代器作比較的,因為輸出迭代器沒有對比的操作。

//OK for output iterators//ERROR for forward iteratorswhile (true) {*pos = foo();++pos;}

  對于前向迭代器,在訪問迭代器指向的數據之前,必須確定該迭代器是否是正確的。這就是為什么上面的循環中的代碼對前向迭代器是錯誤的。如果該迭代器指向的是NULL,那么在沒有檢查之前便去訪問未知地址,會出現未定義行為。

//OK for forward iterators//IMPOSSIBLE for output iteratorswhile (pos != coll.end()) {*pos = foo();++pos;}

?

雙向迭代器

  Bidirectional iterators are forward iterators that provide the additional ability to iterate backward over the elements. Thus, they provide the decrement operator to step backward (Table 7.5).

?

Table?7.5. Additional Operations of Bidirectional Iterators

Expression Effect
-- iter Steps backward (returns new position)
iter-- Steps backward (returns old position)

?

隨機訪問迭代器

?

Random access iterators are bidirectional iterators that can perform random access. Thus, they provide operators for "iterator arithmetic" (in accordance with the "pointer arithmetic" of ordinary pointers). That is, they can add and subtract offsets, process differences, and compare iterators with relational operators such as < and >. Table 7.6 lists the additional operations of random access iterators.

?

Random access iterators are provided by the following objects and types:

?

  • Containers with random access (vector, deque)

  • Strings (string, wstring)

  • Ordinary arrays (pointers)

?

?

Table?7.6. Additional Operations of Random Access Iterators

Expression Effect
iter[n] Provides access to the element that has index n
iter+=n Steps n elements forward (or backward, if n is negative)
iter-=n Steps n elements backward (or forward, if n is negative)
iter+n Returns the iterator of the nth next element
n+iter Returns the iterator of the nth next element
iter-n Returns the iterator of the nth previous element
iter1-iter2 Returns the distance between iter1 and iter2
iter1<iter2 Returns whether iter1 is before iter2
iter1>iter2 Returns whether iter1 is after iter2
iter1<=iter2 Returns whether iter1 is not after iter2
iter1>=iter2 Returns whether iter1 is not before iter2

?

The following program demonstrates the special abilities of random access iterators:

?

?

// iter/itercat.cpp #include <vector>#include <iostream>using namespace std;int main(){vector<int> coll;//insert elements from -3 to 9for (int i=-3; i<=9; ++i) {coll.push_back (i);}/* print number of elements by processing the distance between beginning and end* - NOTE: uses operator -for iterators*/cout << "number/distance: " << coll.end()-coll.begin() << endl;/* print all elements* - NOTE: uses operator < instead of operator ! =*/vector<int>::iterator pos;for (pos=coll.begin(); pos<coll.end(); ++pos) {cout << *pos << ' '; }cout << endl;/* print all elements* - NOTE: uses operator [ ] instead of operator **/for (int i=0; i<coll.size(); ++i) {cout << coll.begin() [i] << ' ';}cout << endl;/* print every second element* - NOTE: uses operator +=*/for (pos = coll.begin(); pos < coll.end()-1; pos += 2) {cout << *pos << ' ';}cout << endl;}

The output of the program is as follows:

number/distance: 13-3 -2 -1 0 1 2 3 4 5 6 7 8 9-3 -2 -1 0 1 2 3 4 5 6 7 8 9-3 -1 1 3 5 7

?

轉載于:https://www.cnblogs.com/wiessharling/p/3990946.html

總結

以上是生活随笔為你收集整理的STL之Iterator(迭代器)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。