std::queue: 模板类queue定义在<queue>头文件中。队列(Queue)是一个容器适配器(Container adaptor)类型,被特别设计用来运行于FIFO(First-in first-out)场景,在该场景中,只能从容器一端添加(Insert)元素,而在另一端提取(Extract)元素。只能从容器”后面”压进(Push)元素,从容器”前面”提取(Pop)元素。用来实现队列的底层容器必须满足顺序容器的所有必要条件。

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

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 std::queue class is a container adapter that gives the programmer the functionality of a queue - specifically, a FIFO (first-in, first-out) data structure. The class template acts as a wrapper to the underlying container - only a specific set of functions is provided. The queue pushes the elements on the back of the underlying container and pops them from the front.  Queue does not allow iteration through its elements.

下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:

#include "queue.hpp"
#include <iostream>
#include <queue> // std::queue
#include <vector>
#include <string>
#include <list>//
// reference: http://www.cplusplus.com/reference/queue/queue/
int test_queue_1()
{
{ // std::queue::back: Access last element, Returns a reference to the last element in the queue.// queue::front: access the first element, queue::back: access the last elementstd::queue<int> myqueue;myqueue.push(12);myqueue.push(20);myqueue.push(75);   // this is now the backfprintf(stderr, "myqueue.back: %d, myqueue.front: %d\n", myqueue.back(), myqueue.front());myqueue.back() -= myqueue.front(); // 75 -= 12std::cout << "myqueue.back() is now " << myqueue.back() << '\n'; // myqueue.back() is now 63
}{ // std::queue::front: Access next element, Returns a reference to the next element in the queue.// The next element is the "oldest" element in the queue and// the same element that is popped out from the queue when queue::pop is called.std::queue<int> myqueue;myqueue.push(77);myqueue.push(20);myqueue.push(16);fprintf(stderr, "myqueue.back: %d, myqueue.front: %d\n", myqueue.back(), myqueue.front());myqueue.front() -= myqueue.back();    // 77-16=61std::cout << "myqueue.front() is now " << myqueue.front() << '\n';
}{ // std::queue::push: Inserts a new element at the end of the queue, after its current last element// std::queue::pop: Remove next element, Removes the next element in the queue, effectively reducing its size by one.// std::queue::empty: Test whether container is empty, Returns whether the queue is empty: i.e. whether its size is zero.std::queue<int> myqueue;int myint;std::cout << "Please enter some integers (enter 0 to end):\n";std::vector<int> vec{ 1, 2, 3, 4, 5 };for (const auto& value : vec) {myqueue.push(value);}fprintf(stderr, "myqueue.back: %d, myqueue.front: %d\n", myqueue.back(), myqueue.front());std::cout << "myqueue contains: ";while (!myqueue.empty()) {std::cout << ' ' << myqueue.front();myqueue.pop();}std::cout << '\n';
}{ // std::queue::emplace: C++11, Construct and insert element.// Adds a new element at the end of the queue, after its current last element.// The element is constructed in-place, i.e. no copy or move operations are performed.// queue::push: inserts element at the end , queue::emplace: constructs element in-place at the end// queue::pop: removes the first element std::queue<std::string> myqueue;myqueue.emplace("First sentence");myqueue.emplace("Second sentence");std::cout << "myqueue contains:\n";while (!myqueue.empty()) {std::cout << myqueue.front() << '\n';myqueue.pop();}
}{ // std::queue::size: Returns the number of elements in the queuestd::queue<int> myints;std::cout << "0. size: " << myints.size() << '\n';for (int i = 0; i < 5; i++) myints.push(i);std::cout << "1. size: " << myints.size() << '\n';myints.pop();std::cout << "2. size: " << myints.size() << '\n';
}{ // std::queue::swap: C++11, Exchanges the contents of the container adaptor// std::swap (queue): Exchanges the contents of x and y.std::queue<int> foo, bar;foo.push(10); foo.push(20); foo.push(30);bar.push(111); bar.push(222);foo.swap(bar);// std::swap(foo, bar);std::cout << "size of foo: " << foo.size() << '\n';std::cout << "size of bar: " << bar.size() << '\n';std::cout << "foo contains: ";while (!foo.empty()) {std::cout << ' ' << foo.front();foo.pop();}std::cout << '\n';std::cout << "bar contains: ";while (!bar.empty()) {std::cout << ' ' << bar.front();bar.pop();}std::cout << '\n';
}return 0;
}// reference: http://stackoverflow.com/questions/709146/how-do-i-clear-the-stdqueue-efficiently
static void clear(std::queue<int> &q)
{std::queue<int> empty;std::swap(q, empty);
}int test_queue_2()
{// clear queue, avoid to pop in a loop// A common idiom for clearing standard containers is swapping with an empty version of the containerstd::queue<int> foo;foo.push(10); foo.push(20); foo.push(30);fprintf(stderr, "foo size: %d\n", foo.size());clear(foo);fprintf(stderr, "foo size: %d\n", foo.size());return 0;
}//
// reference: https://msdn.microsoft.com/en-us/library/s23s3de6.aspx
int test_queue_3()
{
{ // queue::back: Returns a reference to the last and most recently added element at the back of the queue.// The last element of the queue. If the queue is empty, the return value is undefined.using namespace std;queue <int> q1;q1.push(10);q1.push(11);int& i = q1.back();const int& ii = q1.front();cout << "The integer at the back of queue q1 is " << i << "." << endl;cout << "The integer at the front of queue q1 is " << ii << "." << endl;
}{ // queue::empty: true if the queue is empty; false if the queue is nonempty.using namespace std;// Declares queues with default deque base containerqueue <int> q1, q2;q1.push(1);if (q1.empty()) cout << "The queue q1 is empty." << endl;else cout << "The queue q1 is not empty." << endl;if (q2.empty()) cout << "The queue q2 is empty." << endl;else cout << "The queue q2 is not empty." << endl;
}{ // queue::front: Returns a reference to the first element at the front of the queue// queue::size_type: An unsigned integer type that can represent the number of elements in a queue.using namespace std;queue <int> q1;q1.push(10);q1.push(20);q1.push(30);queue <int>::size_type i;i = q1.size();cout << "The queue length is " << i << "." << endl;int& ii = q1.back();int& iii = q1.front();cout << "The integer at the back of queue q1 is " << ii << "." << endl;cout << "The integer at the front of queue q1 is " << iii << "." << endl;
}{ // queue::pop: Removes an element from the front of the queueusing namespace std;queue <int> q1, s2;q1.push(10);q1.push(20);q1.push(30);queue <int>::size_type i;i = q1.size();cout << "The queue length is " << i << "." << endl;i = q1.front();cout << "The element at the front of the queue is " << i << "." << endl;q1.pop();i = q1.size();cout << "After a pop the queue length is " << i << "." << endl;i = q1.front();cout << "After a pop, the element at the front of the queue is " << i << "." << endl;
}{ // queue::push: Adds an element to the back of the queue.using namespace std;queue <int> q1;q1.push(10);q1.push(20);q1.push(30);queue <int>::size_type i;i = q1.size();cout << "The queue length is " << i << "." << endl;i = q1.front();cout << "The element at the front of the queue is " << i << "." << endl;
}{ // queue::queue: Constructs a queue that is empty or that is a copy of a base container object.using namespace std;// Declares queue with default deque base containerqueue <char> q1;// Explicitly declares a queue with deque base containerqueue <char, deque<char> > q2;// These lines don't cause an error, even though they// declares a queue with a vector base containerqueue <int, vector<int> > q3;q3.push(10);// but the following would cause an error because vector has// no pop_front member function// q3.pop( );// Declares a queue with list base containerqueue <int, list<int> > q4;// The second member function copies elements from a containerlist<int> li1;li1.push_back(1);li1.push_back(2);queue <int, list<int> > q5(li1);cout << "The element at the front of queue q5 is " << q5.front() << "." << endl;cout << "The element at the back of queue q5 is " << q5.back() << "." << endl;
}{ // queue::size: Returns the number of elements in the queueusing namespace std;queue <int> q1, q2;queue <int>::size_type i;q1.push(1);i = q1.size();cout << "The queue length is " << i << "." << endl;q1.push(2);i = q1.size();cout << "The queue length is now " << i << "." << endl;
}{ // queue::value_type: A type that represents the type of object stored as an element in a queue.using namespace std;// Declares queues with default deque base containerqueue<int>::value_type AnInt;AnInt = 69;cout << "The value_type is AnInt = " << AnInt << endl;queue<int> q1;q1.push(AnInt);cout << "The element at the front of the queue is " << q1.front() << "." << endl;
}return 0;
}

GitHub: https://github.com/fengbingchun/Messy_Test

C++/C++11中std::queue的使用相关推荐

  1. C++/C++11中std::string用法汇总

    C++/C++11中std::string是个模板类,它是一个标准库.使用string类型必须首先包含<string>头文件.作为标准库的一部分,string定义在命名空间std中. st ...

  2. C++11中std::forward_list单向链表的使用

    std::forward_list是在C++11中引入的单向链表或叫正向列表.forward_list具有插入.删除表项速度快.消耗内存空间少的特点,但只能向前遍历.与其它序列容器(array.vec ...

  3. C++11中std::async的使用

    C++11中的std::async是个模板函数.std::async异步调用函数,在某个时候以Args作为参数(可变长参数)调用Fn,无需等待Fn执行完成就可返回,返回结果是个std::future对 ...

  4. C++11中std::packaged_task的使用

    C++11中的std::packaged_task是个模板类.std::packaged_task包装任何可调用目标(函数.lambda表达式.bind表达式.函数对象)以便它可以被异步调用.它的返回 ...

  5. C++11中std::shared_future的使用

    C++11中的std::shared_future是个模板类.与std::future类似,std::shared_future提供了一种访问异步操作结果的机制:不同于std::future,std: ...

  6. C++11中std::future的使用

    C++11中的std::future是一个模板类.std::future提供了一种用于访问异步操作结果的机制.std::future所引用的共享状态不能与任何其它异步返回的对象共享(与std::sha ...

  7. 概率论中指数分布介绍及C++11中std::exponential_distribution的使用

    指数分布:在深度学习中,我们经常会需要一个在x=0点处取得边界点(sharp point)的分布.为了实现这一目的,我们可以使用指数分布(exponential distribution): p(x; ...

  8. 概率论中高斯分布(正态分布)介绍及C++11中std::normal_distribution的使用

    高斯分布:最常用的分布是正态分布(normal distribution),也称为高斯分布(Gaussian distribution): 正态分布N(x;μ,σ2)呈现经典的"钟形曲线&q ...

  9. 概率论中伯努利分布(bernoulli distribution)介绍及C++11中std::bernoulli_distribution的使用

    Bernoulli分布(Bernoulli distribution):是单个二值随机变量的分布.它由单个参数ø∈[0,1],ø给出了随机变量等于1的概率.它具有如下的一些性质: P(x=1)= ø ...

最新文章

  1. 企业数智化转型与分析流程自动化(APA)
  2. 下面哪个字段是http请求中必须具备的_HTTP协议及其工作原理介绍
  3. scanf函数和cin的区别、类的数组、C++排序函数
  4. 如何解决UltraCompare中中文显示乱码的问题
  5. mysql实用管理器添加外键_MySQL 添加外键
  6. 求护士的心理阴影面积 | 今日最佳
  7. 怎样解决jsp:include标签在包括html文件时遇到的乱码问题
  8. sunShine问题
  9. springboot微服务使用Feign远程调用失败
  10. smartbi 安装教程
  11. jsonpath学习资料
  12. 服务器合租速度的决定因素
  13. background,background-size背景图片和盒子模型
  14. mysql表的导入和导出
  15. 大数据项目实战——电商推荐系统设计
  16. Widget原理分析
  17. 编译和解释的区别是什么?
  18. jQuery获取div中的内容
  19. matlab转差频率控制,异步电动机转差频率间接矢量控制matlab仿真(毕业设计).doc
  20. Java编程思想读书笔记——字符串

热门文章

  1. 基于近距离的测距感知传感器调研以及扩展介绍
  2. 【camera-radar】基于ROS的多传感器融合感知系统实现(雷达+相机)(2)
  3. Linux那些事儿 之 戏说USB(34)接口的驱动
  4. HDU - 3911 Black And White 区间翻转+区间连续最长
  5. 【机器学习入门】(3) 朴素贝叶斯算法:多项式、高斯、伯努利,实例应用(心脏病预测)附python完整代码及数据集
  6. mysql 5.6.24 win32_mysql-5.6.24-win32解决没有my.ini并且修改编码
  7. python3 多线程_图解|为什么 Python 多线程无法利用多核
  8. 设置WebStrom切换最近打开过的项目快捷键Alt+E
  9. Boom Library 93套影视游戏无损配乐音效素材合集包
  10. g-git 相关命令 及其 基本原理探索 (一)