C++ 标准模板库STL 队列 queue 使用方法与应用介绍

queue

queue模板类的定义在<queue>头文件中。

与stack模板类很相似,queue模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque类型。

定义queue对象的示例代码如下:

queue<int> q1;

queue<double> q2;

queue的基本操作有:

入队,如例:q.push(x); 将x接到队列的末端。

出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。

访问队首元素,如例:q.front(),即最早被压入队列的元素。

访问队尾元素,如例:q.back(),即最后被压入队列的元素。

判断队列空,如例:q.empty(),当队列空时,返回true。

访问队列中的元素个数,如例:q.size()

std::queue

FIFO queue

queues are a type of container adaptor, specifically designed to operate in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other.

queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed into the "back" of the specific container and popped from its "front".

The underlying container may be one of the standard container class template or some other specifically designed container class. The only requirement is that it supports the following operations:

  • front()
  • back()
  • push_back()
  • pop_front()

Therefore, the standard container class templates deque and list can be used. By default, if no container class is specified for a particular queue class, the standard container class template deque is used.

In their implementation in the C++ Standard Template Library, queues take two template parameters:

 
template < class T, class Container = deque<T> > class queue;

Where the template parameters have the following meanings:

  • T: Type of the elements.
  • Container: Type of the underlying container object used to store and access the elements.

In the reference for the queue member functions, these same names are assumed for the template parameters.

最新文章

  1. C语言 指针小结
  2. springMVC--@requestBody
  3. u-boot链接脚本分析
  4. HTML中&lt;meta&gt;标签如何正确使用
  5. Friendship
  6. sample a texture as a rendertarget
  7. extjs分组查询
  8. IOT数据库选型——NOSQL,MemSQL,cassandra,Riak或者OpenTSDB,InfluxDB
  9. UVA 10790 How Many Points of Intersection?
  10. AM335x(TQ335x)学习笔记——u-boot-2014.10移植
  11. C++中的引用和移动语义
  12. js 次方 开方
  13. jenkins持续集成简介[一]
  14. 十三.iptabled配置
  15. 使用MSBuild Tools调用csproj项目文件发布网站时$(SolutionDir)宏参数值丢失为空的解决方案
  16. Manjaro 安装svn客户端,以及checkout使用命令
  17. Gym - 101375H MaratonIME gets candies 交互题
  18. c#万能视频播放器
  19. Confluence 6 为 Active Directory 配置一个 SSL 连接
  20. 牛客网暑期ACM多校训练营(第三场)DEncrypted String Matching fft

热门文章

  1. pacman 包管理器相关设定
  2. Python:函数基础
  3. python:布尔类型
  4. 【原】Django问题总结
  5. c++ 查重+排序函数
  6. [ DLPytorch ] 注意力机制&amp;机器翻译
  7. 教育片- Economics by Crash Course(共35集)(未完结)
  8. js学习:基本数据类型
  9. nginx 与上游服务器建立连接的相关设置
  10. 02-10Android学习进度报告十