当前位置:网站首页>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 .
边栏推荐
- 泰迪杯A题完整版 优化更新(4/23)
- 【Es6】利用添加数据,筛选并传输至页面等多项功能实现案例
- 【2022第十届‘泰迪杯’挑战赛】A题:害虫识别完整版(大致思路。详细过程和代码以及结果csv在压缩包中)
- 简单快速建立pytorch环境YOLOv5目标检测 模型跑起来(超简单)
- LeetCode53. 最大子数组和
- 微信小程序获取年月日周及早上、中午、晚上
- 02_电影推荐(ContentBased)_用户画像
- 【C语言_复习_学习第二课】什么是进制?进制之间应该如何转换
- Travel data acquisition, data analysis and data mining [2022.5.30]
- 基于cuda10.0的pytorch深度学习环境配置
猜你喜欢
随机推荐
User - registration / login
Use of transactions - Django, SQL tools
Wechat applet obtains the week, morning, noon and evening of month, year and day
【C语言—零基础第八课】循环结构与break continue
模拟库函数
使用js中的(offset,page)实现登录效果
使用Echars实现水滴状、环形图、分割图、堆叠、组织架构图、地图轮廓等图表
小程序editor富文本编辑使用及rich-text解析富文本
Teddy Cup title a full version optimization update (4/23)
小程序云开发 上传图片到云存储
ThreadLocal thread safety example and its principle
uniapp中使用ucharts图表,饼状图,柱状图,折线图
es6新增-let和const (var的缺点&let及const)
基于cuda10.0的pytorch深度学习环境配置
Three high concurrency methods to realize I++
【C】 Beam calculator
游玩数据获取与数据分析、数据挖掘 【2022.5.30】
Pygame:外星人入侵
IDL调用6S大气校正
C语言 带你 手撕 通讯录









