C++/C++11中std::queue的使用

来源:互联网 发布:淘宝网夏季女装短裤 编辑:程序博客网 时间:2024/05/20 00:10

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-efficientlystatic 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.aspxint 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

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 草鱼身上有红斑像出血了怎么办 宝宝屁眼红的破皮了怎么办 孩子身上起红疙瘩很痒怎么办 久而不射,但软了怎么办 盆底综合肌力1级怎么办 头发掉的厉害怎么办吃什么好 给蜂蛰了肿了痒怎么办 小米手环2没电了怎么办 小米手环2不亮了怎么办 红米3s无限重启怎么办 乐视手机1s卡顿怎么办 老公出轨了怎么办你会选择离婚吗 c盘和d盘换换了怎么办 晚上2点到3点醒怎么办 红米3s变砖了怎么办 6s锁屏密码忘了怎么办 怀孕9个月了胃疼怎么办 怀孕6个月了胃疼怎么办 孕妇胃疼怎么办4个月了 25岁欠了5万块钱怎么办 感冒嗓子疼怎么办最简单的方法 和老婆离婚了我的心好痛怎么办 4s店不给退定金怎么办 教你闪腰了后该怎么办 coolpad酷派手机开不了机怎么办 苹果5s黑屏开不了机怎么办 苹果4s的屏坏了怎么办 苹果6手机充电口接触不良怎么办 5s用久了卡顿怎么办 孕妇血糖高怎么办什么方法降最好 脚砸了肿了紫了怎么办 我想在淘宝上卖东西该怎么办 苹果手机4s开不了机怎么办 冒险岛s前出2条怎么办 狗狗又吐又拉血怎么办 小孩上网成瘾怎么办父母要怎么做 一只眼睛大一只眼睛小怎么办 带近视镜时间长了眼睛变形怎么办 联通卡2g换4g卡怎么办 上火牙疼怎么办教你立刻止疼 吃热的凉的牙疼怎么办