线性存储与链式存储迭代器的不同

 1 /* 线性存储与链式存储迭代器的不同 */
 2 
 3 /* 容器:
 4     <
 5     array , vector, list, deque, stack, queue,set,
 6     mutset, hash_set, hashmap, map, string, bitset
 7     > 
 8 */
 9 
10 
11 
12 #include<iostream>
13 #include<vector>
14 
15 using namespace std;
16 
17 // vector
18 void main()
19 {
20     vector<int> myv = {1,2,3,4,5};
21     // 迭代器的本质就是智能指针,可以随机访问
22     for(auto ib = myv.begin()+2,ie = myv.end();ib!=ie;ib++)
23     {
24         cout << *ib << endl;
25     }
26     
27     // 反向迭代
28     for(auto rb = myv.rbegin()+2,re = myv.rend();rb!=re;rb++)
29     {
30         cout << *rb << endl;
31     }
32 
33     cin.get();
34 }
35 
36 //------------------------------------------------------------------------
37 
38 // list
39 #include<iostream>
40 #include<list>
41 
42 using namespace std;
43 void main()
44 {
45     list<char> mylist = {'A','B','C','D','E'};
46     
47     // +2,不可以,list是链表无法直接访问
48     // list迭代器无法随机访问
49     for(auto ib = mylist.begin()+2,ie = mylist.end();ib!=ie;ib++)
50     {
51         cout << *ib << endl;
52     }
53 
54     // +2 list 迭代器不能随机访问
55     for(auto rb = mylist.rbegin()+2,re = mylist.rend();rb!=re;rb++)
56     {
57         cout << *rb << endl;
58     }
59     
60     cin.get();
61 }
62 
63 
64 //------------------------------------------------------------------------
65 
66 // array
67 #include<iostream>
68 #include<array>
69 
70 using namespace std;
71 void main()
72 {
73     array<int,10> my = {1,2,3,4,5,6,7,8,9,10};
74     // +5 可以 
75     for(auto ib = my.begin()+5,ie = my.end();ib!=ie;ib++)
76     {
77         cout << *ib << endl;
78     }
79 
80     cin.get();
81 }

 

posted on 2015-06-14 08:56  Dragon-wuxl  阅读(158)  评论(0)    收藏  举报

导航