【C++】:STL迭代器使用詳解,很好很詳細

寫在前面,迭代器這種東西,就是爲了使訪問簡單!!ios

容器::iterator iter;程序員

for(iter= 容器.begin();iter!=容器.end();iter++){算法

cout<<*iter或者是 iter->first等等之類的                                    //迭代器就是這麼個套路數組

}數據結構


而後就是set,set就是一個集合,僅此而已,而其餘的,像是map比較有意思,提供了兩種數據結構的對應,而其餘的咱們不要簡單的認爲是隊列和棧,咱們要認爲這是一種容器,可以包含多種數據結構,用了這種思想,咱們就會想的更明白dom


下面給出摘抄下來的正文:函數



指針與數組
指針與其它數據結構呢?好比說鏈表?
存儲空間是非連續的。不能經過對指向這種數據結構的指針作累加來遍歷。
能不能提供一個行爲相似指針的類,來對非數組的數據結構進行遍歷呢?這樣咱們就可以以一樣的方式來遍歷全部數據結構(容器)。
迭代器(Iterator)是指針(pointer)的泛化,它容許程序員以相同的方式處理不一樣的數據結構(容器)。STL中有五種類型的迭代器,它們分別知足必定的要求。不一樣的迭代器要求定義的操做不同。

箭頭表示左邊的迭代器必定知足右邊迭代器須要的條件。spa

下面的圖表畫出了這幾種:指針

input output
\/
forward
|
bidirectional
|
random accesscode

要注意,上面這圖表並非代表它們之間的繼承關係:而只是描述了迭代器的種類和接口。處於圖表下層的迭代器都是相對於處於圖表上層迭代器的擴張集。例如:forward迭代器不但擁有input和output迭代器的全部功能,還擁有更多的功能。

好比某個算法須要一個雙向迭代器(Bidirctional Iterator),你能夠把一個任意存取迭代器(Random Access Iterator)做爲參數;但反之不行。

迭代器iterator 提供了一種通常化的方法對順序或關聯容器類型中的每一個元素進行連續訪問
例如,假設iter爲任意容器類型的一個iterator,則++iter 表示向前移動迭代器使其指向容器的下一個元素,而*iter 返回iterator 指向元素的值,每種容器類型都提供一個begin()和一個end()成員函數。
begin()返回一個iterator 它指向容器的第一個元素
end()返回一個iterator 它指向容器的末元素的下一個位置
經過迭代器,咱們能夠用相同的方式來訪問、遍歷容器。
不一樣容器提供本身的迭代器,因此不一樣迭代器具備不一樣的能力。
迭代器的做用:
  • 可以讓迭代器與算法不干擾的相互發展,最後又能無間隙的粘合起來。
  • 重載了*,++,==,!=,=運算符。用以操做複雜的數據結構。
  • 容器提供迭代器,算法使用迭代器。

各個迭代器的功能以下:

迭代器類別

說明

輸入

從容器中讀取元素。輸入迭代器只能一次讀入一個元素向前移動,輸入迭代器只支持一遍算法,同一個輸入迭代器不能兩遍遍歷一個序列

輸出

向容器中寫入元素。輸出迭代器只能一次一個元素向前移動。輸出迭代器只支持一遍算法,統一輸出迭代器不能兩次遍歷一個序列

正向

組合輸入迭代器和輸出迭代器的功能,並保留在容器中的位置

雙向

組合正向迭代器和逆向迭代器的功能,支持多遍算法

隨機訪問

組合雙向迭代器的功能與直接訪問容器中任何元素的功能,便可向前向後跳過任意個元素

迭代器的操做:

每種迭代器都可進行包括表中前一種迭代器可進行的操做。

迭代器操做

說明

全部迭代器

p++

後置自增迭代器

++p

前置自增迭代器

輸入迭代器

*p

復引用迭代器,做爲右值

p=p1

將一個迭代器賦給另外一個迭代器

p==p1

比較迭代器的相等性

p!=p1

比較迭代器的不等性

輸出迭代器

*p

復引用迭代器,做爲左值

p=p1

將一個迭代器賦給另外一個迭代器

正向迭代器

提供輸入輸出迭代器的全部功能

雙向迭代器

--p

前置自減迭代器

p--

後置自減迭代器

隨機迭代器

p+=i

將迭代器遞增i位

p-=i

將迭代器遞減i位

p+i

在p位加i位後的迭代器

p-i

在p位減i位後的迭代器

p[i]

返回p位元素偏離i位的元素引用

p<p1

若是迭代器p的位置在p1前,返回true,不然返回false

p<=p1

p的位置在p1的前面或同一位置時返回true,不然返回false

p>p1

若是迭代器p的位置在p1後,返回true,不然返回false

p>=p1

p的位置在p1的後面或同一位置時返回true,不然返回false

只有順序容器和關聯容器支持迭代器遍歷,各容器支持的迭代器的類別以下:

容器

支持的迭代器類別

說明

vector

隨機訪問

一種隨機訪問的數組類型,提供了對數組元素進行快速隨機訪問以及在序列尾部進行快速的插入和刪除操做的功能。能夠再須要的時候修改其自身的大小

deque

隨機訪問

一種隨機訪問的數組類型,提供了序列兩端快速進行插入和刪除操做的功能。能夠再須要的時候修改其自身的大小

list

雙向

一種不支持隨機訪問的數組類型,插入和刪除所花費的時間是固定的,與位置無關。

set

雙向

一種隨機存取的容器,其關鍵字和數據元素是同一個值。全部元素都必須具備唯一值。

multiset

雙向

一種隨機存取的容器,其關鍵字和數據元素是同一個值。能夠包含重複的元素。

map

雙向

一種包含成對數值的容器,一個值是實際數據值,另外一個是用來尋找數據的關鍵字。一個特定的關鍵字只能與一個元素關聯。

multimap

雙向

一種包含成對數值的容器,一個值是實際數據值,另外一個是用來尋找數據的關鍵字。一個關鍵字能夠與多個數據元素關聯。

stack

不支持

適配器容器類型,用vector,deque或list對象建立了一個先進後出容器

queue

不支持

適配器容器類型,用deque或list對象建立了一個先進先出容器

priority_queue

不支持

適配器容器類型,用vector或deque對象建立了一個排序隊列

下面列舉了些例子說明各個容器的用法:
一、vector
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <vector>
 
int main()
{
     std::vector< char > charVector;
 
     int x;
     for (x=0; x<10; ++x)
         charVector.push_back(65 + x);
 
     int size = charVector.size();
     for (x=0; x<size; ++x)
     {
         std::vector< char >::iterator start =
             charVector.begin();
         charVector.erase(start);
         std::vector< char >::iterator iter;
         for (iter = charVector.begin();
                 iter != charVector.end(); iter++)
         {
             std::cout << *iter;
         }
         std::cout << std::endl;
     }
 
     return 0;
}

二、deque

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <deque>
 
int main()
{
     std::deque< char > charDeque;
     int x;
     for (x=0; x<10; ++x)
         charDeque.push_front(65 + x);
 
     int size = charDeque.size();
     for (x=0; x<size; ++x)
     {
         std::deque< char >::iterator start =
             charDeque.begin();
         charDeque.erase(start);
         std::deque< char >::iterator iter;
         for (iter = charDeque.begin();
                 iter != charDeque.end(); iter++)
         {
             std::cout << *iter;
         }
         std::cout << std::endl;
     }
 
     return 0;
}

三、list

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <list>
 
int main()
{
     // Create and populate the list.
     int x;
     std::list< char > charList;
     for (x=0; x<10; ++x)
         charList.push_front(65 + x);
 
     // Display contents of list.
     std::cout << "Original list: " ;
     std::list< char >::iterator iter;
     for (iter = charList.begin();
             iter != charList.end(); iter++)
     {
         std::cout << *iter;
         //char ch = *iter;
         //std::cout << ch;
     }
     std::cout << std::endl;
     
     // Insert five Xs into the list.
     std::list< char >::iterator start = charList.begin();
     charList.insert(++start, 5, 'X' );
 
     // Display the result.
     std::cout << "Resultant list: " ;
     for (iter = charList.begin();
     iter != charList.end(); iter++)
     {
         std::cout << *iter;
         //char ch = *iter;
         //std::cout << ch;
     }
     
     return 0;
}

四、set

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <set>
 
int main()
{
     // Create the set object.
     std::set< char > charSet;
 
     // Populate the set with values.
     charSet.insert( 'E' );
     charSet.insert( 'D' );
     charSet.insert( 'C' );
     charSet.insert( 'B' );
     charSet.insert( 'A' );
 
     // Display the contents of the set.
     std::cout << "Contents of set: " << std::endl;
     std::set< char >::iterator iter;
     for (iter = charSet.begin(); iter != charSet.end(); iter++)
         std::cout << *iter << std::endl;
     std::cout << std::endl;
 
     // Find the D.
     iter = charSet.find( 'D' );
     if (iter == charSet.end())
         std::cout << "Element not found." ;
     else
         std::cout << "Element found: " << *iter;
 
     return 0;
}

五、multiset

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <set>
 
int main()
{
     // Create the first set object.
     std::multiset< char > charMultiset1;
 
     // Populate the multiset with values.
     charMultiset1.insert( 'E' );
     charMultiset1.insert( 'D' );
     charMultiset1.insert( 'C' );
     charMultiset1.insert( 'B' );
     charMultiset1.insert( 'A' );
     charMultiset1.insert( 'B' );
     charMultiset1.insert( 'D' );
 
     // Display the contents of the first multiset.
     std::cout << "Contents of first multiset: " << std::endl;
     std::multiset< char >::iterator iter;
     for (iter = charMultiset1.begin();
             iter != charMultiset1.end(); iter++)
         std::cout << *iter << std::endl;
     std::cout << std::endl;
 
     // Create the second multiset object.
     std::multiset< char > charMultiset2;
 
     // Populate the multiset with values.
     charMultiset2.insert( 'J' );
     charMultiset2.insert( 'I' );
     charMultiset2.insert( 'H' );
     charMultiset2.insert( 'G' );
     charMultiset2.insert( 'F' );
     charMultiset2.insert( 'G' );
     charMultiset2.insert( 'I' );
     
     // Display the contents of the second multiset.
     std::cout << "Contents of second multiset: "
         << std::endl;
     for (iter = charMultiset2.begin();
     iter != charMultiset2.end(); iter++)
         std::cout << *iter << std::endl;
     std::cout << std::endl;
     
     // Compare the sets.
     if (charMultiset1 == charMultiset2)
         std::cout << "set1 == set2" ;
     else if (charMultiset1 < charMultiset2)
         std::cout << "set1 < set2" ;
     else if (charMultiset1 > charMultiset2)
         std::cout << "set1 > set2" ;
     
     return 0;
}

六、map

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <map>
 
typedef std::map< int , char > MYMAP;
 
int main()
{
     // Create the first map object.
     MYMAP charMap1;
 
     // Populate the first map with values.
     charMap1[1] = 'A' ;
     charMap1[4] = 'D' ;
     charMap1[2] = 'B' ;
     charMap1[5] = 'E' ;
     charMap1[3] = 'C' ;
 
     // Display the contents of the first map.
     std::cout << "Contents of first map: " << std::endl;
     MYMAP::iterator iter;
     for (iter = charMap1.begin();
             iter != charMap1.end(); iter++)
     {
         std::cout << (*iter).first << " --> " ;
         std::cout << (*iter).second << std::endl;
     }
     std::cout << std::endl;
 
     // Create the second map object.
     MYMAP charMap2;
 
     // Populate the first map with values.
     charMap2[1] = 'F' ;
     charMap2[4] = 'I' ;
     charMap2[2] = 'G' ;
     charMap2[5] = 'J' ;
     charMap2[3] = 'H' ;
 
     // Display the contents of the second map.
     std::cout << "Contents of second map: " << std::endl;
     for (iter = charMap2.begin();
             iter != charMap2.end(); iter++)
     {
         std::cout << (*iter).first << " --> " ;
         std::cout << (*iter).second << std::endl;
     }
     std::cout << std::endl;
 
     // Compare the maps.
     if (charMap1 == charMap2)
         std::cout << "map1 == map2" ;
     else if (charMap1 < charMap2)
         std::cout << "map1 < map2" ;
     else if (charMap1 > charMap2)
         std::cout << "map1 > map2" ;
     
     return 0;
}

七、multimap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <map>
 
typedef std::multimap< int , char > MYMAP;
 
int main()
{
     // Create the first multimap object.
     MYMAP charMultimap;
 
     // Populate the multimap with values.
     charMultimap.insert(MYMAP::value_type(1, 'A' ));
     charMultimap.insert(MYMAP::value_type(4, 'C' ));
     charMultimap.insert(MYMAP::value_type(2, 'B' ));
     charMultimap.insert(MYMAP::value_type(7, 'E' ));
     charMultimap.insert(MYMAP::value_type(5, 'D' ));
     charMultimap.insert(MYMAP::value_type(3, 'B' ));
     charMultimap.insert(MYMAP::value_type(6, 'D' ));
 
     // Display the contents of the first multimap.
     std::cout << "Contents of first multimap: " << std::endl;
     MYMAP::iterator iter;
     for (iter = charMultimap.begin();
             iter != charMultimap.end(); iter++)
     {
         std::cout << (*iter).first << " --> " ;
         std::cout << (*iter).second << std::endl;
     }
     std::cout << std::endl;
 
     // Create the second multimap object.
     MYMAP charMultimap2;
 
     // Populate the second multimap with values.
     charMultimap2.insert(MYMAP::value_type(1, 'C' ));
     charMultimap2.insert(MYMAP::value_type(4, 'F' ));
     charMultimap2.insert(MYMAP::value_type(2, 'D' ));
     charMultimap2.insert(MYMAP::value_type(7, 'E' ));
     charMultimap2.insert(MYMAP::value_type(5, 'F' ));
     charMultimap2.insert(MYMAP::value_type(3, 'E' ));
     charMultimap2.insert(MYMAP::value_type(6, 'G' ));
 
     // Display the contents of the second multimap.
     std::cout << "Contents of second multimap: " << std::endl;
     for (iter = charMultimap2.begin();
             iter != charMultimap2.end(); iter++)
     {
         std::cout << (*iter).first << " --> " ;
         std::cout << (*iter).second << std::endl;
     }
     std::cout << std::endl;
 
     // Compare the multimaps.
     if (charMultimap == charMultimap2)
         std::cout << "multimap1 == multimap2" ;
     else if (charMultimap < charMultimap2)
         std::cout << "multimap1 < multimap2" ;
     else if (charMultimap > charMultimap2)
         std::cout << "multimap1 > multimap2" ;
     
     return 0;
}

八、stack

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <list>
#include <stack>
 
int main()
{
     std::stack< int , std::list< int > > intStack;
 
     int x;
     std::cout << "Values pushed onto stack:"
               << std::endl;
     for (x=1; x<11; ++x)
     {
         intStack.push(x*100);
         std::cout << x*100 << std::endl;
     }
 
     std::cout << "Values popped from stack:"
               << std::endl;
     int size = intStack.size();
     for (x=0; x<size; ++x)
     {
         std::cout << intStack.top() << std::endl;
         intStack.pop();
     }
 
     return 0;
}

九、queue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <list>
#include <queue>
 
int main()
{
     std::queue< int , std::list< int > > intQueue;
 
     int x;
     std::cout << "Values pushed onto queue:"
               << std::endl;
     for (x=1; x<11; ++x)
     {
         intQueue.push(x*100);
         std::cout << x*100 << std::endl;
     }
 
     std::cout << "Values removed from queue:"
               << std::endl;
     int size = intQueue.size();
     for (x=0; x<size; ++x)
     {
         std::cout << intQueue.front() << std::endl;
         intQueue.pop();
     }
 
     return 0;
}

十、priority_queue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <list>
#include <queue>
 
int main()
{
     std::priority_queue< int , std::vector< int >,std::greater< int > > intPQueue;
相關文章
相關標籤/搜索