C++ STL - queue使用详解
2024-09-08 05:18:55
c++队列模板类的定义在<queue>头文件中,queue 模板类需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque 类型。
下面详细介绍queue的使用:
一:定义queue(要有头文件#include <queue>)
queue<int> q1;
queue<double> q2;
二:基本函数
back()返回一个引用,指向队列的最后一个元素。
empty()函数返回真(true)如果队列为空,否则返回假(false)。
front()返回队列第一个元素的引用。
pop()函数删除队列的一个元素
push() 在末尾加入一个元素
size() 返回队列中元素的个数
三:示例代码
#include <cstdlib>
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int e,n,m;
queue<int> q1;
for(int i=0;i<10;i++)
q1.push(i);
if(!q1.empty())
cout<<"dui lie bu kongn";
n=q1.size();
cout<<n<<endl;
m=q1.back();
cout<<m<<endl;
for(int j=0;j<n;j++)
{
e=q1.front();
cout<<e<<" ";
q1.pop();
}
cout<<endl;
if(q1.empty())
cout<<"dui lie bu kongn";
system("PAUSE");
return 0;
}
最新文章
- 传输层协议TCP和UDP
- 夯实基础之php学习-1基础篇
- 【OpenCV】边缘检测:Sobel、拉普拉斯算子
- 亚马逊如何变成 SOA(面向服务的架构)?
- jq 文字上下不间断滚动实例
- static_cast .xml
- c 语言结构体struct的三种定义方式 及 typedef
- ASP.NET性能优化小结
- [前言] 实现一个Android电子书阅读APP
- 【前端】主流API-promise解析,js基础。
- 用C#操作IIS创建虚拟目录和网站
- Python中的数据结构
- Archive required for library “xxx” cannot be read or is not a valid zip file报错解决
- Pandas 基础(2) - Dataframe 基础
- noip第7课资料
- threejs精灵平面Sprite(类似tip效果)
- iOS字符串安全
- Ubuntu 16.04.5安装docker
- net core集成CAP
- 让Outlook一直保持开启
热门文章
- LInux下实时网络流量监控工具nload教程
- POJ 1066
- html里显示中文乱码的解决的方法
- 支持中文的基于词为基本粒度的前缀树(prefix trie)python实现
- UVa 10290 - {Sum+=i++} to Reach N
- UVA - 11077 Find the Permutations (置换)
- 微软继MVC5后,出现ASP.NET VNEXT
- [JavaEE] 了解Java连接池
- SQL注入原理解析以及举例1
- MYSQL5.6和5.7编译标准化安装与配置