队(queue),C++模板实现
body, table{font-family: 微软雅黑; font-size: 13.5pt}
table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;}
th{border: 1px solid gray; padding: 4px; background-color: #DDD;}
td{border: 1px solid gray; padding: 4px;}
tr:nth-child(2n){background-color: #f8f8f8;}
|
队:(queue.h)
#include<iostream>
#include<string>
using namespace std;
//队列判空和判满
//头尾指针相同为空
//尾指针指向下一个可存放数据的单元,如果尾指针偏移一个单元和头指针相同,队列为满
template<class T,int num>
class queue
{
public:
queue();
~queue();
bool empty();
bool full();
bool push(T elem);
bool pop(T& tmp);
int size();
private:
int _front;
int _real;
T _arr[num];
};
template<class T,int num>
queue<T,num>::queue():_front(0),_real(0){}
template<class T,int num>
queue<T,num>::~queue(){}
template<class T,int num>
bool queue<T,num>::empty()
{
return _front == _real;
}
template<class T,int num>
bool queue<T,num>::full()
{
return _front == (_real+1)%num;
}
template<class T,int num>
bool queue<T,num>::push(T elem)
{
if(!full())
{
_arr[_real] = elem;
_real = (_real+1)%num;
return true;
}
else
return false;
}
template<class T,int num>
bool queue<T,num>::pop(T &tmp)
{
if(!empty())
{
tmp = _arr[_front];
_front = (_front+1)%num;
return true;
}
else
return false;
}
template<class T,int num>
int queue<T,num>::size()
{
return (_real-_front+num)%num;
}
|
测试文件(queueTest.cpp)
#include"queue.h"
int main()
{
queue<int,10> q1;
q1.push(3);
q1.push(5);
int tmp;
cout<<q1.size()<<endl;
q1.pop(tmp);
cout<<tmp<<endl;
cout<<"----------------------"<<endl;
queue<string,5> q2;
q2.push("hello");
q2.push("world");
cout<<q2.size()<<endl;
string tmpString;
q2.pop(tmpString);
cout<<q2.size()<<" "<<tmpString<<endl;
return 0;
}
|
队(queue),C++模板实现的更多相关文章
- POJ 3481 Double Queue (treap模板)
Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest ...
- STL学习系列五:Queue容器
Queue简介 queue是队列容器,是一种“先进先出”的容器. queue是简单地装饰deque容器而成为另外的一种容器. #include <queue> 1.queue对象的默认构造 ...
- C++STL学习笔记_(4)queue
10.2.5Queue容器 Queue简介 ² queue是队列容器,是一种"先进先出"的容器. ² queue是简单地装饰deque容器而成为另外的一种容器. ² #inc ...
- STL - queue(队列)
Queue简介 queue是队列容器,是一种"先进先出"的容器. queue是简单地装饰deque容器而成为另外的一种容器. #include <queue> queu ...
- 04--STL序列容器(Stack和Queue)
总括: stack和queue不支持迭代 一:栈Stack (一)栈的简介 stack是堆栈容器,是一种“先进后出”的容器. stack是简单地装饰deque容器而成为另外的一种容器. (二)栈的默认 ...
- STL之Queue容器
1.Queue容器 1)queue是队列容器,是一种“先进先出”的容器. 2)queue是简单地装饰deque容器而成为另外的一种容器. 3)头文件.#include <queue> 2. ...
- STL Queue 容器
STL Queue 容器 Queue简介 queue是队列容器,是一种“先进先出”的容器. queue是简单地装饰deque容器而成为另外的一种容器. # ...
- C++ STL 之 queue
queue 是一种先进先出(first in first out, FIFO)的数据类型,他有两个口,数据元素只能从一个口进,从另一个口出.队列只允许从队尾加入元素,队头删除元素,必须符合先进先出的原 ...
- C++queue的使用
C++队列是一种容器适配器,提供了一种先进先出的数据结构. 队列(queue)模板类定义在<queue>头文件中 基本操作: 定义一个queue变量:queue<Type> q ...
随机推荐
- Mato的文件管理 (莫队)题解
思路: 莫队模板题,转换几次就是找逆序数,用树状数组来储存数就行了 注意要离散化 代码: #include<queue> #include<cstring> #include& ...
- 【ssh免登录】设置集群环境ssh免登录步骤
1.每台机器都需要执行,生成自己的密钥 # ssh-keygen -t rsa 过程中遇到选项,全部enter #cd ~/.ssh # cat id_rsa.pub > authorized_ ...
- 【第七章】 springboot + retrofit
retrofit:一套RESTful架构的Android(Java)客户端实现. 好处: 基于注解 提供JSON to POJO,POJO to JSON,网络请求(POST,GET,PUT,DELE ...
- KMP 初级板子 待更新
复杂度 O(n+m) 这个博主写的蛮不错的 http://www.cnblogs.com/SYCstudio/p/7194315.html 1.本文中,所有的字符串从0开始编号2.为了在程序中表示方便 ...
- linux 下 安装nginx
http://www.cnblogs.com/lovexinyi8/p/5845017.html 测试可用. 参看 https://www.cnblogs.com/liujuncm5/p/671378 ...
- 3G下的无压缩视频传输(基于嵌入式linux) (转载)
本课题研究嵌入式系统在数据采集,3G无线通信方面的应用,开发集视频采集.地理信息采集.无线传输.客户机/服务器模式于一体的车载终端,实现终端采集视频与GPS信息的传输,支持服务器端显示视频与GPS信息 ...
- UVa 10881 蚂蚁
https://vjudge.net/problem/UVA-10881 题意: 一根长度为L厘米的木棍上有n只蚂蚁,每只蚂蚁要么朝左爬,要么朝右爬,速度为1厘米/秒.当两只蚂蚁相撞时,二者同时掉头. ...
- Cocos2d-x学习笔记(八)精灵对象的创建
精灵类即是Sprite,它实际上就是一张二维图. 它首先直接继承了Node类,因此,它具有节点的特征,同时,它也直接继承了TextureProtocol类,因此,它也具有纹理的基本特征. 这里,有必要 ...
- 查找SQL Server 自增ID值不连续记录
在很多的时候,我们会在数据库的表中设置一个字段:ID,这个ID是一个IDENTITY,也就是说这是一个自增ID.当并发量很大并且这个字段不是主键的时候,就有可能会让这个值重复:或者在某些情况(例如插入 ...
- C/C++UNION中包含STRUCT
测试环境:Win7x64,cn_visual_studio_2010_ultimate_x86_dvd_532347.iso,qt-opensource-windows-x86-msvc2010_op ...