当前位置:网站首页>vector的使用方法_vector指针如何使用
vector的使用方法_vector指针如何使用
2022-07-06 16:29:00 【Java架构师必看】
大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说vector的使用方法_vector指针如何使用,希望能够帮助大家进步!!!
一、什么是vector?
向量(Vector)是一个封装了动态大小数组的顺序容器(Sequence Container)。跟任意其它类型容器一样,它能够存放各种类型的对象。可以简单的认为,向量是一个能够存放任意类型的动态数组。
二、容器特性
1.顺序序列
顺序容器中的元素按照严格的线性顺序排序。可以通过元素在序列中的位置访问对应的元素。
2.动态数组
支持对序列中的任意元素进行快速直接访问,甚至可以通过指针算述进行该操作。提供了在序列末尾相对快速地添加/删除元素的操作。
3.能够感知内存分配器的(Allocator-aware)
容器使用一个内存分配器对象来动态地处理它的存储需求。
三、基本函数实现
1.构造函数
vector():创建一个空vector vector(int nSize):创建一个vector,元素个数为nSize vector(int nSize,const t& t):创建一个vector,元素个数为nSize,且值均为t vector(const vector&):复制构造函数 vector(begin,end):复制[begin,end)区间内另一个数组的元素到vector中
2.增加函数
void push_back(const T& x):向量尾部增加一个元素X emplace_back:向量尾部增加一个元素X 以下两个相等
std::vector<Vertex> MM;
MM.push_back(Vertex(a));只听到从架构师办公室传来架构君的声音: 山石荦确行径微,黄昏到寺蝙蝠飞。有谁来对上联或下联?
此代码由Java架构师必看网-架构君整理
std::vector<Vertex> MM;
MM.emplace_back(a);iterator insert(iterator it,const T& x):向量中迭代器指向元素前增加一个元素x iterator insert(iterator it,int n,const T& x):向量中迭代器指向元素前增加n个相同的元素x iterator insert(iterator it,const_iterator first,const_iterator last):向量中迭代器指向元素前插入另一个相同类型向量的[first,last)间的数据
3.删除函数
iterator erase(iterator it):删除向量中迭代器指向元素 iterator erase(iterator first,iterator last):删除向量中[first,last)中元素 void pop_back():删除向量中最后一个元素 void clear():清空向量中所有元素
4.遍历函数
reference at(int pos):返回pos位置元素的引用 reference front():返回首元素的引用 reference back():返回尾元素的引用 iterator begin():返回向量头指针,指向第一个元素 iterator end():返回向量尾指针,指向向量最后一个元素的下一个位置 reverse_iterator rbegin():反向迭代器,指向最后一个元素 reverse_iterator rend():反向迭代器,指向第一个元素之前的位置
5.判断函数
bool empty() const:判断向量是否为空,若为空,则向量中无元素
6.大小函数
int size() const:返回向量中元素的个数 int capacity() const:返回当前向量所能容纳的最大元素值 int max_size() const:返回最大可允许的vector元素数量值
7.其他函数
void swap(vector&):交换两个同类型向量的数据 void assign(int n,const T& x):设置向量中前n个元素的值为x void assign(const_iterator first,const_iterator last):向量中[first,last)中元素设置成当前向量元素
8.看着清楚
1.push_back 在数组的最后添加一个数据
2.pop_back 去掉数组的最后一个数据
3.at 得到编号位置的数据
4.begin 得到数组头的指针
5.end 得到数组的最后一个单元+1的指针
6.front 得到数组头的引用
7.back 得到数组的最后一个单元的引用
8.max_size 得到vector最大可以是多大
9.capacity 当前vector分配的大小
10.size 当前使用数据的大小
11.resize 改变当前使用数据的大小,如果它比当前使用的大,者填充默认值
12.reserve 改变当前vecotr所分配空间的大小
13.erase 删除指针指向的数据项
14.clear 清空当前的vector
15.rbegin 将vector反转后的开始指针返回(其实就是原来的end-1)
16.rend 将vector反转构的结束指针返回(其实就是原来的begin-1)
17.empty 判断vector是否为空
18.swap 与另一个vector交换数据
今天文章到此就结束了,感谢您的阅读,Java架构师必看祝您升职加薪,年年好运。
边栏推荐
- The tutorial of computer reinstallation win10 system is simple and easy to understand. It can be reinstalled directly without U disk
- Wasserstein slim gain with gradient poverty (wsgain-gp) introduction and code implementation -- missing data filling based on generated countermeasure network
- 在Docker中分分钟拥有Oracle EMCC 13.5环境
- Hydrogen future industry accelerates | the registration channel of 2022 hydrogen energy specialty special new entrepreneurship competition is opened!
- Rider离线使用Nuget包的方法
- Summary of three methods for MySQL to view table structure
- Penetration test --- database security: detailed explanation of SQL injection into database principle
- leetcode:236. The nearest common ancestor of binary tree
- How to find out if the U disk file of the computer reinstallation system is hidden
- [unmanned aerial vehicle] multi unmanned cooperative task allocation program platform, including Matlab code
猜你喜欢

AI金榜题名时,MLPerf榜单的份量究竟有多重?

人均瑞数系列,瑞数 4 代 JS 逆向分析

leetcode:236. 二叉树的最近公共祖先

内网穿透zerotier 外网(手机、电脑等)访问内网设备(树莓派、NAS、电脑等)

Design a red envelope grabbing system

Wind chime card issuing network source code latest version - commercially available

17、 MySQL - high availability + read / write separation + gtid + semi synchronous master-slave replication cluster

公链与私链在数据隐私和吞吐量上的竞争
Detailed explanation of regular expression (regexp) in MySQL

B 站弹幕 protobuf 协议还原分析
随机推荐
CRMEB 商城系统如何助力营销?
The tutorial of computer reinstallation win10 system is simple and easy to understand. It can be reinstalled directly without U disk
Yaduo Sangu IPO
Experiment 6: installing eve-ng
Wu Enda 2022 machine learning course evaluation is coming!
DAY ONE
资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
The important data in the computer was accidentally deleted by mistake, which can be quickly retrieved by this method
The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly
PostgreSQL高可用之repmgr(1主2从+1witness)+Pgpool-II实现主从切换+读写分离
Should the jar package of MySQL CDC be placed in different places in the Flink running mode?
A novice asks a question. I am now deployed on a single machine. I submitted an SQL job and it runs normally. If I restart the service job, it will disappear and I will have to
DAY TWO
Interface joint debugging test script optimization v4.0
【通信】两层无线 Femtocell 网络上行链路中的最优功率分配附matlab代码
每日刷题记录 (十五)
在docker中快速使用各个版本的PostgreSQL数据库
Gradle知识概括
《数字经济全景白皮书》保险数字化篇 重磅发布
Design a red envelope grabbing system