当前位置:网站首页>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)范围内的数据排序
边栏推荐
- B+树(4)联合索引 --mysql从入门到精通(十六)
- Kubernetes----PV和PVC的生命周期简介
- 0 basic programming resources (collect first ~ read slowly ~)
- Shell variables and references
- Today in history: IBM obtained the first patent; Verizon acquires Yahoo; Amazon releases fire phone
- Display inline+calc realizes left, middle and right layout, and the middle is adaptive
- Kubernetes flannel: host-gw mode
- Flutter integrated Aurora push
- 【5GC】什么是5G切片?5G切片如何工作?
- Kubelet CRI container runtime
猜你喜欢

A college archives management system based on asp.net
![[typescript] typescript common types (Part 1)](/img/80/5c8c51b92d3a9d76f38beba7be0aa6.png)
[typescript] typescript common types (Part 1)

新功能 | 智能开放搜索上线定制词权重模型

The programmed navigation route jumps to the current route (the parameters remain unchanged), and if it is executed multiple times, it will throw a navigationduplicated warning error?

The best engineer was "forced" away by you like this!

Kubernetes ---- life cycle introduction of PV and PVC

食品安全 | 随便果可以”随便“吃吗?

【TypeScript】TypeScript常用类型(上篇)

Kubelet CRI 容器运行时

Today in history: IBM obtained the first patent; Verizon acquires Yahoo; Amazon releases fire phone
随机推荐
基于C#开放式TCP通信建立与西门子PLC的socket通信示例
Detailed explanation of redis's single login
【TypeScript】TypeScript常用类型(上篇)
B+树索引使用(9)分组、回表、覆盖索引(二十一)
Can MySQL customize variable parameter storage functions?
B+树索引使用(8)排序使用及其注意事项(二十)
Kubernetes - Introduction to PV and PVC of advanced storage
Student examination system based on C #
[typescript] typescript common types (Part 2)
概率论与数理统计
新功能 | 智能开放搜索上线定制词权重模型
关于图片地址链接复制问题
从其他文件触发pytest.main()注意事项
Sword finger offer (21): push in and pop-up sequence of stack
About the copy of picture address links
Kubernetes ---- life cycle introduction of PV and PVC
Extra(5)—mysql执行计划(五十一)
How to optimize a large number of if else [email protected] Detailed explanation of valib
【5GC】什么是5G切片?5G切片如何工作?
Flutter dart generates a list of random numbers that are not repeated in n intervals