类模板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 ...
随机推荐
- 利用ssh-copy-id无需密码登录远程服务器
本地机器生成公钥和私钥 ssh-keygen -t rsa 一路回车,最后会在~/.ssh目录下生成id_rsa和id_rsa.pub这两个文件. 与远程服务器建立信任机制 ssh-copy-id - ...
- Unity3d 枚举某个目录下所有资源
using UnityEngine; using System.Collections; using UnityEditor; using System.Collections.Generic; us ...
- Django~Test View
https://docs.djangoproject.com/en/1.9/topics/testing/ http://docs.seleniumhq.org/ Automated testing ...
- EMIS系统运行时提示【无法验证发行者,您确实要运行此软件吗? 】
无法验证发行者,您确实要运行此软件吗? 遇到这个提示你怎么办? 运行 gpedit.msc 进入组策略用户配置 ==>管理模板==> winows组件 ==> 附件管理器在 &quo ...
- 如何让ListView的item不可点击
原文链接:http://blog.csdn.net/zhangfei_jiayou/article/details/6972752 1. 如果是listView的id是使用系统默认的id,如下, 则可 ...
- 基于Spring的可扩展Schema进行开发自定义配置标签支持
一.背景 最近和朋友一起想开发一个类似alibaba dubbo的功能的工具,其中就用到了基于Spring的可扩展Schema进行开发自定义配置标签支持,通过上网查资料自己写了一个demo.今天在这里 ...
- September 9th 2016 Week 37th Friday
Within you, I lose myself. 有了你,我迷失了自我. I never had such feeling, maybe just because I never invested ...
- vs c++系统函数 计时器和暂停
在vs console下, 1 添加计时器 #include <Windows.h> double start = GetTickCount(); double end = GetTick ...
- !struct operator reload
struct t3DObject //对象信息结构体{ int numOfVerts; // 模型中顶点的数目 int numOfFaces; // 模型中面的数目 int numTexVertex; ...
- ld returned 1 exit status"的解决办法
在Linux下创建线程时,编译时会出现下面的错误,[root@linuxserver 807]# gcc -o 22 22.c/tmp/cc21HcoW.o(.text+0x4c): In funct ...