STL-queue实现队列

来源:互联网 发布:马尔可夫链 转化矩阵 编辑:程序博客网 时间:2024/05/29 04:43

#include<queue>用来将STL的queue包含到程序中。

queue<string>  Q;是一个声明,用于生成管理int型元素的队列。STL提供的queue是一个模板,需要我们在<>中指定类型,从而定义管理该类型数据的容器。

例如:queue中定义了如下表的成员函数

函数名                  功能                                                        复杂度

size()                   返回队列的元素数                                    O(1)

front()                     返回队头的元素                                    O(1)

pop()                   从队列中取出并删除元素                         O(1)

push(x)               向队列中添加元素x                                   O(1)

empty()              在队列为空时返回true                               O(1)

queue的使用方法

#include <cstdio>#include <iostream>#include <cstring>#include <queue>using namespace std;int main(){queue<string> Q;Q.push("red");Q.push("yellow");Q.push("yellow");Q.push("blue");cout<<Q.front()<<" ";Q.pop();cout<<Q.front()<<" ";Q.pop();cout<<Q.front()<<" ";Q.pop();Q.push("green");cout<<Q.front()<<" ";Q.pop();cout<<Q.front()<<endl;return 0;}


原创粉丝点击