【C++】——vector

news/2024/9/20 20:46:00 标签: c++, 开发语言

文章目录

  • vector介绍
  • vector的使用
  • vector的构造
  • vector迭代器
  • vector空间增减
  • 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;
	
}

在这里插入图片描述


http://www.niftyadmin.cn/n/5667622.html

相关文章

报错error: RPC failed,curl 16 Error in the HTTP2 framing layer解决方法

error: RPC failed&#xff1b; curl 16 Error in the HTTP2 framing layerfatal: expected flush after ref listing 问题描述&#xff1a; git pull origin main报错error: RPC failed&#xff0c;curl 16 Error in the HTTP2 framing laye 解决方法1&#xff1a; git con…

GPTo1论文详解

Learning to Reason with LLMs – OpenAI o1 论文详解 Abstract OpenAI 推出了 OpenAI o1&#xff0c;这是一种新的大型语言模型&#xff0c;通过强化学习进行训练&#xff0c;用于执行复杂的推理。o1 在回答之前会思考 - 在响应用户之前&#xff0c;它可以产生一个很长的内部…

影刀RPA实战:网页爬虫之苦瓜书籍数据

书籍常常被视为心灵的慰藉&#xff0c;因为它们能够在不同的层面上为人们提供支持和安慰。 1. 书籍对我们的重要性 书籍是人类知识的载体&#xff0c;也是智慧的结晶。它们不仅是学习的工具&#xff0c;更是人类心灵的慰藉。在忙碌的生活中&#xff0c;书籍能够提供知识、启发…

Selenium 4.* 获取网页token

环境: <dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>${selenium.version}</version> </dependency>java 版本 11 chromeDriver 下载地址 我使用的版本: https…

揭开 Vue 3 中大量使用 ref 的隐藏危机

在 Vue 3 中&#xff0c;ref 是用来创建响应式的引用&#xff0c;它能够追踪和管理单一的变量或对象。当代码中大量使用 ref 时&#xff0c;虽然可以实现对各个状态或数据的精细控制&#xff0c;但也会带来一些问题和潜在影响。 1. 大量使用 ref 带来的问题 1、代码冗长与维护…

C++解决n点最小曼哈顿距离

作者制作不易&#xff0c;关注、点赞、收藏一下吧&#xff01; 1.曼巴顿距离 ‌‌曼哈顿距离是由十九世纪的‌赫尔曼闵可夫斯基所创词汇‌&#xff0c;用于标明两个点在标准坐标系上的绝对轴距总和。 2.代码实现 2.1.导入头文件、命名空间 这个没有什么好说的&#xff0c;…

通信工程学习:什么是ODN光分配网络

ODN&#xff1a;光分配网络 ODN&#xff08;Optical Distribution Network&#xff0c;光分配网络&#xff09;是光接入网中的重要组成部分&#xff0c;它位于光线路终端&#xff08;OLT&#xff09;和光网络单元&#xff08;ONU&#xff09;/光网络终端&#xff08;ONT&#x…

【Python】耗时任务的超时管理

一、背景介绍 在日常编程中&#xff0c;我们经常会遇到一些耗时的任务&#xff0c;如文件处理、网络请求等。为了提高程序的执行效率&#xff0c;我们可以采用多进程的方式来实现任务的并行处理。然而&#xff0c;在某些情况下&#xff0c;任务执行时间过长可能会导致程序卡顿&…