1. 程式人生 > >2、【C++ STL】容器之序列式容器

2、【C++ STL】容器之序列式容器

一、Vector

(1)將元素置於一個動態陣列中加以管理。

(2)可以隨機存取元素(用索引位元組存取)

(3)陣列尾部新增或移除元素非常快速。當在頭部或中部安插元素比較費時。

【示例】

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     vector<int> coll;//vector container for integer
 9 
10     //append elements with values 1 to 6
11 for(int i = 1; i <= 6; i++) 12 { 13 coll.push_back(i);//add elements from end 14 } 15 16 //print all elements followed by a space 17 for(int i = 0; i < coll.size(); i++) 18 { 19 cout << coll[i] << " "; 20 } 21 22 cout << endl;
23 }

【vector綜合示例】

  1 #include <iostream>
  2 #include <vector>
  3 #include <algorithm>
  4 
  5 using namespace std;
  6 
  7 //add elements
  8 void add(vector<int> &vec)
  9 {
 10     int temp, N;
 11     cout << "Please input the size of vec:";
 12     cin >> N;
13 14 cout << "請輸入" << N << "個整數(空格分割,換行結束):"; 15 for(int i = 0; i < N; i++) 16 { 17 cin >> temp; 18 vec.push_back(temp); 19 } 20 } 21 22 //inset new elements 23 void insertElements(vector<int> &vec) 24 { 25 int index, num; 26 cout << "請輸入要插入的位置:"; 27 cin >> index; 28 cout << "請輸入要插入的值:"; 29 cin >> num; 30 vec.insert(vec.begin()+index, num); 31 } 32 33 //delete elements 34 void deleteElements(vector<int> &vec) 35 { 36 int i, place, from, to; 37 38 cout << " ***** 1、刪除1個元素 2、刪除部分元素 3、刪除所有元素 ***** " << endl; 39 cout << "請輸入選單編號:" << endl; 40 41 cin >> i; 42 switch(i) 43 { 44 case 1: 45 { 46 cout << "請輸入要刪除的元素索引:" << endl; 47 cin >> place; 48 if(place < 0 || place+1 > vec.size()) 49 cout << "error" << endl; 50 else 51 vec.erase(vec.begin()+place); 52 } 53 break; 54 case 2: 55 { 56 cout << "請輸入要刪除部分的起始位置(結束位置元素不刪除):" << endl; 57 cin >> from >> to; 58 if(from < 0 || to+1 > vec.size()) 59 cout << "error" << endl; 60 else 61 vec.erase(vec.begin()+from, vec.begin()+to);//刪除區間內,包含from不包含to 62 } 63 break; 64 case 3: 65 { 66 vec.clear(); 67 } 68 break; 69 } 70 } 71 72 //sort all elements 73 void Sort(vector<int> &vec) 74 { 75 int i; 76 cout << " *** 1、升序 2、降序 *** " << endl; 77 cin >> i; 78 switch(i) 79 { 80 case 1:sort(vec.begin(), vec.end());break; 81 case 2:reverse(vec.begin(), vec.end());break; 82 } 83 } 84 85 //使用迭代器來遍歷vector的元素 86 void Display(vector<int> &vec) 87 { 88 vector<int>::iterator it;//vector 迭代器的使用 89 for(it = vec.begin(); it != vec.end(); it++) 90 { 91 cout << *it << " "; 92 } 93 cout << endl; 94 } 95 //修改某個位置的元素 96 void Change(vector<int> &vec) 97 { 98 int place; 99 cout << "請輸入要修改元素的位置"; 100 cin >> place; 101 cout << "請輸入新的資料值:"; 102 cin >> vec[place]; 103 } 104 105 //print all elements 106 void print(vector<int> &vec) 107 { 108 for(int i = 0; i < vec.size(); i++) 109 { 110 cout << vec[i] << " "; 111 } 112 cout << endl; 113 } 114 115 int main() 116 { 117 vector<int> vec; 118 119 add(vec); 120 print(vec); 121 122 insertElements(vec); 123 Display(vec); 124 125 deleteElements(vec); 126 print(vec); 127 128 Change(vec); 129 Display(vec); 130 131 return 0; 132 }

二、Deque

  (1)deque是“double-ended queue的縮寫

  (2)可以隨機存取元素(用索引直接存取)

  (3)陣列頭部和尾部新增或移除元素都非常快,當在中部或頭部安插元素比較費時。

  雙端佇列(deque),顧名思義,兩端都可以操作,插入和刪除。而且,還可以在中間進行操作。內部採用線性表順序結構,與vector不同的是,deque採用分塊的線性儲存結構儲存資料,每塊大小512位元組。
  所有的deque塊使用一個Map塊進行管理,每個Map資料項紀錄各個deque塊的首地址。當考慮容器內部的記憶體分配策略和操作效能時,deque相對於vector更有優勢,同時,也可以用下標來訪問。

【示例】

 1 #include <iostream>
 2 #include <deque>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     deque<float> coll;//deque container for floating-point
 9 
10     //insert elements from 1.1 to 6.6 each at the front
11     for(int i = 1; i <= 6; i++)
12     {
13         coll.push_front(i*1.1);// insert at the front
14     }
15 
16     //print all elements followed by a space
17     for(int i = 0; i < coll.size(); i++)
18     {
19         cout << coll[i] << "  ";
20     }
21 
22     cout << endl;
23 
24     return 0;
25 }

【deque綜合示例】

  1 #include <iostream>
  2 #include <deque>
  3 
  4 using namespace std;
  5 
  6 
  7 int Push(deque<int> &deq)
  8 {
  9     int i, j, place, n, N;
 10 
 11     cout << "1、前插 2、後插  3、中間插" << endl;
 12     cin >> j;
 13     switch(j)
 14     {
 15     case 1://前插,後移
 16         {
 17             cout << "請輸入要插入的整數個數:" << endl;
 18             cin >> N;
 19             cout << "請輸入" << N << "個整數:" << endl;
 20             for(i = 0; i < N; i++)
 21             {
 22                 cin >> n;
 23                 deq.push_front(n);
 24             }
 25         }
 26         break;
 27     case 2://後插,擴張佇列
 28         {
 29             cout << "請輸入要輸入的整數個數:" << endl;
 30             cin >> N;
 31             cout << "請輸入" << N << "個整數:" << endl;
 32             for(i = 0; i < N; i++)
 33             {
 34                 cin >> n;
 35                 deq.push_back(n);
 36             }
 37         }
 38         break;
 39     case 3://中間插入,後移
 40         {
 41             cout << "請輸入要插入的位置:" << endl;
 42             cin >> place;
 43             cout << "請輸入要插入的數值:" << endl;
 44             cin >> n;
 45             deq.insert(deq.begin()+place, n);
 46         }
 47         break;
 48     default:
 49         cout << "error" << endl;
 50     }
 51 }
 52 void POP(deque<int> &deq)
 53 {
 54     int i, j, place, N;
 55     cout << "1、前刪  2、後刪  3、中間刪  4、清空" << endl;
 56     cin >> j;
 57     switch(j)
 58     {
 59     case 1://前刪
 60         cout << "請輸入要刪除的整數的個數:" << endl;
 61         cin >> N;
 62         if(N > deq.size())
 63             cout << "error" << endl;
 64         else
 65         {
 66             for(i = 0; i < N; i++)
 67             {
 68                 deq.pop_front();
 69             }
 70         }
 71         break;
 72     case 2://後刪
 73         cout << "請輸入你要刪除的整數個數:" << endl;
 74         cin >> N;
 75         if(N > deq.size())
 76             cout << "error" << endl;
 77         else
 78         {
 79             for(i = 0; i < N; i++)
 80                 deq.pop_back();
 81         }
 82         break;
 83     case 3://中刪
 84         cout << "前輸入你要刪除的位置(首位置為0):" << endl;
 85         cin >> place;
 86         if(place < 0 || place > deq.size())
 87             cout << "位置越界" << endl;
 88         else
 89         {
 90             deq.erase(deq.begin()+place);
 91         }
 92         break;
 93     default:
 94         cout << "輸入錯誤" << endl;
 95     }
 96 }
 97 
 98 void getFront(deque<int> &deq)
 99 {
100     if(deq.empty())
101         cout << "the deque is empty" << endl;
102     else
103         cout << "佇列頂部元素為:" << deq.front() << endl;
104 }
105 
106 void getBack(deque<int> &deq)
107 {
108     if(deq.empty())
109         cout << "the deque is empty" << endl;
110     else
111         cout << "the back of the deuqe is :" << deq.back() << endl;
112 }
113 
114 void getSize(deque<int> &deq)
115 {
116     cout << "the size of the deque is : " << deq.size() << endl;
117 }
118 
119 void Display(deque<int> &deq)
120 {
121     for(int i = 0; i < deq.size(); i++)
122         cout << deq[i] << "  ";
123     cout << endl;
124 }
125 
126 int main()
127 {
128     deque<int> deq;
129 
130     Push(deq);
131     Display(deq);
132     getSize(deq);
133     getFront(deq);
134     getBack(deq);
135 
136     POP(deq);
137     Display(deq);
138 
139 
140     return 0;
141 }

三、List

  (1)雙向連結串列

  (2)不提供隨機存取(按順序走到需要存取的元素,O(n))

  (3)在任何位置上執行插入和刪除動作都非常塊,內部只需要調整一下指標。

  list是雙向連結串列,有vector,deque的特徵,而且效率高。它有插入(前插,後插,中間插),刪除(前刪,後刪,清空等),排序等功能。而且,可以剔除連續相同元素,保留一個。

【示例】

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     list<char> coll;//list container for character
 9 
10     //append elements from 'a' to 'z'
11     for(char c = 'a'; c <= 'z'; c++)
12     {
13         coll.push_back(c);
14     }
15 
16     //print all elements
17     while(!coll.empty())
18     {
19         cout << coll.front() << "  ";
20         coll.pop_front();
21     }
22     cout << endl;
23 
24     return 0;
25 }

【綜合示例】

  1 #include <iostream>
  2 #include <list>
  3 #include <algorithm>
  4 
  5 using namespace std;
  6 
  7 void Push(list<int> &mlist)
  8 {
  9     int j, place, n, N;
 10     list<int>::iterator it;
 11     cout << "  1、前插  2、後插  3、中間插入   " << endl;
 12 
 13     cin >> j;
 14     switch(j)
 15     {
 16     case 1://前插,向後擴張
 17         cout << "請輸入你要輸入的整數個數:";
 18         cin >> N;
 19         cout << "請輸入" << N << "個整數:" << endl;
 20         for(int i = 0; i < N; i++)
 21         {
 22             cin >> n;
 23             mlist.push_front(n);
 24         }
 25         break;
 26     case 2:
 27         cout << "請輸入你要輸入的整數個數:";
 28         cin >> N;
 29         cout << "請輸入" << N << "個整數:" << endl;
 30         for(int i = 0; i < N; i++)
 31         {
 32             cin >> n;
 33             mlist.push_back(n);
 34         }
 35         break;
 36     case 3:
 37         cout << "請輸入你要插入的位置:";
 38         cin >> place;
 39         cout << "請輸入你要插入的數值:" << endl;
 40         cin >> n;
 41 
 42         it = mlist.begin();
 43         while(place)
 44         {
 45             it++;
 46             place--;
 47         }
 48         mlist.insert(it, n);
 49     default:
 50         cout << "error" << endl;
 51     }
 52 }
 53 
 54 void Pop(list<int> &mlist)
 55 {
 56     int j, place, N;
 57     list<int>::iterator it;
 58     cout << "   1、前刪  2、後刪   3、中間刪  4、清空   " << endl;
 59 
 60     cin >> j;
 61 
 62     switch(j)
 63     {
 64     case 1:
 65         cout << "請輸入你要刪除的整數個數:" << endl;
 66         cin >> N;
 67 
 68         if(N > mlist.size())
 69             cout << "越界" << endl;
 70         else
 71         {
 72             for(int i = 0; i < N; i++)
 73                 mlist.pop_front();
 74         }
 75         break;
 76     case 2:
 77         cout << "請輸入你要刪除的整數的個數:" << endl;
 78         cin >> N;
 79         if(N > mlist.size())
 80             cout << "越界" << endl;
 81         else
 82         {
 83             for(int i = 0; i < N; i++)
 84                 mlist.pop_back();
 85         }
 86         break;
 87     case 3:
 88         cout << "請輸入你要刪除的位置:" << endl;
 89         cin >> place;
 90         if(place < 0 || place > mlist.size())
 91             cout << "越界" << endl;
 92         else
 93         {
 94             it = mlist.begin();
 95             while(place)
 96             {
 97                 it++;
 98                 place--;
 99             }
100         }
101         break;
102     case 4:
103         mlist.clear();
104         break;
105     default:
106         cout << "error" << endl;
107     }
108 }
109 
110 void getFront(list<int> &mlist)
111 {
112     if(mlist.empty())
113         cout << "list is empty" << endl;
114     else
115         cout << "list front element is: " << mlist.front() << endl;
116 }
117 
118 void getBack(list<int> &mlist)
119 {
120     if(mlist.empty())
121         cout << "the list is empty" << endl;
122     else
123         cout << "list back element is: " << mlist.back() << endl;
124 }
125 
126 void getSize(list<int> &mlist)
127 {
128     cout << "the size of the list is: " << mlist.size() << endl;
129 }
130 
131 void Sort(list<int> &mlist)
132 {
133     mlist.sort();//升序
134     mlist.reverse();//升序後降序
135 }
136 
137 void Search(list<int> &mlist)
138 {
139     int n;
140     cout << "請輸入你想要查詢的數:" << endl;
141 
142     cin >> n;
143 
144     list<int>::iterator it;
145     it = find(mlist.begin(), mlist.end(), n);
146     if(it != mlist.end())
147     {
148         int sum = 0;
149         list<int>::iterator temp;
150         temp = mlist.begin();
151         while((*temp) != (*it))
152         {
153             sum++;
154             temp++;
155         }
156         cout << n << "是第" << sum << "個數。" << endl;
157     }
158     else
159         cout << n << "is not in the list" << endl;
160 }
161 //去重
162 void Unique(list<int> &mlist)
163 {
164     mlist.unique();
165 }
166 
167 //列印
168 void Display(list<int> &mlist)
169 {
170     int i;
171     list<int>::iterator it;//list的迭代器
172     list<int>::reverse_iterator rit;//list的反向迭代器
173     cout << "   1、前向遍歷   2、反向遍歷    " << endl;
174     cin >> i;
175     switch(i)
176     {
177     case 1:
178         for(it = mlist.begin(); it != mlist.end(); it++)
179         {
180             cout << *it << "  ";
181         }
182         cout << endl;
183         break;
184     case 2:
185         for(rit = mlist.rbegin(); rit != mlist.rend(); rit++)
186             cout << *rit << "  ";
187         cout << endl;
188         break;
189     default:
190         cout << "error" << endl;
191     }
192 }
193 
194 int main()
195 {
196     list<int> mlist;
197 
198     Push(mlist);
199     Display(mlist);
200 
201     Pop(mlist);
202     Display(mlist);
203 
204     getFront(mlist);
205     getBack(mlist);
206     Sort(mlist);
207     Display(mlist);
208 
209     Search(mlist);
210     Unique(mlist);
211     Display(mlist);
212 
213 
214 
215     return 0;
216 
217 }