数据结构:STL:queue stack

目录

1.queue的头文件

2.queue的定义

3.queue的常用函数

[3.1 push()](#3.1 push())

[3.2 pop()](#3.2 pop())

[3.3 size()](#3.3 size())

[3.4 empty()](#3.4 empty())

[3.5 front()](#3.5 front())

[3.6 back()](#3.6 back())

4.stack的头文件

5.stack的定义

6.stack的常用函数

[6.1 push()](#6.1 push())

[6.2 top()](#6.2 top())

[6.3 pop()](#6.3 pop())

[6.4 size()](#6.4 size())

[6.6 empty()](#6.6 empty())


STLf封装的queue也是十分的强大,一个queue不但可以实现队列,还可以实现循环队列。、

1.queue的头文件

复制代码
#include<queue>

2.queue的定义

queue的定义和vector没什么不同

复制代码
queue<类型名>变量名;

类型可以是int,double等基本数据类型,也可以是自定义的结构体,也可以是STL容器。

复制代码
queue<int>q;
queue<double>q;
queue<char>q;
queue<char,list<char>>q;

3.queue的常用函数

3.1 push()

push()在队尾插入一个元素

复制代码
queue<int>q;
q.push(1);
q.push(2);
cout<<q.front()<<endl;

输出:

复制代码
1

3.2 pop()

pop()删除队首元素

复制代码
qqueue<int>q;
q.push(1);
q.push(2);
q.pop();
cout<<q.front()<<endl;

输出:

复制代码
2 //1已经被删除掉

3.3 size()

size()返回队列中元素个数

复制代码
1
0

queue<int>q;
q.push(1);
q.push(2);
cout<<q.size()<<endl;

输出:

复制代码
2

3.4 empty()

empty()判断队列是否为空,如果队列为空返回true

复制代码
queue<int>q;
cout<<q.empty()<<endl;
q.push(1);
q.push(2);
cout<<q.empty()<<endl;

输出:

复制代码
1
0

3.5 front()

front()返回队首元素

复制代码
queue<int>q;
q.push(1);
q.push(2);
q.push(3);
cout<<q.front()<<endl;
q.pop();
cout<<q.front()<<endl;

输出:

复制代码
1
2

3.6 back()

back()返回队尾元素

复制代码
queue<int>q;
q.push(1);
q.push(2);
cout<<q.back()<<endl;

输出:

复制代码
2

4.stack的头文件

复制代码
#include<stack>

5.stack的定义

复制代码
stack<类型名>变量名;

类型可以是int,double等基本数据类型,也可以是自定义的结构体,也可以是STL容器。

6.stack的常用函数

6.1 push()

push()将元素压入栈中

复制代码
stack<int>st;
st.push(1);
st.push(2);

6.2 top()

因为栈是一种先进后出的数据结构,我们只能访问栈顶元素

复制代码
stack<int>st;
st.push(1);
st.push(2);
cout<<st.top()<<endl;

输出:

复制代码
2

6.3 pop()

pop()用来弹出栈顶元素

复制代码
stack<int>st;
st.push(1);
st.push(2);
cout<<st.top()<<endl;
st.pop();
cout<<st.top()<<endl;

输出:

复制代码
2
1

6.4 size()

size()返回栈中元素个数

复制代码
stack<int>st;
st.push(1);
st.push(2);
st.push(3);
cout<<st.size()<<endl;

输出:

复制代码
3

6.6 empty()

empty()用来判断栈是否为空,为空返回1,表示true;不为空返回0,表示false

复制代码
stack<int>st;
cout<<st.empty()<<endl;
st.push(1);
st.push(2);
cout<<st.empty()<<endl;

输出:

复制代码
1
0
相关推荐
草莓熊Lotso6 小时前
Linux 文件描述符与重定向实战:从原理到 minishell 实现
android·linux·运维·服务器·数据库·c++·人工智能
历程里程碑6 小时前
Linux22 文件系统
linux·运维·c语言·开发语言·数据结构·c++·算法
在路上看风景13 小时前
19. 成员初始化列表和初始化对象
c++
zmzb010313 小时前
C++课后习题训练记录Day98
开发语言·c++
ValhallaCoder13 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
念风零壹14 小时前
C++ 内存避坑指南:如何用移动语义和智能指针解决“深拷贝”与“内存泄漏”
c++
孞㐑¥15 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法
月挽清风15 小时前
代码随想录第十五天
数据结构·算法·leetcode
NEXT0615 小时前
前端算法:从 O(n²) 到 O(n),列表转树的极致优化
前端·数据结构·算法
MZ_ZXD00116 小时前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·vue.js·spring boot·python·django·php