类模板Queue的实现
#include <iostream>
#include <vector>
using namespace std; template <class Type> class Queue;
template <class T > ostream & operator << (ostream &os, const Queue<T> &); template <class Type> class QueueItem {
friend class Queue<Type >;
friend ostream & operator << <Type> (ostream &os, const Queue<Type> &);
//private class
QueueItem(const Type &val) : Item(val), next() {}
Type Item;
QueueItem<Type > *next;
}; template <class Type> class Queue {
friend ostream & operator << <Type> (ostream &os, const Queue<Type> &); public:
Queue() : head(), tail() {}
template <class It> Queue(It _beg, It _end) : head(), tail() { copy_elems(_beg, _end); }
Queue(const Queue<Type> &Q) : head(), tail() { copy_elems(Q); }
Queue& operator = (const Queue &Q);
~Queue() { Destroy(); }
template <class Iter> void _assign(Iter, Iter);
Type &_front() { return head->Item; }
const Type &_front() const { return head->Item; }
void _push(const Type&);
void _pop();
bool _empty() const { return head == ; } private:
QueueItem<Type> *head;
QueueItem<Type> *tail;
void Destroy();
void copy_elems(const Queue<Type> &);
template <class Iter> void copy_elems(Iter, Iter);
}; template <class Type> Queue<Type>& Queue<Type>::operator = (const Queue<Type> &q)
{
if (this != &q) {
head = tail = ;
for (QueueItem<Type> *pt = q.head; pt; pt= pt->next) {
_push(pt->Item);
}
}
return *this;
} template <class Type> void Queue<Type>::copy_elems(const Queue<Type> &q)
{
for (QueueItem<Type> *pt = q.head; pt; pt = pt->next) {
_push(pt->Item);
}
}
template <class Type> template <class Iter> void Queue<Type>::copy_elems(Iter _beg, Iter _end)
{
while (_beg != _end) {
_push(*_beg);
++_beg;
}
} template <class Type> template <class Iter> void Queue<Type>::_assign(Iter _beg, Iter _end)
{
head = tail = ;
copy_elems(_beg, _end);
} template <class Type> void Queue<Type>::_push(const Type& val)
{
QueueItem<Type> *p = new QueueItem<Type>(val);
if (_empty()) {
head = tail = p;
} else {
tail->next = p;
tail = p;
}
} template <class Type> void Queue<Type>::_pop()
{
QueueItem<Type> *p = head;
head = head->next;
delete p;
} template <class Type> ostream & operator << (ostream &os, const Queue<Type> &q)
{
for (QueueItem<Type> *pt = q.head; pt; pt = pt->next) {
os << pt->Item << " ";
}
os << endl;
return os;
} template <class Type> void Queue<Type>::Destroy()
{
while (!_empty()) _pop();
} int main()
{
Queue<int >que;
que._push();
que._push();
que._push();
que._push();
cout << que;
que._pop();
cout << que;
Queue<int >q2, q1(que);
cout << q1;
cout << q2._empty() << endl;
q2 = q1;
cout << q2;
while (!que._empty()) {
cout << que._front() << endl;
que._pop();
}
vector<int > vet(, );
for (int i = ; i < (int)vet.size(); i++) {
cout << vet[i] << ' ';
}
cout << endl;
que._assign(vet.begin(), vet.end());
cout << que;
return ;
}

类模板Queue的实现的更多相关文章
- C++学习笔记50:队列类模板
队列是只能向一端添加元素,从另一端删除元素的线性群体 循环队列 在想象中将数组弯曲成环形,元素出队时,后继元素不移动,每当队尾达到数组最后一个元素时,便再回到数组开头. 队列类模板 //Queue.h ...
- C++标准库类模板(stack)和 队列(queue)
在C++标准库(STL)中有栈和队列的类模板,因此可以直接使用 1.栈(stack):使用栈之前,要先包含头文件 : #include<stack> stack.push(elem); / ...
- 连分数(分数类模板) uva6875
//连分数(分数类模板) uva6875 // 题意:告诉你连分数的定义.求连分数,并逆向表示出来 // 思路:直接上分数类模板.要注意ai可以小于0 #include <iostream> ...
- C++ 模板的编译 以及 类模板内部的实例化
在C++中.编译器在看到模板的定义的时候.并不马上产生代码,仅仅有在看到用到模板时,比方调用了模板函数 或者 定义了类模板的 对象的时候.编译器才产生特定类型的代码. 一般而言,在调用函数的时候,仅仅 ...
- C++解析(26):函数模板与类模板
0.目录 1.函数模板 1.1 函数模板与泛型编程 1.2 多参数函数模板 1.3 函数重载遇上函数模板 2.类模板 2.1 类模板 2.2 多参数类模板与特化 2.3 特化的深度分析 3.小结 1. ...
- c++类模板初探
#include <iostream> #include <string> using namespace std; // 你提交的代码将嵌入到这里 ; template &l ...
- C++STL - 类模板
类的成员变量,成员函数,成员类型,以及基类中如果包含参数化的类型,那么该类就是一个类模板 1.定义 template<typename 类型形参1, typename 类型形参2,...&g ...
- C++ 类模板的使用
从事C++挺久了,在前段时看书时,发现高手,都是在写模板无,泛型编程,顿感差距.自己连模板都没有写,于是就小小的研究了下模板的用法. 模板简而言之就是对某此对象的相同方法,或处理方式,进行归纳,总结, ...
- Xcode6中如何使用自定义的类模板
说到IOS类的模板,有些人感觉很陌生,但是只要有开发过IOS程序的人,其实都用过类的模板,只不过是用的系统自带的类的模板. 例如创建一个ClassTemplateVC继承于UIViewControll ...
随机推荐
- React 源码解读参考,理解原理。
Rubix - ReactJS Powered Admin Template 文档: http://rubix-docs.sketchpixy.com/ ===================== ...
- [猜数字]把两个数和告诉A,积告诉B,求这两个数是什么
1-20的两个数把和告诉A,积告诉B,A说不知道是多少,B也说不知道,这时A说我知道了,B接着说我也知道了,问这两个数是多少? 分析: 设和为S,积为M. 首先,A:我不知道. 说明:S可以分解成多个 ...
- The Imitation Game
<The Imitation Game>是一部非常好的电影,讲述了人工智能之父——阿兰图灵的传奇一生. 重点讲述了他通过破译德国的通讯密码缩短了二战的持续时间,因而拯救了无数生命的伟大事迹 ...
- iOS 开启data protection 的方法
我这里说的data protection,指的是设备设置密码后,如果设备锁屏,并且当前解锁需要密码(有时可能因为自己的设定,导致会再几小时后才需要密码),这时应用程序处于加密状态,无法从外部读取.如果 ...
- TS初探
简介 TypeScript具有类型系统,且是JavaScript的超集.它可以编译成普通的JavaScript代码. TypeScript支持任意浏览器,任意环境,任意系统并且是开源的.Ts主要用于解 ...
- InnoDB O_DIRECT选项漫谈(一)【转】
本文来自:http://insidemysql.blog.163.com/blog/static/2028340422013671186977/ 最近和文件系统内核开发人员做技术交流,对O_DIR ...
- Java for LeetCode 208 Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
- nyoj138 找球号(二)_离散化
找球号(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 在某一国度里流行着一种游戏.游戏规则为:现有一堆球中,每个球上都有一个整数编号i(0<=i< ...
- Mysql 基础1
Mysql int 整型float 小数double 小数varchar(20) 字符串bit 布尔型数据datetime 日期时间类型text 长文本 money 存货币image 存二进制数据 数 ...
- 批处理命令——goto 和 :
谈起goto,相信大家应该想到的是面向过程编程.其实,这就相当于当有人向你谈起class,意味着你就懂得面向对象编程.如果你不懂,那么你们的沟通将会很困难.不懂我说的啥意思吗?请参见曾经分享王路的一篇 ...