2.3 C++STL vector容器详解

2.3.1 引入

vector 容器
动态数组 可变数组
vector容器 单口容器(尾部操作效率高)
一些迭代器
vector动态增长基本原理:
当插入新元素时,如果空间不足,那么vector会重新申请更大的一块内存空间,将原空间数据拷贝到新空间,释放旧空间的数据,再把新元素插入新申请空间。

API理论用法详解见vector容器常用API操作


2.3.2 代码实例

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

void printv(vector<int> v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

//初始化
void text01()
{
	vector<int> v1;//默认构造

	int arr[] = { 10,20,30,40,50 };
	vector<int> v2(arr, arr + sizeof(arr) / sizeof(arr[0]));
	vector<int>v3(v2.begin(), v2.end());
	vector<int> v4(v3);

	printv(v1);
	printv(v2);
	printv(v3);
	printv(v4);
}

//常用赋值操作
void text02()
{
	int arr[] = { 10,20,30,40,50 };
	vector<int> v11(arr, arr + sizeof(arr) / sizeof(int));
	//成员方法
	vector<int> v2;
	v2.assign(v11.begin(), v11.end());

	//重载=
	vector<int> v3;
	v3 = v2;

	int arr1[] = { 100,200,300,400 };
	vector<int> v4(arr1, arr1 + sizeof(arr1) / sizeof(int));

	printv(v11);
	printv(v2);
	printv(v3);
	printv(v4);
	cout << "下面是v11,v4交换后-----------------\n";

	v4.swap(v11);//交换指针

	printv(v11);
	printv(v2);
	printv(v3);
	printv(v4);

}

//大小操作
void text03()
{
	int arr1[] = { 100,200,300,400 };
	vector<int> v4(arr1, arr1 + sizeof(arr1) / sizeof(int));
	
	cout << "size: " << v4.size() << endl;
	if (v4.empty())
	{
		cout << "empty\n";
	}
	else
	{
		cout << "no empty\n";
	}
	printv(v4);
	v4.resize(2);//比实际小扔掉后面的数据
	printv(v4);
	v4.resize(6);//比实际大补上默认值
	printv(v4);
	v4.resize(8,1);//修改默认值
	printv(v4);

	cout << "size: " << v4.size() << endl;//元素个数20
	cout <<"capacity"<< v4.capacity() << endl;//容量 100
	//容量解决每开辟一个即将溢出的新数据就要申请新空间的问题
}

//vector存取数据
void text04()
{
	int arr1[] = { 100,200,300,400 };
	vector<int> v4(arr1, arr1 + sizeof(arr1) / sizeof(int));

	for (int i = 0; i < v4.size(); i++)
	{
		cout << v4[i] << " ";
	}
	cout << endl;
	for (int i = 0; i < v4.size(); i++)
	{
		cout << v4.at(i) << " ";//可以抛异常
	}
	cout << endl;

	cout << "front:" << v4.front() << endl;
	cout << "back:" << v4.back() << endl;
}

//插入和删除
void text05()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(20);
	//头插法
	v.insert(v.begin(), 30);
	v.insert(v.end(), 40);
	printv(v);
	v.insert(v.begin() + 2, 100);//vector支持随机访问
	printv(v);
	//支持数组小标,一般都支持随机访问
	//迭代器可以直接+2 +3 -2 -5操作

	//删除
	v.erase(v.begin());
	printv(v);
	v.erase(v.begin() + 1, v.end());
	printv(v);
	v.clear();
	cout << "size:" << v.size() << endl;
}

//巧用swap缩减空间
void text06()
{
	//vector添加元素 他会自动增长 你删除元素,会自动减少么?
	vector<int> v;
	for (int i = 0; i <10000; i++)
	{
		v.push_back(i);
	}
	cout << "size:" << v.size() << endl;
	cout << "capacity:" << v.capacity() << endl<<"-------";

	v.resize(10);
	cout << "\nsize:" << v.size() << endl;
	cout << "capacity:" << v.capacity() << endl;
	
	//收缩空间
	/*vector<int>(v)匿名对象*/vector<int>(v).swap(v);
	//匿名对象[C++之匿名对象解析](https://www.cnblogs.com/cthon/p/9173472.html)
	cout << "---------" << endl;
	cout << "size:" << v.size() << endl;
	cout << "capacity:" << v.capacity() << endl ;
}

void text07()
{
	//reserve 预留空间 resize 区别
	int num = 0;
	int* address = NULL;
	vector<int> v;
	//v.reserve(100000);删掉这一行注释,reserve预留空间,这样只开辟一次空间
	for (int i = 0; i < 100000; i++)
	{
		v.push_back(i);
		if (address != &(v[0]))
		{
			address = &(v[0]);
			num++;
		}
	}
	cout <<num<< endl;//申请了30次空间
}

int main()
{
	cout << "\ntext01\n";
	text01();
	cout << "\ntext02\n";
	text02();
	cout << "\ntext03\n";
	text03();
	cout << "\ntext04\n";
	text04();
	cout << "\ntext05\n";
	text05();
	cout << "\ntext06\n";
	text06();
	cout << "\ntext07\n";
	text07();
	return 0;
}

2.3.3 运行结果

1
2


总结

vector作为STL中比较常用的容器,需要好好掌握。可以参考我专栏的下一篇deque容器讲解deque容器一起记忆。这里推荐一篇对vector有较为细致讲解的文章C++STL详解(三)—— vector的介绍及使用


谢谢阅读(〃’ ▽ '〃)如有纰漏欢迎指出,觉得还不错就点个赞吧。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只子美

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值