当前位置:网站首页>Some practical operations of vector
Some practical operations of vector
2022-07-26 13:11:00 【PUdd】
#include <vector>
using namespace std;
establish 、 Copy
vector<int> a; // Create an empty vector
vector<int> a(n); // Create a containing n Object's vector,
// At this time, the data has been generated by default , For example, the current array is full of 0
vector<int> a(n, elem);// Create a containing n individual elem Copy vector
vector<int> a1(a2); // Copy a vector
vector<int> a1(begin, end); // take [begin,end) Copy the data in the range
visit
a.begin() Returns a pointer to the first object , That is commonly known as iterators
a.end() Returns a pointer to the next position of the last object
a.begin()+1 Returns a pointer to the next object of the first object
a.end()-1 Returns a pointer to the last object
a.rbegin() Returns a pointer to the last object , reverse iterator
a.rend() Returns a pointer to the previous position of the first object , reverse iterator
a.front() Return the data of the first object , and *a.begin() It's the same
a.back() Return the data of the last object
a.at(i) Return number i Object data at location , Will check whether the data exists
a[i] Return number i Object data at location
Insert
a.push_back(i); // The simplest insertion , Direct to vector Add a data at the end
a.insert(pos,elem); // towards pos Insert an object before the object pointed to by the iterator , Attention is in front of the object
a.insert(pos, n, elem); // towards pos Insert before the object pointed to by the iterator n Same objects
a.insert(pos, begin, end); // towards pos Insert before the object pointed to by the iterator [begin,end) Between the objects
The last three functions will return the location of the new data
Delete
a.clear(); // Delete all objects
a.pop_back(); // Delete the last object
a.erase(pos); // Delete pos The object corresponding to the iterator
a.erase(begin, end); // Delete [begin,end) Between the objects
The last two functions will return the next location of the deleted data
Be careful , Both insert and delete will result in an iterator pointing to the position after the inserted or deleted iterator , The pointer , Citation failure such as
start = a.begin(); end = a.end(); a.insert(start, elem);
// stay begin Insert data before a.erase(start, end); // Will report a mistake , Because at this time end It's invalid
After the above code is reordered, it can be run :start = a.begin(); end = a.end(); it = a.erase(start, end);
// Save the deleted iterator a.insert(it, elem); // stay it Insert data before It's here for convenience , There is no specific type of write iterator
assignment
a[1] = 1; // Order No 1 The object data of is 1
a.assign(1, 1); // Make a by {1}
a.assign(begin,end); // Put the other vector in [begin,end) The data in is assigned to a
Note that the results of the first and second lines are completely different ,assign Function is a bit similar to copy function , It is the operation of the whole
Other common functions :size、empty、swap、sort
a.size() return vector The number of elements in
a.empty() return vector Is it empty , Empty return 1, Not empty return 0
a1.swap(a2); // In exchange for a1,a2 data
swap(a1, a2); // In exchange for a1,a2 data , ditto
swap(a1[1], a1[2]); // In exchange for a1 Of the 2 Data and 3 Data
sort(begin, end); // Yes [begin,end) Sort the data in the range
边栏推荐
- Create EOS account action
- PostgreSQL official website download error
- A college archives management system based on asp.net
- 一笔画问题(中国邮递员问题)
- 最好的工程师,就是这样被你“逼”走的!
- 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
- 基于Bézier曲线的三维造型与渲染
- Kubernetes----高级存储之PV和PVC简介
- 火山引擎云上增长方案全景:30+方案齐出,兵发优势领域
- 维度灾难 维数灾难 暂记
猜你喜欢

华为年内二度招聘“天才少年”;540万Twitter账号信息泄露,卖价3万美元;谷歌解雇了相信AI有意识的工程师|极客头条...

Sword finger offer 24. reverse the linked list

同站攻击(相关域攻击)论文阅读 Can I Take Your Subdomain?Exploring Same-Site Attacks in the Modern Web

Elementary notes of binary tree

Kubernetes - Introduction to PV and PVC of advanced storage

维度灾难 维数灾难 暂记

Today in history: IBM obtained the first patent; Verizon acquires Yahoo; Amazon releases fire phone

Food safety | these common foods are poisonous! Check your dining table quickly
![[5gc] what is 5g slice? How does 5g slice work?](/img/8c/52ba57d6a18133e97fa00b6a7cf8bc.png)
[5gc] what is 5g slice? How does 5g slice work?

Panorama of volcanic engine cloud growth plan: 30 + plans come out together, and military development advantage areas
随机推荐
基于ASP.NET的某高校学院档案管理系统
Learn about Pinia state getters actions plugins
一笔画问题(中国邮递员问题)
C regards type as generic type T and uses it as generic type of method
维度灾难 维数灾难 暂记
Can MySQL customize variable parameter storage functions?
B+树(4)联合索引 --mysql从入门到精通(十六)
基于WebRTC和WebSocket实现的聊天系统
The child component triggers the defineemits of the parent component: the child component passes values to the parent component
Mysql数据目录(1)---数据库结构(二十四)
Shutter background graying effect, how transparency, gray mask
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
[upper computer tutorial] Application of integrated stepping motor and Delta PLC (as228t) under CANopen communication
The strongest tool class of entity mapping: mapstruct Zhenxiang
mqtt send receive
0基础编程资源大全(先收藏~慢慢看~)
父组件访问子组件的方法或参数 (子组件暴漏出方法defineExpose)
Kubernetes ---- life cycle introduction of PV and PVC
2022 employment season! Adobe helps creative industry workers break through the shackles of skills and return to the source of ability
A college archives management system based on asp.net