#include<iostream>
#include <queue>
#include <list>
using namespace std;
#if 0
* 1.队列是一种容器适配器, 用于在FIFO上下文中进行操作, 其中元素从一段插入, 然后在另一端进行提取;
* 2.队列实现为容器适配器, 使用特定容器类的封装对象作为其底层容器的类, 提供一些特定成员来访问其中的元素, 元素被推入特定容器的后面, 并且从容器的前面弹出;
* 3.构造函数通常包括一下几种:
* 1.explict queue(const container_type& ctnr)
* 2.explict queue(container_type&& ctnr = container_type());
* 3.template <class Alloc> explict queue(const Alloc& alloc);
* 4.template <class Alloc> explict queue(const container_type& cntr, const Alloc& alloc);
* 5.template <class Alloc> explict queue(container_type&& cntr, const Alloc &alloc);
* 6.template <class Alloc> explict queue(const queue& X, const Alloc& alloc);
* 7.template <class Alloc> explict queue(queue&& X, const Alloc& alloc);
#endif
int main()
{
//construct object;
deque<int> mydeque(3, 33);
list<int> mylist(2, 11);
queue<int> first;
first.push(3);
queue<int> second(mydeque);
queue<int, list<int>> third;
queue<int, list<int>> fourth(mylist);
cout << "first.size: " << first.size() << endl;
cout << "second.size: " << second .size() << endl;
cout << "third size: " << third.size() << endl;
cout << "fourth.size: " << fourth.size() << endl;
//.back():用于返回当前队列的最后一个元素;
// reference& back();
// const_reference& back() const;
cout << "first.back: " << first.back() << endl;
#if 1
//.front()用于返回第一个元素
//reference& front();
//const_reference& front() const;
first.front() += first.back();
cout << "first.front: " << first.front() << endl;
//.emplace(): template <class... args> void emplace(Args&&.. args)
//用于在队列尾部添加一个新的元素;
queue<string> myqueuestring;
myqueuestring.emplace("this is a freshman");
myqueuestring.emplace("this is a oldman");
cout << "myqueuestring constains: ";
while (!myqueuestring.empty())
{
cout << myqueuestring.front() << " ";
myqueuestring.pop();
}
cout << endl;
//.push用于在一段放入元素;
//void push(const value_type& val);
//void push(value_type&& val);
myqueuestring.push("that is a no-name man");
//.size()用于返回队列里面元素的多少;
//.swap():用于交换两个队列的值;
queue<int> newfirst;
newfirst.push(1);
newfirst.push(2);
newfirst.push(3);
newfirst.push(4);
newfirst.push(5);
queue<int> newsecond;
newsecond.push(5);
newsecond.push(4);
newsecond.push(3);
newsecond.push(2);
newsecond.push(1);
cout << "newfirst contains: ";
while (!newfirst.empty())
{
cout << newfirst.front() << " ";
newfirst.pop();
}
cout << endl;
cout << "newsecond contains: ";
while (!newsecond.empty())
{
cout << newsecond.front() << " ";
newsecond.pop();
}
cout << endl;
#endif
}
queue-的常见操作
最新推荐文章于 2022-11-02 20:56:23 发布