vector类

 > 作者简介:დ旧言~,目前大二,现在学习Java,c,c++,Python等
> 座右铭:松树千年终是朽,槿花一日自为荣。

> 目标:熟悉vector库

> 毒鸡汤:从人生低谷走上人生巅峰是一条很漫长,一开始会很累,但是慢慢就会习惯这种感觉,这说明你在不断的成长。

> 望小伙伴们点赞👍收藏✨加关注哟💕💕 

🌟前言

        相信看完博主的string类CSDN,学习vector会很轻松的🤭🤭,咱们依旧参考c++的官网:cplusplus.com - The C++ Resources Network

 ⭐主体

        这里就需要浅谈一下什么vector,基于在数组的问题,当我们创建一个数组时,此时元素的类型已经锁定,而vector就可以很好的解决这个问题。当然vector不仅仅是这些用法,那咱们就看看vector有啥子东西值得我们学习。

        咱们的主题部分还是按照官网的分类来,具体分为下面这些板块:Member functions(成员函数),Iterators(迭代器),Capacity(容量),Element access(元素访问),Modifiers(修改器),Non-member function (非成员函数)☺️☺️☺️。

🌙Member functions(成员函数)

每一个接口的成员函数都很重要,无论是拷贝构造还是赋值运算重载,都贯彻c++。

💫constructor(构造函数)

根据使用的构造函数版本初始化其值。

在vector的构造函数中有许多的模板,那我们看看到底有哪些类型模板🧐🧐。

1️⃣

这种构造函数本质上是提供一种无参构造

default (1)	 explicit vector (const allocator_type& alloc = allocator_type());

咱们看看它的使用:

#include<iostream>
#include<vector>
using namespace std;
int main()
{vector<char> v1;for (auto ch : v1){cout << ch << endl;}return 0;
}

运行结果:

 2️⃣

这种构造函数本质上是提供一个类模板

fill (2)	
explicit vector (size_type n, const value_type& val = value_type(),const allocator_type& alloc = allocator_type());
  • 💦size_type n:无符号整形
  • 💦const value_type& val = value_type():模板参数类型
  • 💦const allocator_type& alloc = allocator_type():const分配器类型

第三个参数咱们不讲解,只需懂得前面两个参数就行。

咱们看看它的使用:

#include<iostream>
#include<vector>
using namespace std;
int main()
{int a = int();double b = double();char c = char();cout << a << endl;cout << b << endl;printf("%d", c);return 0;
}

运行结果:

解析:

在c++当中给每个自定义类型也创建了对应的默认构造函数,其构造的默认结果为0。

3️⃣

这种构造函数本质上是提供一个迭代器

range (3)	
template <class InputIterator>vector (InputIterator first, InputIterator last,const allocator_type& alloc = allocator_type());
  • 💦第一个参数迭代器开始的地方
  • 💦第二个参数就是迭代器结束的地方
  • 💦第三个参数const分配器类型

第三个参数咱们不讲解,只需懂得前面两个参数就行。

咱们看看它的使用:

#include<iostream>
#include<vector>
using namespace std;
int main()
{vector<int> v{ 1, 2, 3,4 };vector<int> v1(v.begin(), v.end());for (auto ch : v1){cout << ch << endl;}return 0;
}

运行结果:

 4️⃣

这种构造函数本质上就是拷贝构造,创建一个对象的时候将另外一个对象的内容初始化给新创建的对象

copy (4)	vector (const vector& x);

咱们看看它的使用:

#include<iostream>
#include<vector>
using namespace std;
int main()
{vector<int> v1{ 1,2,3,4,5 };vector<int>v2(v1);for (auto ch : v2){cout << ch << endl;}return 0;
}

运行结果:

💫operator(赋值重载)

这个就是赋值重载,将一个vector对象的内容赋值给另一个vector对象

 咱们看看它的使用:

#include <iostream>
#include <vector>
using namespace std;
int main()
{vector<int> v1{ 1,2,3,4,5 };vector<int>v2(v1);v2 = v1;for (auto ch : v2){cout << ch << endl;}return 0;
}

运行结果:

🌙Iterators(迭代器)

        我们已经在string类中讲解这个迭代器,在vector中使用迭代器基本和string类一样,这里我们就加加速,搞快点😏😏。

 💫利用begin和end实现迭代器

  • begin()函数返回的是vector的首位置
  • end()函数返回的是vector最后一个位置(即最后一个元素的下一个位置)

咱看看代码:

#include <iostream>
#include <vector>using namespace std;int main()
{vector<int> v1{ 1,2,3,4,5 };vector<int>::iterator it = v1.begin();while (it != v1.end()){cout << *it;++it;}cout << endl;return 0;
}

运行结果:

 💫范围for实现迭代器

不知道小伙伴还记得auto关键字不,auto可以推导出元素属性(int,char)

咱们看看代码:

#include <iostream>
#include <vector>using namespace std;int main()
{vector<int> v1{ 1,2,3,4,5 };for (auto ch : v1){cout << ch;}cout << endl;return 0;
}

运行结果:

 💫反向迭代器

这里需要介绍一下rbegin()和rend()这两个函数,这两个函数是用来配合反向迭代器使用的。

  • rbegin()函数返回的是vector的最后一个有效元素
  • rend()函数返回的是vector的第一个元素的前一个位置。

咱们看看代码:

#include <iostream>
#include <vector>using namespace std;int main()
{vector<int> v1{ 1,2,3,4,5 };vector<int>::reverse_iterator rit = v1.rbegin();while (rit != v1.rend()){cout << *rit;rit++;}cout << endl;return 0;
}

运行结果:

💫const修饰的迭代器

const修饰的迭代器是不可以改变的(只能读不能写)

咱们看看代码:

#include <iostream>
#include <vector>using namespace std;
void Func(const vector<int> v)
{vector<int>::const_iterator cit = v.begin();// 读操作while (cit != v.end()){cout << *cit;cit++;}cout << endl;// 不能进行写操作,会报错// cit = s.begin();// while (cit != s.end())// {//     (*cit) += 1;//     cout << *cit;//     cit++;// }
}

🌙Capacity(容量)

学习这个板块还是比较简单的,也运用比较广泛的一个板块。

💫size

size函数计算元素总个数的。

咱们看看代码:

#include <iostream>
#include <vector>using namespace std;int main()
{vector<int> v1{ 1,2,3,4,5 };cout << v1.size() << endl;cout << v1.capacity() << endl;return 0;
}

运行结果:

 💫max_size

size函数计算该对象最多容乃多少个元素

咱们看看代码:

#include <iostream>
#include <vector>using namespace std;int main()
{vector<int> v;cout << "该对象最多容乃多少个元素" << endl;cout << v.max_size() << endl;return 0;
}

运行结果:

 💫capacity

capacity函数作用是查看该对象的容量

咱们看看代码:

#include <iostream>
#include <vector>using namespace std;int main()
{vector<int> v1{ 1,2,3,4,5 };cout << "size:" << v1.size() << endl;cout << "capacity:" << v1.capacity() << endl;cout << endl;return 0;
}

运行结果:

💫reserve和resize

reserve函数是扩容函数,可以增大capacity的值。

resize其实也是扩容函数,但resize改变的是size的值,当size的值增大时自动触发vector的扩容机制从而也增大了capacity的值。

resize在增带size值的时候还会对没有元素的位置初始化,如果没有指定初始化内容就默认初始化为空,而reserve不会进行初始化。

咱们看看代码:

#include <iostream>
#include <vector>using namespace std;int main()
{vector<int> v1{ 1,2,3,4,5 };cout << "size:" << v1.size() << endl;cout << "capacity:" << v1.capacity() << endl;cout << endl;v1.reserve(25);cout << "size:" << v1.size() << endl;cout << "capacity:" << v1.capacity() << endl;cout << endl;v1.resize(50);cout << "size:" << v1.size() << endl;cout << "capacity:" << v1.capacity() << endl;cout << endl;return 0;
}

运行结果:

 💫empty

empty函数作用是查看该对象的内容是否为空

  • 有元素返回为0
  • 没有元素返回为1

咱们看看代码:

#include <iostream>
#include <vector>using namespace std;int main()
{vector<int> v1;cout << v1.empty() << endl;vector<int> v2{ 1 };cout << v2.empty() << endl;return 0;
}

运行结果:

🌙Element access(元素访问)

这个板块还是比较重要的,vector作用之一就是元素访问。

 💫operator[ ]

在vector中我们将操作符[ ]进行了重载,使其[ ]可以像数组那样通过下标来获取对应的元素和修改对应的元素。

 💫at函数

返回位置pos处的元素的引用。本质上还是一种遍历。

唯一与operator的区别就在于对于不合法的下标at会抛异常不会报错而[ ]会报错

咱们看看代码:

#include <iostream>
#include <vector>int main()
{std::vector<int> v1{ 1,2,3,4,5 };for (unsigned i = 0; i < v1.size(); ++i){std::cout << v1.at(i);}return 0;
}

运行结果:

🌙Modifiers(修改器)

在vector中修改器是十分重要的,大家务必多多练习,多多查看官网。

 💫assign函数

这个函数可以改变vector的容量和长度。

咱们看看代码:

#include <iostream>
#include <vector>int main()
{std::vector<int> first;std::vector<int> second;std::vector<int> third;first.assign(7, 100);             std::vector<int>::iterator it;it = first.begin() + 1;second.assign(it, first.end() - 1); int myints[] = { 1776,7,4 };third.assign(myints, myints + 3);   std::cout << "Size of first: " << int(first.size()) << '\n';std::cout << "Size of second: " << int(second.size()) << '\n';std::cout << "Size of third: " << int(third.size()) << '\n';return 0;
}

运行结果:

 💫push_back插入操作

将元素c追加到vector的末尾,使其长度增加一,也就是说push_back函数只能够尾插入一个元素,不能插入多个元素。

咱们看看代码:

#include <iostream>
#include <vector>using namespace std;int main()
{vector<int> v1{ 1,2,3,4,5 };// 插入一个元素v1.push_back(10);for (auto ch : v1){cout << ch<< " ";}return 0;
}

运行结果:

 💫pop_back删除操作

擦除vector的最后一个元素,有效地将其长度减少一个,本质上可以实现vector对象的尾删操作。

 咱们看看代码:

#include <iostream>
#include <vector>using namespace std;int main()
{vector<int> v1{ 1,2,3,4,5 };v1.pop_back();for (auto ch : v1){cout << ch<< " ";}return 0;
}

 运行结果:

 💫insert插入操作

insert函数可以在任意的指定位置进行插入。

咱们看看代码:

#include <iostream>
#include <vector>using namespace std;int main()
{vector<int> v1{ 1,2,3,4,5 };for (auto ch : v1){cout << ch<< " ";}cout << endl;// 在下标为0的位置插入一个10vector<int>::iterator it1 = v1.begin();v1.insert(it1, 10);for (auto ch : v1){cout << ch << " ";}cout << endl;// 在下标为0的位置插入三个100vector<int>::iterator it2 = v1.begin() + 1;v1.insert(it2, 3, 100);for (auto ch : v1){cout << ch << " ";}cout << endl;return 0;
}

运行结果:

  💫erase删除操作

erase函数则可以做到在vector对象中的任意位置删除指定元素或者删除一段区间的元素。

咱们看看代码:

#include <iostream>
#include <vector>using namespace std;int main()
{vector<int> v1{ 1,2,3,4,5,6,7,8,9};for (auto ch : v1){cout << ch << " ";}cout << endl;// 删除一个元素vector<int>::iterator it1 = v1.begin();v1.erase(it1);for (auto ch : v1){cout << ch << " ";}cout << endl;// 删除一段元素vector<int>::iterator it2 = v1.begin();v1.erase(it2 + 1, v1.end() - 1);for (auto ch : v1){cout << ch << " ";}cout << endl;return 0;
}

运行结果:

 💫swap

clear函数是将全部的数据删除。

这里就不用代码演示了。

🌙Non-member function (非成员函数)

本来是不打算来讲解这块的,这里面只讲解swap,这个函数很容易搞混,在c++中有三个swap,每一个swap的作用都不一样,我们这里只讲解:

容器x的内容与y的内容交换。两个容器对象的类型必须相同(模板参数相同),但大小可能不同。

🌟结束语

       今天内容就到这里啦,时间过得很快,大家沉下心来好好学习,会有一定的收获的,大家多多坚持,嘻嘻,成功路上注定孤独,因为坚持的人不多。那请大家举起自己的小手给博主一键三连,有你们的支持是我最大的动力💞💞💞,回见。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/266069.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

物料分类帐概览

原文地址&#xff1a;Overview: What is SAP Material Ledger? | SAP Blogs 物料分类账是收集物料主数据存储在物料主数据中的物料交易数据的工具。 物料分类帐使用此数据来计算价格以评估这些物料。 物料台账是实际成本核算的基础。它允许以多种货币对材料库存进行评估&am…

Java程序编写(下)

Scanner对象 通过java.util.Scanner来获得Scanner类 基本语法&#xff1a; Scanner s new Scanner(System.in)通过next()和nextLine()方法获取输入的字符串。 通过hasNext()和hasNextLine()判断是否还有输入的数据。 next(): 1、一定要读取到有效字符后才可以结束输入 2、…

15:00面试,15:06就出来了,问的问题太变态了。。。

刚从小厂出来&#xff0c;没想到在另一家公司我又寄了。 在这家公司上班&#xff0c;每天都要加班&#xff0c;但看在钱给的比较多的份上&#xff0c;也就不太计较了。但万万没想到5月一纸通知&#xff0c;所有人不准加班了&#xff0c;不仅加班费没有了&#xff0c;薪资还要降…

自定义异步任务管理器和线程

import com.lancoo.common.utils.Threads; import com.lancoo.common.utils.spring.SpringUtils;import java.util.TimerTask; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit;/*** 异步任务管理器* * author lancoo*/ public c…

单片机控制步进电机

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、步进电机的工作原理是什么&#xff1f;二、连线图三、程序1.参考程序2.实际测试 四、开发板1.原理图2.实际连接图3.参考程序4.测试5. 思考 五、步距角总结 …

搭乘“低代码”快车,引领食品行业数字化转型全新升级

数字化技术作为重塑传统行业重要的力量&#xff0c;正以不可逆转的趋势改变着企业经营与客户消费的方式。 在近些年的企业数字化服务与交流过程中&#xff0c;织信团队切实感受到大多数企业经营者们从怀疑到犹豫再到焦虑最终转为坚定的态度转变。 在这场数字化转型的竞赛中&a…

Linux网络——高级IO

目录 一.五种IO模型 1.阻塞式IO 2.非阻塞式IO 3.信号驱动IO 4.多路转接IO&#xff1a; 5.异步IO 二.同步通信 vs 异步通信 三.设置非阻塞IO 1.阻塞 vs 非阻塞 2.非阻塞IO 3.实现函数SetNoBlock 四.I/O多路转接之select 1.初识select 2.select函数原型 3.socket就绪…

第 9 部分 — 内存增强 Transformer 网络:数学见解

一、说明 在顺序数据处理领域&#xff0c;传统的 Transformer 架构擅长处理短期依赖性&#xff0c;但在需要大量内存和长序列上下文保留的任务中表现不佳。在这篇综合博客中&#xff0c;我打算探索一种新颖的混合方法&#xff0c;将 Transformer 与显式长期记忆模块集成在一起。…

AX和A(T)X的区别是?

目录 1.快速了解的例子&#xff1a; &#xff08;1&#xff09;假设所有节点的初始特征都是[1, 0, 0] &#xff0c;那么AX的结果是&#xff1a; &#xff08;2&#xff09; 的结果是&#xff1a; (3) 总结&#xff1a; 2.计算结构系数的例子 &#xff08;1&#xff09…

Vue学习计划-Vue2--VueCLi(三)ref属性、mixins混入、插件、scoped样式

1. ref属性 被用来给元素或子组件注册引用信息&#xff08;id的替代者&#xff09;应用在html标签上获取的是真实DOM元素&#xff0c;应用在组件标签上是组件实例对象&#xff08;VC&#xff08;VueComponent&#xff09;&#xff09;使用方式&#xff1a; 打标识<h1 ref&q…

安装Anconda时出现Failed to extract packages的解决方法

目录 1. 问题所示2. 原理分析3. 解决方法 1. 问题所示 在win7安装Anconda的时候&#xff0c;出现Failed to extract packages 截图如下所示&#xff1a; 2. 原理分析 该版本过于新&#xff0c;无法兼容win7系统&#xff0c;要么更换系统 要么将anconda版本降低即可 3. 解决…

Axure简单安装与入门

目录 一.Axure简介 二.应用场景 三.安装与汉化 3.1.安装 3.2.汉化 四. 入门 4.1.复制、剪切及粘贴区域 4.2.选择模式 4.3. 插入形状 4.4.预览、共享 感谢大家观看&#xff01;希望能帮到你哦&#xff01;&#xff01;&#xff01; 一.Axure简介 Axure RP是一款专业的原型…