当前位置:网站首页>STL container - basic operation of vector
STL container - basic operation of vector
2022-07-19 05:13:00 【Doraemon 0219】
Catalog
One 、vector Statement of ( initialization )
Two 、 increase : Additive elements
3、 ... and 、 Delete : Remove elements
Four 、 Change : Modifying elements
5、 ... and 、 check : Traversing elements
Preface
C++ STL Is a very powerful container Library , Where dynamic array vector It is one of the most convenient containers . Next up vector Some basic operations , The main operation is still “ increase Delete Change check ”:
One 、vector Statement of ( initialization )
vector It's a vector type , It can hold many types of data , Such as several integers , This article takes storage shaping as an example .
Yes vector There are several common ways to initialize , Just master the red marking :
(1)vector <int> ver = vector<int>(); or vector <int> ver;// Defined at this time ver The space occupied is uncertain , New spaces can be added dynamically
(2) vector<int> ver(10); // Defined 10 The vector of an integral element ( The angle bracket is the element type name , It can be any legal data type ), But no initial value is given , Its value is uncertain .
(3)vector<int> ver(10,1); // Defined 10 The vector of an integral element , And the initial value of each element is 1
(4)vector<int> ver(b); // use b Vector to create ver vector , Overall replicability assignment
(5)vector<int> ver(b.begin(),b.begin+3); // Defined ver The value is b pass the civil examinations 0 One to the first 2 individual ( common 3 individual ) Elements
(6)int b[7]={1,2,3,4,5,9,8}; vector<int> ver(b,b+7); // Get the initial value from the array
(7)vector<vector<int>> ver; // Two dimensional vector
Two 、 increase : Additive elements
int t;
cin>>t;
ver.push_back(t);// take t Add to vector End of array
ver.push_front(t);// take t Add to vector At the beginning of the array
ver.insert(ver.begin()+n, t);// take t Insert into ver.begin()+n Parameters here cannot be written int type , Pointer should be written !!
Be careful :If use cin>> Enter data or use subscript ver[i]=t assignment , that vector There must be space , It can be used resize() Give space or use directly push_back(t) Just add elements .
It can also be used during initialization vector <int> ver(n); In this way, we can cin>> Enter data or use subscript ver[i]=t assignment , But this can't be used push_back(t) Add data , because push_back(t) Is to add data to the end !!! The former n The default is 0,push_back(t) It will not be modified before n The value of .
3、 ... and 、 Delete : Remove elements
ver.erase(iterator p);// Delete iterator p The element pointing to the position
ver.erase(ver.begin()+1,ver.begin()+3); // Delete a pass the civil examinations 1 individual ( From 0 Count on it ) To the first 2 Elements , That is to say, deleted elements from a.begin()+1 Count up ( Including it ) Until a.begin()+ 3( It's not included )
ver.pop_back();// Delete the last element
ver.pop_front();// Delete first element
Four 、 Change : Modifying elements
To be added ing…… See the summary for details
5、 ... and 、 check : Traversing elements
// There are two ways to traverse elements :
//1. Subscript traversal
for (int i = 0; i < n; i++)
cout << ver[i] << " ";
//2. Iterator traversal vector <int>::iterator p;
for(auto p=ver.begin();p!=ver.end();p++)
cout << *p << " ";
among auto representative vector <int>::iterator( Iterator type )
Be careful :*(p+1)== *(ver.begin()+1) == ver[i+1]
summary
vector It also contains functions with various functions , Summarized below ( part ):
push_back() // Add a data at the end of the array
pop_back() // Remove the last data of the arrayat() // Get the data of the number position
begin() // Get a pointer to the array header
end() // Get the last cell of the array +1 The pointer to
find() // Determine whether an element existsfront() // Get a reference to the array header
back() // Get a reference to the last cell of the array
max_size() // obtain vector The biggest is how big
capacity() // At present vector The size of the allocation
size() // The size of the data currently used
capacity(); // return a The total number of elements that can be held in memory
a.reserve(100); // To change the current vecotr The size of the allocated space will a The capacity of (capacity) Expand to 100, That is to say, test now a.capacity(); The return value is 100
a.resize(10); // take a Set the number of existing elements to 10 individual , Delete more , Less is more , The default value of the added element is 0
a.resize(10,2); // take a Set the number of existing elements to 10 individual , Delete more , Less is more , The value of the added element is 2
erase() // Delete the data item that the pointer points to
clear() // Clear the current vector
rbegin() // take vector The reverse start pointer returns ( In fact, it is the original end-1)
rend() // take vector The end pointer of the reverse construct returns ( In fact, it is the original begin-1)
empty() // Judge vector Is it empty
swap() // And another vector Exchange data
a.swap(b); //b Vector , take a The elements in and b The elements in are exchanged as a wholereverse(obj.begin(),obj.end()); reverse iterator , Realize element exchange
find(nums.begin(), nums.end(), target)// The return is target The first address , If the return tail address is not found nums.end()sort(a.begin(),a.end(),cmp); // Yes a From a.begin()( Including it ) To a.end()( It's not included ) The elements are arranged from small to large cmp Customizable , There can be no ( If not, the default is from small to large )
copy(a.begin(),a.end(),b.begin()+1); // hold a From a.begin()( Including it ) To a.end()( It's not included ) Copy the elements of to b in , from b.begin()+1 The location of ( Including it ) Start copying , Cover the original elements .
边栏推荐
- Ucharts chart, pie chart, bar chart and line chart are used in uniapp
- [batch] batch delete intermediate folder - personal research script
- 学习C语言的第四天
- 轮播图移动速度(匀速,缓动)案例归总
- Mongo DB aggregate operations and indexes
- es6新增-let和const (var的缺点&let及const)
- 实习项目2-主页配置-我的数据模块
- IText modify PDF Text
- 【Es6】forEach,for...in ,for...of专栏,让你通过项目案例快速分辨各种for语句的使用方式及区别(完整版)内部有详细注释
- Pygame:外星人入侵
猜你喜欢
![Database training 7 [index and creation of data integrity constraints]](/img/7d/2855d945c0d7ffb970634451b600a1.png)
Database training 7 [index and creation of data integrity constraints]

小程序editor富文本编辑使用及rich-text解析富文本

Uni app conditional compilation ifdef ENDIF compatible with multiple terminals

Internship project 2 - Homepage configuration - my data module

vscode终端无法使用解决的办法

The code of yolov5 model for pest identification in Title A of the 10th Teddy cup data mining challenge (has been run through, original works, continuously updated)

Mongo DB aggregate operations and indexes

模拟库函数
![[batch] batch delete intermediate folder - personal research script](/img/6d/699987559bc25998e635a403d9d52d.png)
[batch] batch delete intermediate folder - personal research script

数据分析与数据挖掘实战案例本地房价预测(716):
随机推荐
uniapp 表单(input、radio、picker)提交获取参数值
01_电影推荐(ContentBased)_物品画像
学习C语言第8天
Wechat applet cloud development and use method-1
Wechat applet status bar
STL容器——set集合的应用
热更新及其原理
uniapp中使用ucharts图表,饼状图,柱状图,折线图
The code of yolov5 model for pest identification in Title A of the 10th Teddy cup data mining challenge (has been run through, original works, continuously updated)
mysql数据库实验实训6,数据视图(详细)
User management - restrictions
MySQL optimization
多功能(实现)封装函数
Install MySQL
vscode终端无法使用解决的办法
数据分析与数据挖掘实战案例本地房价预测(716):
学习C语言第三天
es6新增-Symbol数据类型
H5页面使用js生成二维码
读论文《SNUNet-CD: A Densely Connected Siamese Network for Change Detection of VHR Images》