【C++】——vector

vector介绍

  1. vector是一个动态数组,可以根据需求变大变小
  2. vector支持随机访问
  3. vector会自动管理内存分配和释放
  4. vector在尾部添加和删除的效率非常高,中间和头部插入较慢,因为内存是连续的,除了尾部的增删以外都需要挪动被处理数据之后的全部数据

vector的使用

vector的存在形式
在这里插入图片描述
vector的接口
在这里插入图片描述

vector的构造

vecotr的常用构造大致有一下几种

#define   _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
using namespace std;

void test_vector1()
{
	vector<int>(); // 匿名对象会在这段代码执行结束完毕后销毁,即;之前
	vector<int> v0 = { 10,9,8,7,6 }; // 初始化列表构造
	vector<int> v1; // 无参构造
	vector<int> v2(10, 0); // 构造一个10个大小且全部初始化为0的vector
	vector<int> v3(v2); // 拷贝构造
	vector<int> v4(v2.begin(), v2.end()); // 范围构造
	
	int arr[] = { 1,2,3,4,5 };
	vector<int> v5(arr, arr + sizeof(arr) / sizeof(int)); // 用原生指针或者库里的begin(),end()都可以,对于标准库不熟悉的话可以这么写

	for (vector<int>::iterator it = v5.begin(); it < v5.end(); ++it)
	{
		cout << *it << " ";
	}

	cout << endl;

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

vector迭代器

在这里插入图片描述

void test_vector2()
{
	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;

	// 反向迭代器遍历
	vector<int>::reverse_iterator rt = v.rbegin(); // 指向最后一个指针
	while (rt != v.rend()) // 指向第一个指针
	{
		cout << *rt << " "; // 这里的++是倒着走
		++rt;
	}
	cout << endl;

	// 范围for遍历
	for (auto e : v)
	{
		cout << e << " ";
	}
	cout << endl;

}

vector空间增减

在这里插入图片描述

  1. vecotr在不同环境下的扩容倍数不同,一般是1.5到2倍
  2. reserve只改变capacity,不改变size
  3. resize改变size,不一定改变capacity,如果当前capacity大于等于szie,则不改变,如果小于,capacity会至少增大到能够容下size(如果新的大小远低于当前capacity,vector可能会选择减小其capacity以节省内存)
void test_vector3()
{
	vector<int> v1(10);
	v1.reserve(100); 
	cout << "v1.size = " << v1.size() << " "  <<"v1.capacity = " << v1.capacity() << endl;

	vector<int> v2(v1);
	v2.resize(50);
	cout << "v2.size = " << v2.size() << " " << "v2.capacity = " << v2.capacity() << endl;
	v2.resize(5);
	cout << "v2.size = " << v2.size() << " " << "v2.capacity = " << v2.capacity() << endl;
	
}

在这里插入图片描述

vector增删查改

在这里插入图片描述

void test_vector4()
{
	vector<int> v1;
	//增: push_back(尾插)
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);
	cout << "增:";
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	//删: pop_back(尾删)
	v1.pop_back();
	cout << "删:";
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	//查: find(这个是算法库中的接口,不是vector的接口,返回类型是查找位置的迭代器)
	auto pos1 = find(v1.begin(), v1.end(), 2);
	cout << "查:";
	cout << *pos1 << endl;    //如果没找到就不进行任何操作

	//在任意位置插入: insert
	//要先用find找到要插入的位置,然后再插入数据
	auto pos2 = find(v1.begin(), v1.end(), 2);
	if (pos2 != v1.end())
	{
		v1.insert(pos2, 20);
	}
	cout << "插入任意位置:";
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	//在任意位置删除: erase
	//同样要先用find找到要删除的位置,然后再删除数据
	auto pos3 = find(v1.begin(), v1.end(), 2);
	v1.erase(pos3);
	cout << "删除任意位置:";
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	//改: operator[]
	v1[0] = 100;
	cout << "改:";
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;
	
}

在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值