2021-03-18-C++学习之15-vector容器

一、vector基本概念

  • 功能
    • vector数据结构和数组非常相似,也称为单端数组
  • vector与普通数组区别
    • 数组是静态的,vector可以动态扩展
  • 动态扩展
    • 并不是在原空间之后续接新空间,而是找更大的内存空间,然后将原数据拷贝到新空间,释放原空间。
      在这里插入图片描述
  • vector的迭代器是支持随机访问的迭代器

二、vector构造函数
功能:创建vector容器
函数原型:
在这里插入图片描述
代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件

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

//vector容器构造
void test01()
{
	//默认构造 无参构造
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);

	//通过区间方式进行构造
	//将区间(v1.begin(), v1.end())中的元素拷贝给本身
	vector<int>v2(v1.begin(), v1.end());
	printVector(v2);

	//n个elem方式构造
	//10个100
	vector<int>v3(5, 100);
	printVector(v3);

	//拷贝构造
	vector<int>v4(v3);
	printVector(v4);
}
void test02()
{
	
}

int main()
{
	test01();
	test02();
	
	system("pause");
	return 0;
}

输出:

0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
100 100 100 100 100
100 100 100 100 100
请按任意键继续. . .

三、vector赋值操作
给vector容器进行赋值
函数原型:

//重载等号操作符
vector& operator+(const vector &vec);
//将(beg, end)区间中的数据拷贝赋值给本身
asign(beg,end);
//将n个elem拷贝赋值给本身
assign(n,elem);

代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件

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

//vector容器赋值
void test01()
{
	cout << "----------v1----------" << endl;
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);

	//赋值  operator=
	cout << "----------v2----------" << endl;
	vector<int>v2;
	v2 = v1;
	printVector(v2);

	// assign赋值
	cout << "----------v3----------" << endl;
	vector<int>v3;
	v3.assign(v1.begin(), v1.end());
	printVector(v3);

	//n个elem 方式赋值
	cout << "----------v4----------" << endl;
	vector<int>v4;
	v4.assign(5, 100);
	printVector(v4);
}
void test02()
{
	
}

int main()
{
	test01();
	test02();
	
	system("pause");
	return 0;
}

输出:

----------v1----------
0 1 2 3 4 5 6 7 8 9
----------v2----------
0 1 2 3 4 5 6 7 8 9
----------v3----------
0 1 2 3 4 5 6 7 8 9
----------v4----------
100 100 100 100 100
请按任意键继续. . .

四、vector容量和大小
功能描述:

  • 对vector容器的容量和大小操作

函数原型:
在这里插入图片描述
代码参数:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件

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

//vector容器容量和大小
void test01()
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);

	if (v1.empty()) //为真 代表容器为空
	{
		cout << "v1为空" << endl;
	}
	else
	{
		cout << "v1不为空" << endl;
		//输出:v1的容量为:13
		cout << "v1的容量为:" << v1.capacity() << endl;
		//输出:v1的大小为:10
		//容量 >= 大小
		cout << "v1的大小为:" << v1.size() << endl;
	}

	//重新指定大小
	//如果重新指定的比原来长,默认用0填充新的多余的位置
	//v1.resize(15);
	//printVector(v1); //输出:0 1 2 3 4 5 6 7 8 9 0 0 0 0 0
	v1.resize(15, 100);  //利用重载版本,可以指定默认填充值,参数2
	printVector(v1); //输出:0 1 2 3 4 5 6 7 8 9 100 100 100 100 100

	//如果重新指定的比原来短了,超出部分会删除掉
	v1.resize(5); 
	printVector(v1); //输出:0 1 2 3 4
}
void test02()
{
	
}

int main()
{
	test01();
	test02();
	
	system("pause");
	return 0;
}

输出:

0 1 2 3 4 5 6 7 8 9
v1不为空
v1的容量为:13
v1的大小为:10
0 1 2 3 4 5 6 7 8 9 100 100 100 100 100
0 1 2 3 4
请按任意键继续. . .

五、vector插入和删除
函数原型:
在这里插入图片描述
代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件

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

//vector容器容量和大小
void test01()
{
	vector<int>v1;
	//尾插
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);
	//遍历
	printVector(v1); //输出:10 20 30 40 50

	//尾删
	v1.pop_back();
	printVector(v1); //输出:10 20 30 40

	//插入  第一个参数是迭代器
	v1.insert(v1.begin(),100);
	printVector(v1); //输出:100 10 20 30 40

	v1.insert(v1.begin(), 2, 1000); //在指定迭代器位置插入n个数据
	printVector(v1); //输出:1000 1000 100 10 20 30 40

	//删除 参数也是迭代器
	v1.erase(v1.begin());
	printVector(v1); //输出:1000 100 10 20 30 40

	//清空
	//v1.erase(v1.begin(), v1.end());
	//或者
	v1.clear();
	printVector(v1); //无任何输出
}
void test02()
{
	
}

int main()
{
	test01();
	test02();
	
	system("pause");
	return 0;
}

输出:

10 20 30 40 50
10 20 30 40
100 10 20 30 40
1000 1000 100 10 20 30 40
1000 100 10 20 30 40

请按任意键继续. . .

六、vector数据存取
函数原型:
在这里插入图片描述
代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件

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

//vector容器数据存取
void test01()
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	//利用 [] 方式访问数组中元素
	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1[i] << " ";
	}
	cout << endl;

	//利用at方式访问元素
	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1.at(i) << " ";
	}
	cout << endl;

	//获取第一个元素
	cout << "第一个元素为:" << v1.front() << endl;

	//获取最后一个元素
	cout << "最后一个元素为:" << v1.back() << endl;
}
void test02()
{
	
}

int main()
{
	test01();
	test02();
	
	system("pause");
	return 0;
}

输出:

0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
第一个元素为:0
最后一个元素为:9
请按任意键继续. . .

七、vector互换容器

  • 实现两个容器内元素进行互换
    函数原型:
swap(vec);  //将vec与本身的元素互换

C++ vector容器的swap方法(容器互换)参见链接:
C++ vector容器的swap方法(容器互换)

在这里插入图片描述
代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件

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

//vector容器互换
void test01()
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	cout << "交换前:" << endl;
	cout << "v1:";
	printVector(v1);

	vector<int>v2;
	for (int i = 10; i > 0; i--)
	{
		v2.push_back(i);
	}
	cout << "v2:";
	printVector(v2);

	cout << "交换后:" << endl;
	v1.swap(v2);
	cout << "v1:";
	printVector(v1);
	cout << "v2:";
	printVector(v2);
}

//实际用途
//巧用swap可以收缩内存空间
void test02()
{
	vector<int>v1;
	for (int i = 0; i < 100000; i++)
	{
		v1.push_back(i);
	}
	cout << "v1的容量为:" << v1.capacity() << endl;//输出:v1的容量为:138255
	cout << "v1的大小为:" << v1.size() << endl;

	v1.resize(3); //重新指定大小
	cout << "v1的容量为:" << v1.capacity() << endl;//输出:v1的容量为:138255
	cout << "v1的大小为:" << v1.size() << endl;

	//巧用swap收缩内存
	v1.resize(5);
	vector<int>(v1).swap(v1);
	cout << "v1的容量为:" << v1.capacity() << endl;//输出:v1的容量为:5
	cout << "v1的大小为:" << v1.size() << endl;//v1的大小为:5
}

int main()
{
	test01();
	test02();
	
	system("pause");
	return 0;
}

输出:

交换前:
v1:0 1 2 3 4 5 6 7 8 9
v2:10 9 8 7 6 5 4 3 2 1
交换后:
v1:10 9 8 7 6 5 4 3 2 1
v2:0 1 2 3 4 5 6 7 8 9
v1的容量为:138255
v1的大小为:100000
v1的容量为:138255
v1的大小为:3
v1的容量为:5
v1的大小为:5
请按任意键继续. . .

八、vector预留空间
功能:减少vector在动态扩展容量时的扩展次数。
函数原型:

reserve(int len); //容器预留len个元素长度,预留位置不初始化,元素不可访问。

代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<vector>
#include<algorithm> //标准算法头文件

//vector容器预留空间
void test01()
{
	vector<int>v;
	//利用reserve预留空间
	v.reserve(100000);
	int num = 0;
	int* p = NULL;
	for (int i = 0; i < 100000; i++)
	{
		v.push_back(i); 
		if (p != &v[0]) //动态扩展了30次内存空间
		{
			p = &v[0];
			num++;
		}
	}
	//输出:num = 30。
	//执行v.reserve(100000); 后,输出:num = 1
	cout << "num = " << num << endl; 
}

void test02()
{
	
}

int main()
{
	test01();
	test02();
	
	system("pause");
	return 0;
}

输出:

num = 1
请按任意键继续. . .
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

努力学习的代码小白

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

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

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

打赏作者

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

抵扣说明:

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

余额充值