当前位置:网站首页>vector的一些实用操作
vector的一些实用操作
2022-07-26 13:07:00 【PUdd】
#include <vector>
using namespace std;
创建、复制
vector<int> a; //创建一个空的vector
vector<int> a(n); //创建一个含有n个对象的vector,
//此时数据已缺省构造产生,比如现在的数组里全是0
vector<int> a(n, elem);//创建一个含有n个elem拷贝的vector
vector<int> a1(a2); //复制一个vector
vector<int> a1(begin, end); //将[begin,end)范围内的数据复制过来
访问
a.begin() 返回指向首个对象的指针,也就是一般所说的迭代器
a.end() 返回指向最后一个对象的下一个位置的指针
a.begin()+1 返回指向首个对象的下一个对象的指针
a.end()-1 返回返回指向最后一个对象的指针
a.rbegin() 返回指向最后一个对象的指针,反向迭代器
a.rend() 返回指向首个对象的前一个位置的指针,反向迭代器
a.front() 返回首个对象的数据,和*a.begin()是一样的
a.back() 返回最后一个对象的数据
a.at(i) 返回编号i位置处的对象数据,会检查数据是否存在
a[i] 返回编号i位置处的对象数据
插入
a.push_back(i); //最简单的插入,直接向vector末尾加入一个数据
a.insert(pos,elem); //向pos迭代器指向的对象前插入一个对象,注意是对象前
a.insert(pos, n, elem); //向pos迭代器指向的对象前插入n个相同对象
a.insert(pos, begin, end); //向pos迭代器指向的对象前插入[begin,end)之间的对象
后三个函数都会返回新数据的位置
删除
a.clear(); //删除所有对象
a.pop_back(); //删除最后一个对象
a.erase(pos); //删除pos迭代器对应的对象
a.erase(begin, end); //删除[begin,end)之间的对象
后两个函数会返回被删除数据的下一个位置
注意,插入和删除都会导致指向插入或者删除的迭代器之后的位置的迭代器,指针,引用失效 比如
start = a.begin(); end = a.end(); a.insert(start, elem);
//在begin前插入数据 a.erase(start, end); //会报错,因为此时end已经失效
上面的代码调整顺序以后就可以运行了:start = a.begin(); end = a.end(); it = a.erase(start, end);
//保存下删除之后的迭代器 a.insert(it, elem); //在it前插入数据 这里为了省事,没有写迭代器的具体类型
赋值
a[1] = 1; //令编号1的对象数据为1
a.assign(1, 1); //令a为{1}
a.assign(begin,end); //把另一个vector中[begin,end)中的数据赋值给a
要注意第一行和第二行结果是完全不同的,assign函数有点类似复制函数,是对整体的操作
其它常用函数:size、empty、swap、sort
a.size() 返回vector中元素的个数
a.empty() 返回vector是否为空,空返回1,不为空返回0
a1.swap(a2); //交换a1,a2数据
swap(a1, a2); //交换a1,a2数据,同上
swap(a1[1], a1[2]); //交换a1的第2个数据和第3个数据
sort(begin, end); //对[begin,end)范围内的数据排序
边栏推荐
- 0 basic programming resources (collect first ~ read slowly ~)
- Is the account opened by flush safe?
- jvm:类加载子系统干什么的?由什么组成?需要记住哪些八股文?
- Detailed explanation of redis's single login
- 基于WebRTC和WebSocket实现的聊天系统
- Who is responsible for the problems of virtual idol endorsement products? And listen to the lawyer's analysis
- Kubernetes Flannel:HOST-GW模式
- 深度学习3D人体姿态估计国内外研究现状及痛点
- 高通再次「押宝」中科创达,挑战智能驾驶软硬件全栈方案
- B+树索引使用(7)匹配列前缀,匹配值范围(十九)
猜你喜欢

PostgreSQL official website download error

Huawei recruited "talented teenagers" twice this year; 5.4 million twitter account information was leaked, with a selling price of $30000; Google fired engineers who believed in AI consciousness | gee

Vs code set the method of ctrl+s saving and automatic formatting

A college archives management system based on asp.net

Code error reporting and problem solving experience II: test error reporting in yolov5

基于Locust框架进行文件上传下载性能测试

Solution: unable to load the file c:\users\user\appdata\roaming\npm\npx PS1, because running scripts is prohibited on this system.

新功能 | 智能开放搜索上线定制词权重模型
![[typescript] typescript common types (Part 1)](/img/80/5c8c51b92d3a9d76f38beba7be0aa6.png)
[typescript] typescript common types (Part 1)

Kubernetes Flannel:HOST-GW模式
随机推荐
Kubelet CRI container runtime
Kubernetes -- Introduction to common plug-ins of kubernetes
From January to June, China's ADAS suppliers accounted for 9%, and another parts giant comprehensively laid out the new smart drive track
Azure synapse analytics Performance Optimization Guide (2) -- optimize performance using materialized views (Part 1)
JDBC gets connections from the connection pool (Druid connection pool)
Example of establishing socket communication with Siemens PLC based on C # open TCP communication
The "2022 Huawei developer competition eastern China division opening ceremony" was successfully held in Fuzhou
Use float to realize left, middle and right layout, and the middle content is adaptive
Flutter textfield sets the height and automatically wraps lines, and the rounded border removes the underline
Kubernetes flannel: host-gw mode
Reflection, an implementation of automatic repeated call interface
C regards type as generic type T and uses it as generic type of method
Visual stdio(VS)中的(int argc、char** argv)命令行参数
B+树索引使用(7)匹配列前缀,匹配值范围(十九)
Kubernetes ---- life cycle introduction of PV and PVC
Kubernetes----高级存储之PV和PVC简介
Flutter prevents scientific counting and removes mantissa invalid 0
PXE principle and configuration
Huawei ultra fusion fusioncube solution notes
SLAM 02.整体框架