当前位置:网站首页>Detailed explanation of the usage of vector, queue and stack
Detailed explanation of the usage of vector, queue and stack
2022-07-25 00:32:00 【Think-killer】
vector Containers
vector Containers are similar to arrays , Storage space containing a continuous set of addresses . Yes vector You can query 、 Insert 、 Delete and other operations .
1.insert function
Three uses :1.iterator insert(iterator loc,const TYPE &val);
2.void insert(iterator loc,size_type num,const TYPE &val);
3.void insert(iterator loc,input_iterator start,input_iterator end);
e.g:
vector<int>a;
a.insert(a.begin(),99);// Head elements 99
a.insert(a.end(),97);// Trailing element 97
a.insert(a.end(),4,5);// take 4 individual 5 Insert the tail
a.push_back(10);// Additive elements 10 To vector The tail
a.pop_back();// Delete the last element
a.erase(number.begin());// Delete first element
a.size();// return vector Number of current elements .
a.capacity();// return vector The number of elements it can hold .
a.empty();// Judge vector Is it empty .
a.front();// Returns the first number
a.back();// Return to the last element
a.clear();// Empty
vector<int>number(5,99);
// structure number,5 The values are 99 The elements of
vector<int>number1(number);
// structure number1, Copy number Containers
vector<int>number2(number.begin(),number.end());
// structure number2, Copy number The value from the beginning to the end of the container
for(int i=0;i<a.size();i++)
cout<<a[i]<<" ";// Output
for(int i=0;i<a.size();i++)
cout<<a.at(i)<<endl;//at Function output is safer
for(vector<int>::iterator i=a.begin();i!=a.end();i++)
cout<<*i<<endl;// Iterator output ( Equivalent to pointer )
for(auto x:a) cout<<x<<" ";//C++11 The new grammar is more concise and efficient
2. Array weight removal
#include<algorithm>//unique For its library function
sort(a.begin(),a.end());// You have to sort
a.erase(unique(a.begin(), a.end()), a.end()); // because unique The return value of the function is the subscript of the first repeating element , Therefore, you can clear the element from the return address to the end of the container .
// Remove duplicate numbers
3. Support comparison operation
vector<int>a(3,4),b(4,3);
if(a>b) puts("a>b");
// Compare in dictionary order
queue( queue )
queue : fifo
queue<int>q;
q.push();// Insert elements... At the end of the team
q.front();// Return to team leader element
q.pop();// Pop up the header element
q.back();// Return to the end of the team
q.size();// Number of return elements
q.empty();// Determine whether it is null
priority_queue( Priority queue , Default large root heap )
#include<queue>
priority_queue<int>heap;// Default large root heap
// Ascending queue , Small cap pile
2 priority_queue <int,vector<int>,greater<int> > q;
3 // Descending queue , Big pile top
4 priority_queue <int,vector<int>,less<int> >q;
heap.push();// Insert an element
heap.top();// Return to the top of heap element
heap.pop();// Pop up the top element
heap.size();// Returns the number of elements in the queue
heap.empty();// Whether the queue is empty
Can learn from the boss : Add link description
stack( Stack )
#include<stack>
stack<int>a;
a.size();// Number of return elements
a.empty();// Is the stack empty
a.push();// Insert elements to the top of the stack
a.top();// Back to top of stack element
a.pop();// Pop up top element
边栏推荐
- Multi table query of SQL
- torch.nn.SyncBatchNorm.convert_ sync_ Mindspore usage corresponding to batchnorm
- Lambda&Stream
- [acwing周赛复盘] 第 61 场周赛20220723
- Soft test --- fundamentals of programming language (Part 2)
- C language force buckle the eighth question of string conversion integer. Ergodic method
- ROS机械臂 Movelt 学习笔记3 | kinect360相机(v1)相关配置
- 痛并快乐的-NIO编程
- [mindspore] [xception model] script statement is suspected to be wrong
- px rem em
猜你喜欢

Fast development board for Godson solid state drive startup (burning system to solid state) - partition

Advanced multithreading (Part 2)

Wechat applet development learning 5 (custom components)

The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode

Tencent low code platform is officially open source! You can drag and drop and generate mobile phone projects and PC projects! Get private benefits

Oracle is not null cannot filter null values

C language: deep analysis function stack frame

First experience of flask
![[acwing weekly rematch] 61st weekly 20220723](/img/8b/df2c8d516db1e7e5f2d50bcf62b2b1.png)
[acwing weekly rematch] 61st weekly 20220723
![[LeetCode周赛复盘] 第 303 场周赛20220724](/img/ba/0f16f1f42e4a2593ec0124f23b30d7.png)
[LeetCode周赛复盘] 第 303 场周赛20220724
随机推荐
Palm package manager of kubernetes learning offline installation of NFS client provider
Detailed usage of iperf
The font changes with the change of the form
Redis memory analysis tool RMA usage
Heap and stack in embedded development
[acwing weekly rematch] 61st weekly 20220723
[mindspore] [xception model] script statement is suspected to be wrong
[Bert] QA, reading comprehension, information retrieval
[untitled]
The use of Multimeter in circuit analysis experiment of Shandong University
1. Smoke test
软考 --- 程序设计语言基础(下)
Uncaught typeerror: cannot read properties of null (reading 'append') solution
Which automation tools can double the operation efficiency of e-commerce?
Install and configure php5-7 version under centos7.4
Log4j configuration file
EF core: self referencing organizational structure tree
Notes on topic brushing (XXII) -- Dynamic Planning: basic ideas and topics
Moonpdflib Preview PDF usage record
LeetCode_ 392_ Judgement subsequence