目录
一、(constructor)构造函数声明
二、vector的迭代器
三、vector的增删改查
四、insert和erase,以及查找find(find不属于vector,是stl提供的算法)
五、三种遍历方式
六、源代码
vector是可变大小数组的序列容器
一、(constructor)构造函数声明

//构造
int TestVector1()
{
	//无参构造
	vector<int> v1;
	//构造并初始化n个val
	vector<int> v2(4, 100);
	//使用迭代器进行初始化构造
	vector<int> v3(v2.begin(), v2.end());
	//拷贝构造
	vector<int> v4(v3);
	return 0;
} 
二、vector的迭代器
//迭代器
void PrintVector(const vector<int>& v)
{
	//遍历打印
	vector<int>::const_iterator it = v.begin();
	while (it != v.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}
void TestVector2()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	//遍历打印
	vector<int>::iterator it = v.begin();
	while (it != v.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
	//修改
	it = v.begin();
	while (it != v.end())
	{
		*it *= 2;
		++it;
	}
	//使用反向迭代器进行遍历再打印
	vector<int>::reverse_iterator rit = v.rbegin();
	while (rit != v.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;
	PrintVector(v);
} 
三、vector的增删改查
void TestVector4()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	auto it = v.begin();
	while (it != v.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
	v.pop_back();
	v.pop_back();
	it = v.begin();
	while (it != v.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
} 
四、insert和erase,以及查找find(find不属于vector,是stl提供的算法)
void TestVector5()
{
	vector<int> v{ 1,2,3,4 };
	auto pos = find(v.begin(), v.end(), 3);
	if (pos != v.end())
	{
		v.insert(pos, 30);
	}
	vector<int>::iterator it = v.begin();
	while (it != v.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
	pos = find(v.begin(), v.end(), 3);
	// 删除pos位置的数据
	v.erase(pos);
	it = v.begin();
	while (it != v.end()) {
		cout << *it << " ";
		++it;
	}
	cout << endl;
} 
五、三种遍历方式
void TestVector6()
{
	vector<int> v{ 1,2,3,4 };
	//通过[]读写第0个位置
	v[0] = 10;
	cout << v[0] << endl;
	//使用for+[]方式遍历
	for (size_t i = 0; i < v.size(); ++i)
	{
		cout << v[i] << " ";
	}
	cout << endl;
	vector<int> swapv;
	swapv.swap(v);
//使用迭代器遍历
	auto it = swapv.begin();
	while (it != swapv.end())
	{
		cout << *it << " ";
		++it;
	}
	// 3. 使用范围for遍历
	for (auto x : v)
		cout << x << " ";
	cout << endl;
} 
六、源代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include<vector>
//构造
int TestVector1()
{
	//无参构造
	vector<int> v1;
	//构造并初始化n个val
	vector<int> v2(4, 100);
	//使用迭代器进行初始化构造
	vector<int> v3(v2.begin(), v2.end());
	//拷贝构造
	vector<int> v4(v3);
	return 0;
}
//迭代器
void PrintVector(const vector<int>& v)
{
	//遍历打印
	vector<int>::const_iterator it = v.begin();
	while (it != v.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}
void TestVector2()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	//遍历打印
	vector<int>::iterator it = v.begin();
	while (it != v.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
	//修改
	it = v.begin();
	while (it != v.end())
	{
		*it *= 2;
		++it;
	}
	//使用反向迭代器进行遍历再打印
	vector<int>::reverse_iterator rit = v.rbegin();
	while (rit != v.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;
	PrintVector(v);
}
//尾插和尾删
void TestVector4()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	auto it = v.begin();
	while (it != v.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
	v.pop_back();
	v.pop_back();
	it = v.begin();
	while (it != v.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}
void TestVector5()
{
	vector<int> v{ 1,2,3,4 };
	auto pos = find(v.begin(), v.end(), 3);
	if (pos != v.end())
	{
		v.insert(pos, 30);
	}
	vector<int>::iterator it = v.begin();
	while (it != v.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
	pos = find(v.begin(), v.end(), 3);
	// 删除pos位置的数据
	v.erase(pos);
	it = v.begin();
	while (it != v.end()) {
		cout << *it << " ";
		++it;
	}
	cout << endl;
}
void TestVector6()
{
	vector<int> v{ 1,2,3,4 };
	//通过[]读写第0个位置
	v[0] = 10;
	cout << v[0] << endl;
	//使用for+[]方式遍历
	for (size_t i = 0; i < v.size(); ++i)
	{
		cout << v[i] << " ";
	}
	cout << endl;
	vector<int> swapv;
	swapv.swap(v);
//使用迭代器遍历
	auto it = swapv.begin();
	while (it != swapv.end())
	{
		cout << *it << " ";
		++it;
	}
	// 3. 使用范围for遍历
	for (auto x : v)
		cout << x << " ";
	cout << endl;
}
int main()
{
	TestVector1();
	TestVector2();
	TestVector4();
	TestVector5();
	TestVector6();
	return 0;
} 

拜拜啦,又是朝着money奋斗的一天!





![[附源码]计算机毕业设计JAVA校园一卡通管理信息系统台](https://img-blog.csdnimg.cn/384ff0cde90d4aaa93ddb06d9c140644.png)









![[附源码]计算机毕业设计springboot校园商铺](https://img-blog.csdnimg.cn/80f868d4a3a346158e29165ac1ef8984.png)
![[附源码]Python计算机毕业设计Django基于web的羽毛球管理系统](https://img-blog.csdnimg.cn/162d3968520145bd9d5d26d0fbb8319a.png)

