C++——vector

1.简介

2.成员函数

2.1构造函数

cpp 复制代码
void test_vector1()
{
    //1.无参构造
	vector<int> v1;
	cout << v1.capacity() << endl;

    //2.传参构造
	vector<int> v2(10,1);

    //3.迭代器构造
	vector<int> v3(v2.begin(), v2.end());

	//也可以使用其它容器的迭代器区间来初始化

	string s1("hello world");
	vector<int> v4(s1.begin(), s1.end());
	//这里数据类型不匹配,但是会隐式类型转换
	//char类型提升成int,会把ASCII码值传过去


    //4.拷贝构造
	vector<int> v5(v3);
}

3.遍历方式

cpp 复制代码
void test_vector1()
{
	vector<int> v1(10,1);

	//1.[]
	for (size_t i = 0; i < v1.size(); i++)
	{
		cout << v1[i] << " ";
	}
	cout << endl;

    
    //2.迭代器
	//vector<int>::iterator it = v1.begin();
	auto it = v1.begin();
	while (it != v1.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;


    //3.范围for
	for (auto a : v1)
	{
		cout << a << " ";
	}
	cout << endl;
}

4.扩容机制

cpp 复制代码
void test_vector2()
{
	//扩容机制

	size_t sz;
	vector<int> v;
	sz = v.capacity();
	cout << "making v grow:\n";
	for (int i = 0; i < 100; ++i)
	{
		v.push_back(i);
		if (sz != v.capacity())
		{
			sz = v.capacity();
			cout << "capacity changed: " << sz << '\n';
		}
	}

}

5.Capacity

5.1 max_size

cpp 复制代码
void test_vector3()
{
	vector<int> v1;
	cout << v1.max_size() << endl;

	vector<char> v2;
	cout << v2.max_size() << endl;

	string s1;
	cout << s1.max_size() << endl;

}

5.2reserve和resize

cpp 复制代码
void test_vector4()
{
	vector<int> v1;

	v1.reserve(100);
	//想要预先开空间,就使用reserve

	//这里就不会进去,因为size还是0,只是capacity变成了100
	for (size_t i = 0; i < v1.size(); i++)
	{
		v1[i] = i;
		//cout << v1[i] << " ";
	}
	cout << endl;

	for (auto a : v1)
	{
		cout << a << " ";
	}
	cout << endl;

}

那如果强行进入循环呢?

cpp 复制代码
void test_vector4()
{
	vector<int> v1;

	v1.reserve(100);

	//那如果强行进去呢?
	//空间已经开出来了,能不能强行访问?
	for (size_t i = 0; i < 100; i++)
	{
		v1[i] = i;
		//答案是不能的,会崩溃
		//这里的[]里面加了断言,断言访问的下标必须是小于size的,大于size说明越界了
		//但注意:断言在release下不起作用
	}
	cout << endl;

}

顺序表和数组有个区别

顺序表有个规定,数据必须是在0到size-1,数据是连续访问的,只能访问0到size-1的数据

所以这里想要访问只能使用resize

书写代码:v1.resize(100);

resize不给值的话默认使用0

5.3shrink_to_fit

shrink_to_fit是缩容接口

把当前容器的capacity缩为size,一般是异地缩容

它是以时间换空间的做法,一般不建议使用,空间比较廉价,而时间更为宝贵

6.Modifiers

6.1 push_back

cpp 复制代码
void test_vector5()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);

	for (auto a : v1)
	{
		cout << a << " ";
	}
	cout << endl;

}

6.2 insert

cpp 复制代码
void test_vector5()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);

	//头插
	v1.insert(v1.begin(), 0);
	for (auto a : v1)
	{
		cout << a << " ";
	}
	cout << endl;

}

如果想在3前面进行插入数据该怎么做

注意,vector中没有find函数,find写在算法库

6.3 algorithm:find

cpp 复制代码
void test_vector5()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);

	//想在3前面进行插入数据
	auto it = find(v1.begin(), v1.end(), 3);
	if (it != v1.end())
	{
		v1.insert(it, 100);
		//默认插入都是在当前位置之前

	}
	//注意:find写在算法库中

}

6.4 erase

cpp 复制代码
void test_vector5()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);

	//想在3前面进行插入数据
	auto it = find(v1.begin(), v1.end(), 3);
	if (it != v1.end())
	{
		v1.insert(it, 100);
		//默认插入都是在当前位置之前

	}


	it = find(v1.begin(), v1.end(), 3);
	v1.erase(it);//

	for (auto a : v1)
	{
		cout << a << " ";
	}
	cout << endl;

}

6.5clear

cpp 复制代码
void test_vector5()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);

	//clear一般不清理capacity
	cout << v1.size() <<" "<< v1.capacity() << endl;

	v1.clear();
	cout << v1.size() << " " << v1.capacity() << endl;

	v1.shrink_to_fit();//想要释放空间只能调用这个函数

}

6.6 emplace和emplace_back

emplace和insert功能相似

emplace_back和push_back功能相似

emplace和emplace_back在有些场景下会更高效一些

7.非成员函数重载

注意,vector还有其它的容器都没有去实现流插入和流提取

在string中,字符串打印没有争议,不加任何修饰地打印即可

但其它容器并不适用

8.练习题

8.1

144. 二叉树的前序遍历 - 力扣(LeetCode)

8.2

136. 只出现一次的数字 - 力扣(LeetCode)

C语言实现起来相当复杂

C++就会便捷许多

8.3

118. 杨辉三角 - 力扣(LeetCode)

相关推荐
暮冬-  Gentle°3 小时前
C++中的命令模式实战
开发语言·c++·算法
Volunteer Technology5 小时前
架构面试题(一)
开发语言·架构·php
清水白石0085 小时前
Python 对象序列化深度解析:pickle、JSON 与自定义协议的取舍之道
开发语言·python·json
2401_876907526 小时前
Python机器学习实践指南
开发语言·python·机器学习
㓗冽6 小时前
分解质因数-进阶题10
c++
图图的点云库6 小时前
高斯滤波实现算法
c++·算法·最小二乘法
努力中的编程者6 小时前
栈和队列(C语言底层实现环形队列)
c语言·开发语言
码不停蹄Zzz7 小时前
C语言——神奇的static
java·c语言·开发语言
CoderCodingNo7 小时前
【GESP】C++七级考试大纲知识点梳理, (1) 数学库常用函数
开发语言·c++
老鱼说AI7 小时前
CUDA架构与高性能程序设计:异构数据并行计算
开发语言·c++·人工智能·算法·架构·cuda