queue队列容器
2015-08-19 19:54
429 查看
queue队列也是一种线性存储表,元素的插入在表的一端进行,在表的另一端删除,具有先进先出的特点,插入的一端称为队尾,删除的一端称为队首。C++
STL的队列泛化,默认使用双端队列容器deque作为底层架构。元素的出队不返回队首元素,需要调用取队首函数来获取队首元素。队列是一种常用的数据结构,通常以消息队列的形式应用于进程间通信。
(1) queue()
queue<int> q;
(2) queue(const queue&)
queue<int,list<int> > q1;
queue<int,list<int> > q2(q1);
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
queue<int> q;
while(!q.empty())
{
q.pop();
}
非空判断,调用empty函数。
size函数获取队列的大小。
STL的队列泛化,默认使用双端队列容器deque作为底层架构。元素的出队不返回队首元素,需要调用取队首函数来获取队首元素。队列是一种常用的数据结构,通常以消息队列的形式应用于进程间通信。
创建queue对象
有以下两种方式。(1) queue()
queue<int> q;
(2) queue(const queue&)
queue<int,list<int> > q1;
queue<int,list<int> > q2(q1);
元素入队
入队函数为push,C++ STL没有预先设定队列的大小,元素入队不会判断是否队满。queue<int> q;
q.push(1);
q.push(2);
q.push(3);
元素出队
出队函数pop,函数不会判断队列是否为空,需要自行判断。queue<int> q;
while(!q.empty())
{
q.pop();
}
取队首、队尾元素
队列容器的front函数和back函数,分别读取队首和队尾元素。queue<int> q; while(!q.empty()) { cout<<q.front()<<endl; q.pop(); }
非空判断,调用empty函数。
#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); while(!q.empty()) { cout<<q.front()<<endl;//1、2、3、4、5 q.pop(); } return 0; }
size函数获取队列的大小。
#include<iostream> #include<queue> #include<list> #define QUEUE_SIZE 2 using namespace std; int main() { queue<int,list<int> >q; if(q.size()<QUEUE_SIZE) { q.push(1); } if(q.size()<QUEUE_SIZE) { q.push(10); } if(q.size()<QUEUE_SIZE) { q.push(15); } while(!q.empty()) { cout<<q.front()<<endl;//1、10 q.pop(); } return 0; }
相关文章推荐
- UIScrollView
- UI16_数据持久化(NSUserDefaults,简单对象,复杂对象归档反归档,文件管理者"删除添加文件")
- Codeforces Round #241 (Div. 2) AGuess a number!
- IOS学习之路十二(UITableView下拉刷新页面)
- Handler详解(一)---关联到UI线程
- Unique Binary Search Trees II
- sql server中的TimeStamp时间戳与UniqueIdentifier数据类型
- 在navgationController中添加UISegmentedControl
- EF里Guid类型数据的自增长、时间戳和复杂类型的用法
- ios-UIKit动画
- EF里的默认映射以及如何使用Data Annotations和Fluent API配置数据库的映射
- Unique Paths
- A Beginner’s Guide to Eigenvectors, PCA, Covariance and Entropy
- poj3625 Building Roads(最小生成树)
- Android第三讲——五大布局(UI)
- 设计模式之建造者模式(Builder Pattern)
- Version和Build字段的关系
- Android 中MenuInflater的使用(布局定义菜单
- [LeetCode]Unique Paths II
- [LeetCode]Unique Paths