当前位置:网站首页>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架构师必看祝您升职加薪,年年好运。
边栏推荐
- MATLIB reads data from excel table and draws function image
- Summary of three methods for MySQL to view table structure
- Gradle knowledge generalization
- 英国都在试行4天工作制了,为什么BAT还对996上瘾?
- matplotlib画柱状图并添加数值到图中
- How does win11 restore the traditional right-click menu? Win11 right click to change back to traditional mode
- 若依请求url中带有jsessionid的解决办法
- 【2022全网最细】接口测试一般怎么测?接口测试的流程和步骤
- 每年 2000 亿投资进入芯片领域,「中国芯」创投正蓬勃
- MySQL implementation of field segmentation from one line to multiple lines of example code
猜你喜欢
How to implement Lua entry of API gateway
Gradle知识概括
Hydrogen future industry accelerates | the registration channel of 2022 hydrogen energy specialty special new entrepreneurship competition is opened!
Please help xampp to do sqlilab is a black
Résumé des connaissances de gradle
Master binary tree in one article
氢创未来 产业加速 | 2022氢能专精特新创业大赛报名通道开启!
若依请求url中带有jsessionid的解决办法
What should I do if the USB flash disk data is formatted and how can I recover the formatted USB flash disk data?
1000字精选 —— 接口测试基础
随机推荐
leetcode:236. The nearest common ancestor of binary tree
传统企业要为 Web3 和去中心化做的 11 个准备
Matplotlib draws a histogram and adds values to the graph
pinia 模块划分
matplotlib画柱状图并添加数值到图中
快手的新生意,还得靠辛巴吆喝?
每年 2000 亿投资进入芯片领域,「中国芯」创投正蓬勃
[communication] optimal power allocation in the uplink of two-layer wireless femtocell network with matlab code
【系统分析师之路】第七章 复盘系统设计(面向服务开发方法)
AI金榜题名时,MLPerf榜单的份量究竟有多重?
openresty ngx_lua子请求
Basic chart interpretation of "Oriental selection" hot out of circle data
I've been laid off, and I'll lose money for everything. The days when I once made a monthly salary of 20000 are not coming back
If the request URL contains jsessionid, the solution
若依请求url中带有jsessionid的解决办法
DAY SIX
[system analyst's road] Chapter 7 double disk system design (service-oriented development method)
Common modification commands of Oracle for tables
JDBC programming of MySQL database
Wasserstein slim gain with gradient poverty (wsgain-gp) introduction and code implementation -- missing data filling based on generated countermeasure network