欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

vector中resize与reserve的区别

程序员文章站 2022-03-21 21:16:38
...

vector中resize与reserve的区别

resize()

resize() 函数的作用是改变 vector 元素个数。

resize(n,m) 第二个参数可以省略,n 代表改变元素个数为 n ,m 代表初始化为 m 。

  • resize() 可以改变有效空间的大小。
  • 如果 n 比 vector 的 size 小,结果是 size 减小到 n ,然后删除 n 之后的数据。
示例:
void test1() {
	vector<int> v;
	v.reserve(20);
	v.resize(10);
	cout << "vector.size()     = " << v.size() << endl;
	cout << "vector.capacity() = " << v.capacity() << endl;
	for (int i = 0; i < v.size(); i++) {
		cout << v[i] << " ";
	}
	cout << endl;
	v.resize(5);
	cout << "vector.size()     = " << v.size() << endl;
	cout << "vector.capacity() = " << v.capacity() << endl;
	for (int i = 0; i < v.size(); i++) {
		cout << v[i] << " ";
	}
	cout << endl;
}
输出:
vector.size()     = 10
vector.capacity() = 20
0 0 0 0 0 0 0 0 0 0
vector.size()     = 5
vector.capacity() = 20
0 0 0 0 0
  • 如果 n 比 vector 的 size 大,比 vector 的 capacity 小,结果是增加 size 并初始化,如果指定了第二参数,则初始化为指定值,没指定初始化则为缺省值,capacity 不变。
示例:
void test2() {
	vector<int> v;
	v.reserve(20);
	v.resize(10);
	cout << "vector.size()     = " << v.size() << endl;
	cout << "vector.capacity() = " << v.capacity() << endl;
	for (int i = 0; i < v.size(); i++) {
		cout << v[i] << " ";
	}
	cout << endl;
	v.resize(15, 1);//v.resize(15)
	cout << "vector.size()     = " << v.size() << endl;
	cout << "vector.capacity() = " << v.capacity() << endl;
	for (int i = 0; i < v.size(); i++) {
		cout << v[i] << " ";
	}
	cout << endl;
}
输出:
vector.size()     = 10
vector.capacity() = 20
0 0 0 0 0 0 0 0 0 0
vector.size()     = 15
vector.capacity() = 20
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1
  • 如果 n 比 vector 中的 capacity 大,结果是先增加容量,然后增加 size 并初始化。capacity 和 size 均改变。
示例:
void test1() {
	vector<int> v;
	v.reserve(20);
	v.resize(10);
	cout << "vector.size()     = " << v.size() << endl;
	cout << "vector.capacity() = " << v.capacity() << endl;
	for (int i = 0; i < v.size(); i++) {
		cout << v[i] << " ";
	}
	cout << endl;
	v.resize(30, 1);//v.resize(30);
	cout << "vector.size()     = " << v.size() << endl;
	cout << "vector.capacity() = " << v.capacity() << endl;
	for (int i = 0; i < v.size(); i++) {
		cout << v[i] << " ";
	}
	cout << endl;
}
输出:
vector.size()     = 10
vector.capacity() = 20
0 0 0 0 0 0 0 0 0 0
vector.size()     = 30
vector.capacity() = 30
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

reserve()

reserve() 函数的作用是改变总空间的大小。

reverse(n)只有一个参数。

  • 如果 n 的值比大于 vector 的 size,则 capacity 增容到 n ,size 不变。
示例:
void test1() {
	vector<int> v;
	v.reserve(20);
	v.resize(10);
	cout << "vector.size()     = " << v.size() << endl;
	cout << "vector.capacity() = " << v.capacity() << endl;
	for (int i = 0; i < v.size(); i++) {
		cout << v[i] << " ";
	}
	cout << endl;
	v.reserve(30);
	cout << "vector.size()     = " << v.size() << endl;
	cout << "vector.capacity() = " << v.capacity() << endl;
	for (int i = 0; i < v.size(); i++) {
		cout << v[i] << " ";
	}
	cout << endl;
}
输出:
vector.size()     = 10
vector.capacity() = 20
0 0 0 0 0 0 0 0 0 0
vector.size()     = 10
vector.capacity() = 30
0 0 0 0 0 0 0 0 0 0
  • 如果 n 的值比小于或者等于 vector 的 size ,则 capacity 不变,size 也不变。
示例:
void test1() {
	vector<int> v;
	v.reserve(20);
	v.resize(10);
	cout << "vector.size()     = " << v.size() << endl;
	cout << "vector.capacity() = " << v.capacity() << endl;
	for (int i = 0; i < v.size(); i++) {
		cout << v[i] << " ";
	}
	cout << endl;
	v.reserve(10);
	cout << "vector.size()     = " << v.size() << endl;
	cout << "vector.capacity() = " << v.capacity() << endl;
	for (int i = 0; i < v.size(); i++) {
		cout << v[i] << " ";
	}
	cout << endl;
}
输出:
vector.size()     = 10
vector.capacity() = 20
0 0 0 0 0 0 0 0 0 0
vector.size()     = 10
vector.capacity() = 20
0 0 0 0 0 0 0 0 0 0
相关标签: C++