#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的实现的更多相关文章

  1. C++学习笔记50:队列类模板

    队列是只能向一端添加元素,从另一端删除元素的线性群体 循环队列 在想象中将数组弯曲成环形,元素出队时,后继元素不移动,每当队尾达到数组最后一个元素时,便再回到数组开头. 队列类模板 //Queue.h ...

  2. C++标准库类模板(stack)和 队列(queue)

    在C++标准库(STL)中有栈和队列的类模板,因此可以直接使用 1.栈(stack):使用栈之前,要先包含头文件 : #include<stack> stack.push(elem); / ...

  3. 连分数(分数类模板) uva6875

    //连分数(分数类模板) uva6875 // 题意:告诉你连分数的定义.求连分数,并逆向表示出来 // 思路:直接上分数类模板.要注意ai可以小于0 #include <iostream> ...

  4. C++ 模板的编译 以及 类模板内部的实例化

    在C++中.编译器在看到模板的定义的时候.并不马上产生代码,仅仅有在看到用到模板时,比方调用了模板函数 或者 定义了类模板的 对象的时候.编译器才产生特定类型的代码. 一般而言,在调用函数的时候,仅仅 ...

  5. C++解析(26):函数模板与类模板

    0.目录 1.函数模板 1.1 函数模板与泛型编程 1.2 多参数函数模板 1.3 函数重载遇上函数模板 2.类模板 2.1 类模板 2.2 多参数类模板与特化 2.3 特化的深度分析 3.小结 1. ...

  6. c++类模板初探

    #include <iostream> #include <string> using namespace std; // 你提交的代码将嵌入到这里 ; template &l ...

  7. C++STL - 类模板

    类的成员变量,成员函数,成员类型,以及基类中如果包含参数化的类型,那么该类就是一个类模板   1.定义 template<typename 类型形参1, typename 类型形参2,...&g ...

  8. C++ 类模板的使用

    从事C++挺久了,在前段时看书时,发现高手,都是在写模板无,泛型编程,顿感差距.自己连模板都没有写,于是就小小的研究了下模板的用法. 模板简而言之就是对某此对象的相同方法,或处理方式,进行归纳,总结, ...

  9. Xcode6中如何使用自定义的类模板

    说到IOS类的模板,有些人感觉很陌生,但是只要有开发过IOS程序的人,其实都用过类的模板,只不过是用的系统自带的类的模板. 例如创建一个ClassTemplateVC继承于UIViewControll ...

随机推荐

  1. Semantic-UI-React (称 stardust) 对比 Antd

    Semantic-UI-React: http://react.semantic-ui.com/ ANTD :http://ant.design/ Amaze UI React: http://ama ...

  2. poj 1521

    http://poj.org/problem?id=1521 题意:给你一个字符串,首先是计算出一个按正常编码的编码长度,其次是计算出一个用霍夫曼编码的编码长度,最后求正常编码的长度除以霍夫曼编码长度 ...

  3. 使用phpmyadmin修改XAMPP中MySQL的默认空密码

    XAMPP是开发php应用的一套完整的工具合集,就像安装软件一样安装,其他的都配置好了,不用自己再去繁琐的单独配置Apache.MySQL.php这几个模块了,以前我一直在使用的是Appserv,也是 ...

  4. Effective C++ -----条款15:在资源管理类中提供对原始资源的访问

    APIs往往要求访问原始资源(raw resources),所以每一个RAII class应该提供一个“取得其所管理之资源”的办法. 对原始资源的访问可能经由显示转换(.get()成员函数或者指针取值 ...

  5. Base64编码格式详解

    什么是Base64? 按照RFC2045的定义,Base64被定义为:Base64内容传送编码被设计用来把任意序列的8位字节描述为一种不易被人直接识别的形式.(The Base64 Content-T ...

  6. tomcat浏览器地址支持中文方法

  7. autolayout autoresizing

    WWDC 2012 Session笔记——202, 228, 232 AutoLayout(自动布局)入门 这是博主的WWDC2012笔记系列中的一篇,完整的笔记列表可以参看这里.如果您是首次来到本站 ...

  8. objective-c可变数组

     1 #pragma mark ---------------可变数组-----------------  2 //        可以在数组里面进行增删改的操作  3 //  4 //        ...

  9. qt编译mysql插件

    安装MySQL,C:\Program Files (x86)\MySQL\MySQL Server 5.7,然后把include和lib文件夹拷贝到C盘,因为qmake不允许路径中有空格!!! 安装Q ...

  10. [Android Pro] Dangerous permissions and permission groups.

    Permission Group Permissions CALENDAR READ_CALENDAR WRITE_CALENDAR CAMERA CAMERA CONTACTS READ_CONTA ...