当前位置:网站首页>STL container - basic operation of queue and deque
STL container - basic operation of queue and deque
2022-07-27 09:13:00 【Doraemon 0219】
One .queue
1.queue The initialization
queue<Type> name; Type It can be int,float,double,char Even structure type, etc .
queue<int>q1;
queue<double>q2;
queue<char>q3;
2.queue Common functions of
Because the queue (queue) Itself is a kind of first in, first out restriction data structure , So in STL Only through front() To visit Team leader Elements , Or through back() To visit A party Elements .
push() Insert an element at the end of the team
pop() Delete the first element of the queue , no return value
size() Return the number of elements in the queue
empty() If the queue is empty, return true
Access elements :
front() Returns the first element in the queue
back() Returns the last element in the queue 3. visit queue All elements in
while(q.size()!=0)
{
cout<<q.front().name<<" ";
deque.pop();
}Be careful ,queue It's a single ended queue , Not a linear structure , Iterator access and subscript access are not allowed ! and deque( deque ) Sure !!
Two .deque( deque )
deque The container is a given type The elements of Linear processing , Like a vector , it Can access any element quickly and randomly , And it's efficient Insert and delete The tail element of the container . But it has something to do with vector Different ,deque It's a double ended array , and vector It's single ended .deque Supports efficient insertion and deletion of header elements of containers , So it's also called deque .
Deque characteristic :
- deque On the interface and vector Very similar , It can be directly replaced in many operation places .
- deque Random access to elements ( Direct access to index values is supported , use [] Operator or at() Method )
- deque Adding or removing elements from the head and tail is very fast , But it's time-consuming to insert or remove elements in the middle .
1. initialization
deque<Type> name;
deque<int> deque;
2.deque Common functions of
deque.push_back(t); // Add a data at the end of the container t
deque.push_front(t); // Insert a data into the head of the container t
deque.pop_back(); // Delete the last data in the container
deque.pop_front(); // Delete the first data of the container
deque.size(); // Returns the number of elements in the container
deque.empty(); // Judge whether the container is empty
deque.clear(); // Clear queue 3.deque Data access and element access
Data access
First of all Use subscript operation deque[0] = 100;
second Use at Method Such as : deque.at(2) = 100;
Third The reference returned by the interface deque.front() and deque.back()
Be careful : The first and second methods must pay attention to cross-border !!Element access
The first way
while(deque.size()!=0)
{
cout<<deque.front().name<<" ";
deque.pop_front();
}The second way ( Iterators access )
for(auto p=deque.begin();p!=deque.end();p++)
{
cout<<(*p)<<" ";
}The third way ( The subscript access )
for(int i=0;i<deque.size();i++)
{
cout<<deque[i]<<" ";
}边栏推荐
- Built in method of tensorflow model training and evaluation
- How to register code cloud account
- 5G没能拉动行业发展,不仅运营商失望了,手机企业也失望了
- 博客怎么上传动态gif图
- QDoubleValidator不生效问题解决办法
- "Weilai Cup" 2022 Niuke summer multi school training camp 1
- Nut joke based on arkui ETS
- Ztree custom title attribute
- What if the parameters in QT are structs or custom classes when sending signals?
- Test picture
猜你喜欢

The execution sequence of async/await, macro tasks and micro tasks

PyTorch自定义CUDA算子教程与运行时间分析

Data interaction based on restful pages

Deep understanding of Kalman filter (3): multidimensional Kalman filter

Deep understanding of Kalman filter (1): background knowledge

async/await的执行顺序以及宏任务和微任务

Understand various IOU loss functions in target detection

PyQt5快速开发与实战 4.1 QMainWindow
![[micro service ~sentinel] sentinel dashboard control panel](/img/df/2fbe7826ea2b80a81d29351052ae28.png)
[micro service ~sentinel] sentinel dashboard control panel

巴比特 | 元宇宙每日必读:广州南沙发布“元宇宙九条”措施,平台最高可获得2亿元资金支持...
随机推荐
Music experience ceiling! Emotional design details of 14 Netease cloud music
ES6 new - object part
软件测试功能测试全套常见面试题【功能测试-零基础】必备4-1
基于restful页面数据交互
Deep understanding of Kalman filter (1): background knowledge
"Weilai Cup" 2022 Niuke summer multi school training camp 1
Two tips in arkui framework
基于ArkUI eTS开发的坚果食谱(NutRecipes
vscod
QT uses SQLite to open multiple database files at the same time
Some practical, commonly used and increasingly efficient kubernetes aliases
CUDA programming-02: first knowledge of CUDA Programming
DNS domain name space
【云驻共创】华为云:全栈技术创新,深耕数字化,引领云原生
SQL exercise set
拍卖行做VC,第一次出手就投了个Web3
如何注册码云账号
[I2C reading mpu6050 of Renesas ra6m4 development board]
Software testing function testing a full set of common interview questions [function testing - zero foundation] essential 4-1
Restful