队(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 ...
随机推荐
- 嵌入式C语言--面试题
C语言测试是招聘嵌入式系统程序员过程中必须而且有效的方法.这些年,我既参加也组织了许多这种测试,在这过程中我意识到这些测试能为带面试者和被面试者提供许多有用信息,此外,撇开面试的压力不谈,这种测试也是 ...
- 棋盘状态压缩dp
状态压缩入门DP整理 只针对入门 一般都是用2进制的方法,压缩成一个数,所以n的范围都会特变小 一些套路 状态一般是很多的,可以搜索或者位运算筛选一下,基本都是这样的吧 当要存两个状态或者数组存不下的 ...
- Unity3D学习笔记(二十一):InputFiled、Dropdown、Scroll Rect、Mask
InputFiled组件(输入框) Text Component(显示内容):显示输入内容的Text的组件 Text(输入内容):输入的文本内容 Character Limit:字符数量限值,0是无限 ...
- Tomcat Connector
转自: http://blog.csdn.net/aesop_wubo/article/details/7617416 如下图所示,Tomcat服务器主要有两大核心模块组成:连接器和容器,本节只分析连 ...
- MVC ---- 如何扩展方法
先定义一个扩展类: public static class StringExtend { //扩展一个string的方法 public static bool IsNullOrEmpty(this s ...
- plsql过期解决方法
1.首先,登陆PL/SQL Developer,PL/SQL Developer要到期了 2.输入指令“regedit”打开注册表,如图所示 3.然后,在注册表里按HKEY_CURRENT_USER\ ...
- hdu 1269 迷宫城堡 最简单的联通图题 kosaraju缩点算法
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Des ...
- Windows10中注册 regsvr32 xxx.ocx报错but the call to DIIRegisterServer failed with error code 0x80040200
网站中有读取居民身份证的机器,需要安装一些注册activeX控件然后进入指定目录下执行以下命令regsvr32 xxx.ocx报了个错: but the call to DIIRegisterServ ...
- Qt_OpenGL_教程
1. 中文版: Qt OpenGL教程 http://blog.csdn.net/myths_0/article/details/24431597 http://qiliang.net/old/neh ...
- Java 常用对象-System类
2017-11-02 21:41:06 System类:System 类包含一些有用的类字段和方法.它不能被实例化. *常用方法 public static void gc() 运行垃圾回收器. 调用 ...