vector的resize()和reserve()


一、当vector对象没有进行初始化时

#include<iostream>
#include<vector>
using namespace std;

int main()
{
	vector<int> test;
	cout << "initial:" << endl;
	cout << "initial size():" << test.size() << endl;
	cout << "initial capacity():" << test.capacity() << endl;
	cout << endl;
	
	return 0;
}

运行结果:

initial:
initial size():0
initial capacity():0

结论:如果没有对vector进行初始化,其size()和capacity()都为0。

二、capacity()和size()

运行以下代码:

#include<iostream>
#include<vector>
using namespace std;

int main()
{
	vector<int> test;
	test.reserve(20);
	cout << "after reserve(20):" << endl;
	cout << "size:" << test.size() << endl;
	cout << "capacity:" << test.capacity() << endl;
	cout << endl;
/************对应输出*******************/
after reserve(20):
size:0
capacity:20
/***************************************/

	test.resize(15);
	cout << "after resize(15):" << endl;
	cout << "size:" << test.size() << endl;
	cout << "capacity:" << test.capacity() << endl;
	cout << "test[0]=" << test[0] << endl;
	cout << "test[1]=" << test[1] << endl;
	cout << endl;
/************对应输出*******************/
after resize(15):
size:15
capacity:20
test[0]=0
test[1]=0
/***************************************/

	test.reserve(10);
	cout << "after reserve(10):" << endl;
	cout << "size:" << test.size() << endl;
	cout << "capacity:" << test.capacity() << endl;
	cout << "test[0]=" << test[0] << endl;
	cout << "test[1]=" << test[1] << endl;
	cout << endl;
/************对应输出*******************/
after reserve(10):
size:15
capacity:20
test[0]=0
test[1]=0
/***************************************/


	test.reserve(16);
	cout << "after reserve(16):" << endl;
	cout << "size:" << test.size() << endl;
	cout << "capacity:" << test.capacity() << endl;
	cout << "test[0]=" << test[0] << endl;
	cout << "test[1]=" << test[1] << endl;
	cout << endl;
/************对应输出*******************/
after reserve(16):
size:15
capacity:20
test[0]=0
test[1]=0
/***************************************/

	test.resize(17,2);
	cout << "after resize(17,2):" << endl;
	cout << "size:" << test.size() << endl;
	cout << "capacity:" << test.capacity() << endl;
	cout << "test[0]=" << test[0] << endl;
	cout << "test[1]=" << test[1] << endl;
	cout << "test[16]=" << test[16] << endl;
	cout << endl;
/************对应输出*******************/
after resize(17,2):
size:17
capacity:20
test[0]=0
test[1]=0
test[16]=2
/***************************************/

	test.resize(21, 2);
	cout << "after resize(21,2):" << endl;
	cout << "size:" << test.size() << endl;
	cout << "capacity:" << test.capacity() << endl;
	cout << "test[0]=" << test[0] << endl;
	cout << "test[1]=" << test[1] << endl;
	cout << "test[16]=" << test[16] << endl;
	cout << endl;
/************对应输出*******************/
after resize(21,2):
size:21
capacity:30
test[0]=0
test[1]=0
test[16]=2
/***************************************/

	test.reserve(35);
	cout << "after reserve(35):" << endl;
	cout << "size:" << test.size() << endl;
	cout << "capacity:" << test.capacity() << endl;
	cout << "test[0]=" << test[0] << endl;
	cout << "test[1]=" << test[1] << endl;
	cout << "test[16]=" << test[16] << endl;
/************对应输出*******************/
after reserve(35):
size:21
capacity:35
test[0]=0
test[1]=0
test[16]=2
/***************************************/

	return 0;
}

访问越界:
在这里插入图片描述
结论:

capacity:该值在容器初始化时赋值,指的是容器能够容纳的最大的元素的个数。还不能通过下标等访问,因为此时容器中还没有创建任何对象。
size:指的是此时容器中实际的元素个数。可以通过下标访问0-(size-1)范围内的对象

三、resize()和reserve()

运行以下代码:

#include<iostream>
#include<vector>
using namespace std;

int main()
{
	vector<int> test(15, 2);
	cout << "initial:" << endl;
	cout << "initial size():" << test.size() << endl;
	cout << "initial capacity():" << test.capacity() << endl;
	cout << endl;
/************对应输出*******************/
initial:
initial size():15
initial capacity():15
/***************************************/

	test.reserve(20);
	cout << "after reserve(20):" << endl;
	cout << "size:" << test.size() << endl;
	cout << "capacity:" << test.capacity() << endl;
	cout << endl;
/************对应输出*******************/
after reserve(20):
size:15
capacity:20
/***************************************/

	test.resize(15);
	cout << "after resize(15):" << endl;
	cout << "size:" << test.size() << endl;
	cout << "capacity:" << test.capacity() << endl;
	cout << "test[0]=" << test[0] << endl;
	cout << "test[1]=" << test[1] << endl;
	cout << endl;
/************对应输出*******************/
after resize(15):
size:15
capacity:20
test[0]=2
test[1]=2
/***************************************/

	test.reserve(10);
	cout << "after reserve(10):" << endl;
	cout << "size:" << test.size() << endl;
	cout << "capacity:" << test.capacity() << endl;
	cout << "test[0]=" << test[0] << endl;
	cout << "test[1]=" << test[1] << endl;
	cout << endl;
/************对应输出*******************/
after reserve(10):
size:15
capacity:20
test[0]=2
test[1]=2
/***************************************/

	test.reserve(16);
	cout << "after reserve(16):" << endl;
	cout << "size:" << test.size() << endl;
	cout << "capacity:" << test.capacity() << endl;
	cout << "test[0]=" << test[0] << endl;
	cout << "test[1]=" << test[1] << endl;
	cout << endl;
/************对应输出*******************/
after reserve(16):
size:15
capacity:20
test[0]=2
test[1]=2
/***************************************/

	test.resize(17,6);
	cout << "after resize(17,6):" << endl;
	cout << "size:" << test.size() << endl;
	cout << "capacity:" << test.capacity() << endl;
	cout << "test[0]=" << test[0] << endl;
	cout << "test[1]=" << test[1] << endl;
	cout << "test[16]=" << test[16] << endl;
	cout << endl;
/************对应输出*******************/
after resize(17,6):
size:17
capacity:20
test[0]=2
test[1]=2
test[16]=6
/***************************************/

	test.resize(21, 5);
	cout << "after resize(21,5):" << endl;
	cout << "size:" << test.size() << endl;
	cout << "capacity:" << test.capacity() << endl;
	cout << "test[0]=" << test[0] << endl;
	cout << "test[1]=" << test[1] << endl;
	cout << "test[16]=" << test[16] << endl;
	cout << endl;
/************对应输出*******************/
after resize(21,5):
size:21
capacity:30
test[0]=2
test[1]=2
test[16]=6
/***************************************/

	test.reserve(35);
	cout << "after reserve(35):" << endl;
	cout << "size:" << test.size() << endl;
	cout << "capacity:" << test.capacity() << endl;
	cout << "test[0]=" << test[0] << endl;
	cout << "test[1]=" << test[1] << endl;
	cout << "test[16]=" << test[16] << endl;
/************对应输出*******************/
after reserve(35):
size:21
capacity:35
test[0]=2
test[1]=2
test[16]=6
/***************************************/

test.reserve(38);
cout << "after reserve(38):" << endl;
cout << "size:" << test.size() << endl;
cout << "capacity:" << test.capacity() << endl;
cout << "test[0]=" << test[0] << endl;
cout << "test[1]=" << test[1] << endl;
cout << "test[17]=" << test[17] << endl;
/************对应输出*******************/
after reserve(38):
size:21
capacity:38
test[0]=2
test[1]=2
test[17]=5
/***************************************/

test.resize(10);
cout << "after resize(10):" << endl;
cout << "size:" << test.size() << endl;
cout << "capacity:" << test.capacity() << endl;
cout << "test[0]=" << test[0] << endl;
cout << "test[1]=" << test[1] << endl;
cout << "test[9]=" << test[9] << endl;
/************对应输出*******************/
after resize(10):
size:10
capacity:38
test[0]=2
test[1]=2
test[9]=2
/***************************************/

	return 0;
}

结论:

resize既分配了空间,也创建了对象;reserve表示容器预留空间,但并不是真正的创建对象。
当resize()的参数小于size时,调用resize()后的size等于resize()的参数。
当resize()的参数大于size时,调用resize()后size等resize()的参数,如果没有指明初始值,新增元素的值按照默认值分配。
resize()能增加capacity()的值,但不能减小capacity()的值。

reserve只分配空间,不创建对象,对size不起作用。 当reserve()的参数小于capacity时,reserve()无效;
当reserve()的参数大于capcity时,reserve()后的capacity等于reserve()的参数


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值