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 }

猜你喜欢

转载自www.cnblogs.com/Long-w/p/9816107.html