vector-入门(二)

继续vector-入门(一)的记录。

c.capacity()

返回容器中数据个数。

c.max_size()

返回容器中最大数据的数量

c.resize(num)

重新指定队列的长度。

c.size()

返回容器中实际数据的个数

c.reserve()

保留适当的容量。

#include <stdlib.h>
#include <vector>
using namespace std;

void main()
{
	//创建一个int类型的vector容器
	vector<int>test;

	//test为 3个33
	test.assign(3, 33);	
	
	printf("Original reserve,  size:%d, capacity:%d, max_size:%d\n", test.size(), test.capacity(), test.max_size());

	test.reserve(5);
	printf("After reserve,  size:%d, capacity:%d, max_size:%d\n", test.size(), test.capacity(), test.max_size());

	//test.resize(5);
	//printf("After resize,  size:%d, capacity:%d, max_size:%d\n", test.size(), test.capacity(), test.max_size());

	system("pause");
}

reserve()截图:

resize()截图:

 

c.clear()

移除容器中所有数据。

c.empty()

判断容器是否为空

c.erase(pos)

c.erase(beg,end)

删除pos位置的数据,传回下一个数据的位置。

删除[beg,end)区间的数据,传回下一个数据的位置。

#include <stdafx.h>
#include <stdlib.h>
#include <vector>
using namespace std;

void main()
{
	//创建一个int类型的vector容器
	vector<int>test;

	//test为 3个33
	test.assign(3, 33);	

	printf("IsEmpty:%d\n", test.empty());

	test.clear();

	printf("after clear(), IsEmpty:%d\n", test.empty());

	system("pause");	
}

 

#include <stdlib.h>
#include <vector>
using namespace std;

void main()
{
	//创建一个int类型的vector容器
	vector<int>test;

	test.push_back(1);
	test.push_back(2);
	test.push_back(3);
	test.push_back(4);
	test.push_back(5);

	//删除test-begin的0号位数据
	test.erase(test.begin());
	
	printf("size:%d, [0]:%d, [1]:%d, [2]:%d, [3]:%d\n", 
		test.size(), test.at(0), test.at(1), test.at(2), test.at(3));

	//删除test,从begin开始的2号位到结束的数据
	test.erase(test.begin()+2, test.end());
	
	printf("size:%d, [0]:%d, [1]:%d\n", 
		test.size(), test.at(0), test.at(1));

	system("pause");	
}



c1.swap(c2)

swap(c1,c2)

c1c2元素互换。

同上操作。

#include <stdlib.h>
#include <vector>
using namespace std;

void main()
{
	//创建一个int类型的vector容器
	vector<int>test1;
	vector<int>test2;

	test1.push_back(1);
	test1.push_back(2);
	test1.push_back(3);
	
	test2.push_back(44);
	test2.push_back(55);

	//test1.swap(test2);
	swap(test1, test2);
	
	printf("test1, size:%d, [0]:%d, [1]:%d\n", 
		test1.size(), test1.at(0), test1.at(1));

	printf("test2, size:%d, [0]:%d, [1]:%d, [2]:%d\n", 
		test2.size(), test2.at(0), test2.at(1), test2.at(2));	

	system("pause");	
}

 

vector<Elem> c

vector <Elem> c1(c2)

vector <Elem> c(n)

vector <Elem> c(n, elem)

vector <Elem> c(beg,end)

c.~ vector <Elem>()

创建一个空的vector

复制一个vector

创建一个vector,含有n个数据,数据均已缺省构造产生

创建一个含有nelem拷贝的vector

创建一个以[beg;end)区间的vector

销毁所有数据,释放内存。

创建一个Widget类型的空的vector对象:

vector<Widget> vWidgets;

 

创建一个包含500Widget类型数据的vector

vector<Widget> vWidgets(500);

 

创建一个包含500Widget类型数据的vector,并且都初始化为0

vector<Widget> vWidgets(500, Widget(0));

 

创建一个Widget的拷贝:

vector<Widget> vWidgetsFromAnother(vWidgets);

 

还有另外的成员函数,感觉一般情况压根用不到,这边就不做详解了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值