Vector类 是在 java 中可以实现自动增长的对象数组,vector在C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库。
1 #include <iostream>
2 #include <vector>
3 using namespace std;
4
5 void printVector(vector<int>& v)
6 {
7 for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
8 {
9 cout << *it << " ";
10 }
11 cout << endl;
12 }
13
14 // 初始化
15 // vector<T> v; //采用模板实现类实现,默认构造函数
16 // vector(v.begin(), v.end());//将 v[begin(), end())区间中的元素拷贝给本身。
17 // vector(n, elem);//构造函数将 n 个 elem 拷贝给本身。
18 // vector(const vector &vec);//拷贝构造函数。
19 void test01()
20 {
21 vector<int> v1; // 默认构造
22 int arr[] = { 10, 20, 30, 40 };
23 vector<int> v2(arr, arr + sizeof(arr) / sizeof(int));
24 vector<int> v3(v2.begin(), v2.end());
25 vector<int> v4(v3);
26
27 printVector(v2);
28 printVector(v3);
29 printVector(v4);
30 cout << "------------------" << endl;
31 }
32
33 // 常用赋值操作
34 // assign(beg, end);//将[beg, end)区间中的数据拷贝赋值给本身。
35 // assign(n, elem);//将 n 个 elem 拷贝赋值给本身。
36 // vector& operator=(const vector &vec);//重载等号操作符
37 // swap(vec);// 将 vec 与本身的元素互换
38 void test02()
39 {
40 int arr[] = { 10, 20, 30, 40 };
41 vector<int> v11(arr, arr + sizeof(arr) / sizeof(int));
42 // 成员方法
43 vector<int> v2;
44 v2.assign(v11.begin(), v11.end());
45 // 重载=
46 vector<int> v3;
47 v3 = v2;
48
49 int arr1[] = { 100, 200, 300, 400 };
50 vector<int> v4(arr1, arr1 + sizeof(arr1) / sizeof(int));
51
52 printVector(v11);
53 printVector(v2);
54 printVector(v3);
55 printVector(v4);
56
57 cout << "------------------" << endl;
58
59 v4.swap(v11);
60 printVector(v11);
61 printVector(v2);
62 printVector(v3);
63 printVector(v4);
64 cout << "------------------" << endl;
65 }
66
67 // 大小操作
68 // size();//返回容器中元素的个数
69 // empty();//判断容器是否为空
70 // resize(int num);//重新指定容器的长度为 num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
71 // resize(int num, elem);//重新指定容器的长度为 num,若容器变长,则以 elem 值填充新位置。如果容器变短,则末尾超出容器长 > 度的元素被删除。
72 // capacity();//容器的容量
73 // reserve(int len);//容器预留 len 个元素长度,预留位置不初始化,元素不可访问。
74 void test03()
75 {
76 int arr[] = {100, 200, 300, 400};
77 vector<int> v4(arr, arr + sizeof(arr) / sizeof(int));
78
79 cout << "size: " << v4.size() << endl;
80 if (v4.empty() == true)
81 {
82 cout << "空!" << endl;
83 }
84 else
85 {
86 cout << "不空!" << endl;
87 }
88 printVector(v4);
89 v4.resize(2);
90 printVector(v4);
91 v4.resize(6);
92 printVector(v4);
93 v4.resize(8, 1);
94 printVector(v4);
95 for (int i = 0; i < 10000; i++)
96 {
97 v4.push_back(i);
98 }
99 cout << "size: " << v4.size() << endl;
100 cout << "容量: " << v4.capacity() << endl;
101 cout << "------------------" << endl;
102 }
103
104 // vector存取数据
105 // at(int idx); //返回索引 idx 所指的数据,如果 idx 越界,抛出 out_of_range 异常。
106 // operator[];//返回索引 idx 所指的数据,越界时,运行直接报错
107 // front();//返回容器中第一个数据元素
108 // back();//返回容器中最后一个数据元素
109 void test04()
110 {
111 int arr[] = { 100, 200, 300, 400 };
112 vector<int> v4(arr, arr + sizeof(arr) / sizeof(int));
113
114 for (int i = 0; i < v4.size(); i++)
115 {
116 cout << v4[i] << " ";
117 }
118 cout << endl;
119 for (int i = 0; i < v4.size(); i++)
120 {
121 cout << v4.at(i) << " ";
122 }
123 cout << endl;
124 // 区别:at抛异常 []不抛异常
125 cout << "front: " << v4.front() << endl;
126 cout << "back: " << v4.back() << endl;
127 cout << "------------------" << endl;
128 }
129
130 // 插入和删除
131 // insert(const_iterator pos, int count, ele);//迭代器指向位置 pos 插入 count 个元素 ele.
132 // push_back(ele); //尾部插入元素 ele
133 // pop_back();//删除最后一个元素
134 // erase(const_iterator start, const_iterator end);//删除迭代器从 start 到 end 之间的元素
135 // erase(const_iterator pos);//删除迭代器指向的元素
136 // clear();//删除容器中所有元素
137 void test05()
138 {
139 vector<int> v;
140 v.push_back(10);
141 v.push_back(20);
142 // 头插法
143 v.insert(v.begin(), 30);
144 v.insert(v.end(), 40);
145 printVector(v);
146 v.insert(v.begin() + 2, 100); // vector支持随机访问
147 printVector(v);
148 // 删除
149 v.erase(v.begin());
150 printVector(v);
151 v.erase(v.begin() + 1, v.end());
152 printVector(v);
153 v.clear();
154 cout << "size: " << v.size() << endl;
155 cout << "------------------" << endl;
156 }
157
158 // 巧用swap缩减空间
159 // vector添加元素 他会自动增长 你删除元素时候,会自动减少吗?
160 void test06()
161 {
162 vector<int> v;
163 for (int i = 0; i < 100000; i++)
164 {
165 v.push_back(i);
166 }
167 cout << "size: " << v.size() << endl;
168 cout << "capacity: " << v.capacity() << endl;
169 v.resize(10);
170 cout << "------------------" << endl;
171 cout << "size: " << v.size() << endl;
172 cout << "capacity: " << v.capacity() << endl;
173 // 收缩空间
174 vector<int>(v).swap(v);
175 cout << "------------------" << endl;
176 cout << "size: " << v.size() << endl;
177 cout << "capacity: " << v.capacity() << endl;
178 cout << "------------------" << endl;
179 }
180
181 // reserve 预留空间 resize区别
182 // 问:reserv 和 resize 的区别 ?
183 // 答 : reserve 是容器预留空间,但在空间内不真正创建元素对象,所以在没有添加新的对
184 // 象之前,不能引用容器内的元素.resize 是改变容器的大小,且在创建对象,因此,调用这个函数之后,就可以引用容
185 // 器内的对象了
186 void test07()
187 {
188 int num = 0;
189 int* address = NULL;
190 vector<int> v;
191 // v.reserve(100000);
192 for (int i = 0; i < 100000; i++)
193 {
194 v.push_back(i);
195 if (address != &(v[0]))
196 {
197 address = &(v[0]);
198 num++;
199 }
200 }
201 cout << "num: " << num << endl;
202 cout << "------------------" << endl;
203 // 如果你知道容器大概要存储的元素个数,那么你可以用reserve预留空间
204 }
205
206 int main()
207 {
208 test01();
209 test02();
210 test03();
211 test04();
212 test05();
213 test06();
214 test07();
215 getchar();
216 return 0;
217 }