C++入门STL容器Vector使用基础,深挖 Vector替代 C 语言繁琐容器的利器

Vector介绍

vector标准模板库 中最常用的容器之一,本质上可以理解为:

👉 一个"动态数组"

也就是说,它和普通数组类似,但比数组更强大、更灵活

vector具体介绍

Vector的使用

vector学习时一定要学会查看文档:vector的文档介绍,vector在实际中非常的重要,在实际中

我们熟悉常见的接口就可以

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;
void test1()
{
	vector<int>v1;
	vector<int>v2(10, 1);
	vector<int> v3(++v2.begin(), --v2.end());
}
int main()
{
	test1();
    return 0;
}

遍历vector的三大"剑客"

1.operator\[\]+下标访问

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;
void test1()
{
	vector<int>v2(5, 1);
	for (size_t i = 0;i < v2.size();i++)
	{
		cout << v2[i] << " ";
	}
}
int main()
{
	test1();
    return 0;
}

2.范围for

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;
void test1()
{
	vector<int>v2(5, 1);
	for (auto it : v2)
	{
		cout << it << " ";
	}
}
int main()
{
	test1();
    return 0;
}

3.iterator迭代器遍历

迭代器(Iterator)是一种用于访问容器中元素的对象

👉 本质作用:提供一种统一的方式遍历和操作容器中的数据

对于 vector 而言,由于其底层是连续存储空间

其迭代器本质上可以看作是对指针的封装

|----------|------------------------|
| begin() | 获取第一个元素的迭代器 |
| end() | 获取最后一个元素的下一个位置的迭代器 |
| rbegin() | 获取最后一个元素的反向迭代器 |
| rend() | 获取第一个元素前一位的反向迭代器 |

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;
void test1()
{
	vector<int>v2(5, 1);
	vector<int>::iterator it = v2.begin();
	while (it != v2.end())
	{
		cout << *it << " ";
		it++;
	}
}
int main()
{
	test1();
    return 0;
}

空间存储,size,capacity与扩容策略

vector的空间分为size(有效数据个数),capacity(最大容纳数据个数)两部分

1.capacity的代码在vs和g++下分别运行会发现,vs下capacity是按1.5倍增长的,g++是按2

倍增长的。这个问题经常会考察,不要固化的认为,vector增容都是2倍,具体增长多少是

根据具体的需求定义的。vs是PJ版本STL,g++是SGI版本STL

2.reserve只负责开辟空间,如果确定知道需要用多少空间,reserve可以缓解vector增容的代

价缺陷问题

3.resize在开空间的同时还会进行初始化,影响size

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;
void test3()
{
	vector<int> v(10, 1);
	v.reserve(20);
	cout << v.size() << endl;
	cout << v.capacity() << endl;
	cout << endl;
	v.reserve(15);
	cout << v.size() << endl;
	cout << v.capacity() << endl;
	cout << endl;
	v.reserve(5);
	cout << v.size() << endl;
	cout << v.capacity() << endl;
	cout << endl;
}int main()
{
	test3();
    return 0;
}

如果我们知道元素个数,就可以提前扩容,避免边插边扩容而导致的效率底下

增删改查:日常高频使用操作

它们都只能通过迭代器插入删除

内置类型的增删改查

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;
void test4()
{
	vector<int> v(10, 1);
	v.push_back(2);
	v.insert(v.begin() + 3, 0);
	for (auto it : v)
	{
		cout << it << " ";
		it++;
	}
	cout << endl;
	vector<int> v1(5.0);
	cout << "输入5个数:";
	for (size_t i = 0;i < 5;i++)
	{
		cin >> v1[i];
	}
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;
}
int main()
{
	test4();
	return 0;
}

自定义类型的增删改查

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;
class AA
{
public:
	AA(int a1 = 0, int a2 = 0)
		:_a1(a1)
		, _a2(a2)
	{
	}

	int _a1 = 1;
	int _a2 = 1;
};

void test_vector3()
{
	//自定义类型的增删查改
	AA aa1;
	vector<AA> vAA1 = { aa1, {0, 0}, {1, 1}, {2, 2} };
	vector<AA>::iterator it1 = vAA1.begin();
	while (it1 != vAA1.end())
	{
		cout << it1->_a1 << "-" << it1->_a2 << " ";
		//也可以是:cout << (*it)._a1 << "-" << (*it)._a2 << " "; //注意.的优先级比*高,需要括号
		it1++;
	}
	cout << endl;

	vAA1.push_back({ 3, 3 });
	AA aa2(4, 4);
	vAA1.push_back(aa2);
	vAA1.insert(vAA1.begin() + 1, aa2);
	vector<AA>::iterator it2 = vAA1.begin();
	while (it2 != vAA1.end())
	{
		cout << it2->_a1 << "-" << it2->_a2 << " ";
		it2++;
	}
}

int main()
{
	test_vector3();
	return 0;
}

Vector中存储string类型

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;
void test5()
{
	vector<string> v1;
	string s1("xxxx");
	v1.push_back(s1);

	v1.push_back("xxx");
	for (const auto& e : v1)
	{
		cout << e << " ";
	}
	cout << endl;
}
int main()
{
	test5();
	return 0;
}

vector中存储vector<int>类型(二维数组)

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;
void test5()
{
	vector<int> v(5, 1);
	vector<vector<int>> vv(10, v);
	vv[2][1] = 2;
	//vv.operator[](2).operator[](1) = 2;
	for (size_t i = 0;i < vv.size();i++)
	{
		for (size_t j = 0;j < vv[i].size();++j)
		{
			cout << vv[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}
int main()
{
	test5();
	return 0;
}

那我们就来看一下两个\[\]的情况

结尾

vector使用介绍就到这里,代码最重要是练习,需要每天坚持练习,道阻且长,前方依旧任重而道远,加油!!!

相关推荐
名字还没想好☜19 小时前
Python f-string 进阶:数字格式化、对齐填充、调试 = 号与嵌套表达式
开发语言·数据库·python·字符串格式化·f-string
·薯条大王19 小时前
经济实惠玩云服务器|一台云服务器多人共用,子账号配置教程
java·linux·运维·服务器·汇编·c++·python
一壶浊酒..1 天前
Scrape Webpack练习
开发语言·javascript·webpack
命运之光1 天前
【C语言完整代码】就诊信息管理系统
java·c语言·开发语言
命运之光1 天前
【C语言完整代码】图书管理系统:图书馆场景下的增删改查实战
c语言·开发语言
猿长大人1 天前
C# | Serilog 新手入门
开发语言·c#·.net·log
hansang_IR1 天前
【题解】LC:倍增 / 区间并查集(Range Parallel Unionfind)
c++·算法·并查集
东东最爱敲键盘1 天前
day7.c++继承
c++
在世修行1 天前
从零打造 C# 工业视觉检测系统(七):串口通信 SerialPort 封装与开发
开发语言·c#·modbus rtu·rs232/rs485
沫璃染墨1 天前
《从零入门Linux系统篇(十五):系统工具篇·六——GDB调试器详解:从程序执行控制到高级调试技巧》
linux·运维·服务器·c语言·c++