当前位置:网站首页>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 .
边栏推荐
- 微信小程序wx.setClipboardData复制文本
- H5页面使用js生成二维码
- es6新增-对象部分
- 小程序云开发表单提交并在页面中获取数据
- Ucharts chart, pie chart, bar chart and line chart are used in uniapp
- Using JS to realize the second level menu of anjuke and the full version (demonstration of precautions and problem points)
- Infinite classification
- 02_ Movie recommendation (contentbased)_ User portrait
- 决策树原理和案例应用-泰坦尼克号生存预测
- 泰迪杯A题完整版 优化更新(4/23)
猜你喜欢

How to upload qiniu cloud

实习项目1-个性化主页配置

Feature extraction of machine learning (digitization and discretization of category features and digitization of text features)

Use of transactions - Django, SQL tools
![Travel data acquisition, data analysis and data mining [2022.5.30]](/img/d3/7222f852f8a8ba8cddc22a55d90fad.png)
Travel data acquisition, data analysis and data mining [2022.5.30]

百度地图 实现 热力图

机器学习之PCA特征降维+案例实践

微信小程序云开发使用方法-1

基于RTX30显卡的ArcGIS Pro2.8深度学习环境配置
![[batch] batch delete intermediate folder - personal research script](/img/6d/699987559bc25998e635a403d9d52d.png)
[batch] batch delete intermediate folder - personal research script
随机推荐
NVIDIA GeForce Experience登录报错:验证程序加载失败,请检查您的浏览器设置,例如广告拦截程序(解决办法)
[2022 10th Teddy Cup Challenge] Title A: complete version of pest identification (general idea. Detailed process and code and results CSV in compressed package)
IDL调用6S大气校正
获取数组中对象内部的数值最大与最小值多功能版及点名系统完整版并展示效果
es6新增-数组部分
Feature extraction of machine learning (digitization and discretization of category features and digitization of text features)
Applet cloud development upload pictures to cloud storage
About the current response, the method getoutputstream() has been called
Database training 7 [index and creation of data integrity constraints]
泰迪杯A题完整版 优化更新(4/23)
Applet cloud development form submission and data acquisition in the page
vscode终端无法使用解决的办法
PAT乙级1002:写出这个数
The principle and local storage of the throttle valve of the rotation chart are summarized
Convolutional neural network
<script>标签内容详解
STL容器——set集合的应用
SQL statement learning
Pygame:外星人入侵
数据可视化