C++的迭代器Interator
***************************************************
更多精彩,歡迎進入:http://shop115376623.taobao.com
STL視頻教程:
http://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-9510581626.21.y9vLuz&id=43055362725
***************************************************
容器就是數據結構的泛指,迭代器就是指針的泛指,可以指向元素。
容器相當于一個儲藏柜,里面裝的許多不同的物品就像是儲存的元素,比如面包、啤酒、蘋果、現金。要取得各個物體就得用與各個物體向匹配的工具,如取出面包要用盤子、取出啤酒要用杯子、取出蘋果要用籃子、取出現金要用錢包。迭代器的作用就相當于取出物品的工具的抽象,通過迭代器泛指現實生活中從貯藏室中取出物體的工具。C++迭代器是一種檢查容器內元素并遍歷元素的數據類型。
[容器---數據結構(儲藏柜)、迭代器--指針(取出物品的工具)]
C++迭代器Interator就是一個指向某種STL對象的指針。 通過該指針可以簡單方便地遍歷所有元素。1. Iterator定義
? ? ? 英文定義:
In C++, an iterator is any object that, pointing to some element in a range of elements (such as an array or a?container), has the ability to iterate through the elements of that range using a set of operators (at least, the increment (++) and dereference (*) operators).
The most obvious form of iterator is a?pointer: A pointer can point to elements in an array, and can iterate through them using the increment operator (++). But other forms of iterators exist. For example, each?container?type (such as a?vector) has a specific?iterator?type designed to iterate through its elements in an efficient way
C++中的iterator為STL中的重要概念。iterator的概念源自于對遍歷一個線性容器工具的抽象,即如何你能訪問這個容器的某個元素。對于最簡單的數組,當然可以用數組的索引值,因為數組是連續存放在內存中的;但對于鏈表,就必須用指針。除此之外,還有還有很多種數據結構需要提供一個方便的工具來訪問其中的元素,方法有ID,關鍵字等等。為了統一所有的容器的這種工具的使用,一般提供一整套容器的開發者就會用一種方式來表示各種容器的訪問工具。例如C++STL就是使用iterator。MFC自己的容器使用position。C#和java也有自己的方法,但方法是不變的。
iterator的用法可以被統一,但不同的底層容器實現其iterator的原理是不一樣的。例如iterator++你可以理解為移動到容器的下一個元素,如果底層如果是數組,把索引值加一就行;如果底層是鏈表,就得執行類似于m_pCurrent=m_pCurrent->pNext;的操作。因此每種容器都有自己的iterator實現方法。Iterator的常用方法有:?
iterator++????? 移到下個元素?
iterator--??????? 移到上個元素?
*iterator?????? ? 訪問iterator所指元素的值
< > == !=?????? Iterator之間的比較,例如判斷哪個元素在前
2. 容器的 iterator 類型
????? 每種容器類型都定義了自己的C++迭代器類型,如vector:vector<int>::iterator iter;這符語句定義了一個名為 iter 的變量,它的數據類型是vector<int> 定義的iterator 類型。每個標準庫容器類型都定義了一個名為iterator的成員,這里的iterator與迭代器實際類型的含義相同。
begin和end操作每種容器都定義了一對命名為begin和end的函數,用于返回迭代器。如果容器中有元素的話,由 begin 返回的迭代器指向第一個元素:
vector<int>::iterator iter = ivec.begin();
????? 上述語句把 iter 初始化為由名為vector操作返回的值。假設vector不空,初始化后,iter即指該元素為ivec[0]。
由end操作返回的C++迭代器指向vector的"末端元素的下一個"。"超出末端迭代器"(off-the-end iterator)。表明它指向了一個不存在的元素。如果vector為空,begin返回的迭代器與end返回的迭代器相同。由end操作返回的迭代器并不指向 vector 中任何實際的元素,相反,它只是起一個哨兵(sentinel)的作用,表示我們已處理完 vector 中所有元素。
cout<<"Befort *2 the elements are:"<<endl; ??
for(vector<int>::size_type ix=0; ?
ix!=10;++ix){ ivec.push_back(ix); ??
cout<<ivec[ix]<<'\t'; ??
} ??
//把每個值乘以2并輸出 ??
cout<<endl<<"After *2 the elements are:"<<endl; ??
for(vector<int>::iterator iter=ivec.begin(); ?
iter!=ivec.end();++iter) ?
{ ??
*iter*=2; cout<<*iter<<'\t'; ??
} ??
return 0; ?
}?
b)例2
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> ivec;
ivec.push_back(1);
ivec.push_back(2);
ivec.push_back(3);
ivec.push_back(4);
for(vector<int>::iterator iter = ivec.begin();iter != ivec.end(); ++iter)
cout << *iter << endl;
return 0;
}
總結
以上是生活随笔為你收集整理的C++的迭代器Interator的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Dataset之LFW:LFW人脸数据库
- 下一篇: s3c2440移植MQTT