STL之Queue
来源:互联网 发布:mac可以接鼠标吗 编辑:程序博客网 时间:2024/05/21 02:51
queue是队列容器,是一种“先进先出”的容器。
queue是简单地装饰deque容器而成为另外的一种容器。
#include <queue>
对象的默认构造
queue采用模板类实现,queue对象的默认构造形式:queue queT; 如:
queue<int> queInt; //一个存放int的queue容器。queue<float> queFloat; //一个存放float的queue容器。queue<string> queString; //一个存放string的queue容器。... //尖括号内还可以设置指针类型或自定义类型。
push()与pop()方法
queue.push(elem); //往队尾添加元素queue.pop(); //从队头移除第一个元素queue<int> queInt;queInt.push(1);queInt.push(3);queInt.push(5);queInt.push(7);queInt.push(9);queInt.pop();queInt.pop();此时queInt存放的元素是5,7,9
对象的拷贝构造与赋值
queue(const queue &que); //拷贝构造函数queue& operator=(const queue &que); //重载等号操作符queue<int> queIntA;queIntA.push(1);queIntA.push(3);queIntA.push(5);queIntA.push(7);queIntA.push(9);queue<int> queIntB(queIntA); //拷贝构造queue<int> queIntC;queIntC = queIntA; //赋值
数据存取
queue.back(); //返回最后一个元素queue.front(); //返回第一个元素queue<int> queIntA;queIntA.push(1);queIntA.push(3);queIntA.push(5);queIntA.push(7);queIntA.push(9);int iFront = queIntA.front(); //1int iBack = queIntA.back(); //9queIntA.front() = 11; //11
大小
queue.empty(); //判断队列是否为空queue.size(); //返回队列的大小queue<int> queIntA; queIntA.push(1); queIntA.push(3); queIntA.push(5); queIntA.push(7); queIntA.push(9); if (!queIntA.empty()){ int iSize = queIntA.size(); //5}
示例代码
#include <iostream>using namespace std;#include <queue>//队列中基本数据类型void main61(){ queue<int> q; q.push(1); q.push(2); q.push(3); cout << "队头元素:" << q.front() << endl; cout << "队列的大小" << q.size() <<endl; while ( !q.empty()) { int tmp = q.front(); cout << tmp << " "; q.pop(); }}//队列的算法 和 数据类型的分离//teacher结点class Teacher{public: int age; char name[32];public: void printT() { cout << "age:" << age << endl; }};void main62(){ Teacher t1, t2, t3; t1.age = 31; t2.age = 32; t3.age = 33; queue<Teacher> q; q.push(t1); q.push(t2); q.push(t3); while (!q.empty()) { Teacher tmp = q.front(); tmp.printT(); q.pop(); }}void main63(){ Teacher t1, t2, t3; t1.age = 31; t2.age = 32; t3.age = 33; queue<Teacher *> q; q.push(&t1); q.push(&t2); q.push(&t3); while (!q.empty()) { Teacher *tmp = q.front(); tmp->printT(); q.pop(); }}void main666(){ //main61(); //main62(); main63(); cout<<"hello..."<<endl; system("pause"); return ;}
阅读全文
0 0
- STL之queue
- STL之queue
- STL 之 stack - queue
- STL之queue
- STL 之 queue
- STL之stack、queue
- STL之queue实现
- STL之Stack,Queue
- STL之queue
- STL系列之queue
- STL容器之queue
- STL之queue
- STL适配器之queue
- STL模板之queue
- STL之queue容器
- 29STL之Queue
- STL之Queue
- STL学习之queue适配器
- HTML5-ES6
- 8个重要的电子邮件黑名单及如何从黑名单中删除?
- 生产者与消费者模型
- HDU 2457 || POJ 3691 DNA repair (AC自动机 + dp)
- 基于MapReduce框架的K-means算法实现
- STL之Queue
- 单调递增最长子序列
- python 命令行参数解析的标准模板
- 母舰
- CheerLeaders Uva 11806(容斥原理)
- 大道至简读后感
- 直接使用safari网页下载百度网盘里的内容跳过提示使用客户端下载
- js获取时间
- matlab中图像处理的基本函数(1)