当前位置:网站首页>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 .
边栏推荐
- 【Es6】详细解说Set ,Array的常用对象及其他方法(完整版)
- Actual cases of data analysis and data mining local house price prediction (716):
- 第十届泰迪杯数据挖掘挑战赛A题害虫识别YOLOv5模型代码(已跑通,原创作品,持续更新)
- User management - paging
- (精讲)Es6 剩余参数,ES6内置对象,模板字符串内容(详例宝典)及灵活运用项目的实战案例
- 机器学习之PCA特征降维+案例实践
- Database training 7 [index and creation of data integrity constraints]
- uniapp 表单(input、radio、picker)提交获取参数值
- 【Es6】利用添加数据,筛选并传输至页面等多项功能实现案例
- Uniapp uses uview to realize folding panel
猜你喜欢

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

Submit the uniapp form (input, radio, picker) to get the parameter value

【C语言_复习_学习第二课】什么是进制?进制之间应该如何转换

Actual cases of data analysis and data mining local house price prediction (716):

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

小程序editor富文本编辑使用及rich-text解析富文本
![Database training 7 [index and creation of data integrity constraints]](/img/7d/2855d945c0d7ffb970634451b600a1.png)
Database training 7 [index and creation of data integrity constraints]

Simply and quickly establish a pytorch environment yolov5 target detection model to run (super simple)

数据库实训7【索引与数据完整性约束的创建】

Baidu map realizes thermal map
随机推荐
es6新增-函数部分
【Es6】详细解说Set ,Array的常用对象及其他方法(完整版)
无重复字符的最长字串
STL容器——vector的基本操作
Internship project 1 - personalized homepage configuration
Base64 and file conversion
[p5.js] simulated fireworks effect - interactive media design assignment
关于New_Online_Judge_1081_哥德巴赫猜想的思考
机器学习之特征提取(类别特征进行数值化、离散化、文本特征进行数值化)
学习C语言第二天
微信小程序云开发使用方法-1
6S参数
es6新增-对象部分
【C语言—零基础第九课】函数中的爱恨情仇
【C语言_复习_学习第二课】什么是进制?进制之间应该如何转换
MySQL optimization
Database training 7 [index and creation of data integrity constraints]
一个问题的探讨
About the current response, the method getoutputstream() has been called
【C语言—零基础第十一课】旋转大转盘之指针