STL vector用法介紹+ STL 常用函式用法
阿新 • • 發佈:2018-12-27
1 #include <iostream>
2 #include <deque>
3
4 using namespace std;
5
6 int main()
7 {
8 deque<int> d;
9
10 //尾部插入
11 d.push_back(1);
12 d.push_back(3);
13 d.push_back(2);
14 for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
15 {
16 cout << (*it) << " ";
17 }
18 cout << endl << endl;
19
20 //頭部插入
21 d.push_front(10);
22 d.push_front(-23);
23 for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
24 {
25 cout << (*it) << " ";
26 }
27 cout << endl << endl;
28
29 d.insert(d.begin() + 2,9999);
30 for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
31 {
32 cout << (*it) << " ";
33 }
34 cout << endl << endl;
35
36 //反方向遍歷
37 for(deque<int>::reverse_iterator rit = d.rbegin(); rit != d.rend(); ++rit )
38 {
39 cout << (*rit) << " ";
40 }
41 cout << endl << endl;
42
43 //刪除元素pop pop_front從頭部刪除元素 pop_back從尾部刪除元素 erase中間刪除 clear全刪
44 d.clear();
45 d.push_back(1);
46 d.push_back(2);
47 d.push_back(3);
48 d.push_back(4);
49 d.push_back(5);
50 d.push_back(6);
51 d.push_back(7);
52 d.push_back(8);
53 for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
54 {
55 cout << (*it) << " ";
56 }
57 cout << endl;
58
59 d.pop_front();
60 d.pop_front();
61 for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
62 {
63 cout << (*it) << " ";
64 }
65 cout << endl;
66
67 d.pop_back();
68 d.pop_back();
69 for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
70 {
71 cout << (*it) << " ";
72 }
73 cout << endl;
74
75 d.erase(d.begin() + 1);
76 for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
77 {
78 cout << (*it) << " ";
79 }
80 cout << endl;
81 return 0;
82 }