容器适配器之Queue
标准库定义了三个容器适配器,使得已有类型得到改造,这里,他们都有默认实现,当然也可以自己传入别的容器类型参数。
根据在Primer中的阅读,做了如下test。
1、pop():队首元素移除,并无返回值,与C++风格一致。
2、front():返回队首元素,不删除。
3、back():返回队尾。
4、size():大小。
5、emplace():构造函数直接调用插入队尾。突然发现这个很有用,当你需要类型转换的时候,可以使用这个函数。
#include <iostream>
#include <queue>
using namespace std;
int main()
{
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.push(5);
cout<<"head\t"<<q.front()<<endl;
cout<<"tail\t"<<q.back()<<endl;
cout<<"size\t"<<q.size()<<endl;
q.pop();//auto h=并没有返回值
cout<<"now head\t"<<q.front()<<endl;
q.emplace(6);//调用构造函数
cout<<"now tail\t"<<q.back()<<endl;
cout << "Hello world!\t\t\t@zem" << endl;
return 0;
}