继续浏览c++ primer 看到模板与泛型编程这章。就顺便把这几节的代码综合了下,对一个Queue队列模板的实现

贴一下代码(看完书。自己敲,忘记了哪再看下书)

#include <ostream>
using std::ostream; //声明Queue的模板类
template <class Type> class Queue;
//声明模板函数
template <class T> ostream& operator<<(ostream& , const Queue<T>&); //定义QueueItem的模板类
template <class Type> class QueueItem
{
//定义友元模板类和友元模板函数
friend class Queue<Type>;
friend ostream& operator<< <Type>(ostream& , const Queue<Type>&);
//QueueItem构造函数
QueueItem(const Type &t):item(t),next(0){}
QueueItem *next;
Type item;
}; //定义Queue模板类
template <class Type> class Queue
{
//定义友元模板函数
friend ostream& operator<< <Type>(ostream& , const Queue<Type>&);
public:
//构造函数
Queue():head(0),tail(0){}
template <class It> Queue(It beg, It end):head(0),tail(0){copy_elems(beg,end);}
template <class Iter> void assign(Iter , Iter);
//复制构造函数
Queue(const Queue &object){head(0);tail(0);copy_elems(object);}
//赋值操作符
Queue& operator=(const Queue&);
//析构函数
~Queue(){destroy();}
//push操作
void push(const Type&);
//pop操作
void pop();
//取队列头元素的操作front
Type& front();
//推断是否为空的操作
bool empty(){return head==0;}
private:
QueueItem *head;
QueueItem *tail;
void destroy();
void copy_elems(const Queue&);
template <class Iter> void copy_elems(Iter , Iter);
};
//重载输出操作符
template <class T> ostream& operator<<(ostream &os , const Queue<T> &object)
{
os << "<";
QueueItem *p;
for(p=object.head;p!=object.tail;p=p->next)
{
os <<p->item << " ";
}
os << ">" << endl;
}
//定义Queue模板类中的模板成员函数
template<class Type> template <class Iter> void Queue<Type>::assign(Iter beg, Iter end)
{
destroy();
copy_elems(beg , end);
}
//定义Queue模板类中的copy_elems模板成员函数
template <class Type> template <class Iter> void Queue<Type>::copy_elems(Iter beg, Iter end)
{
while(beg != end)
{
push(*beg);
++beg;
}
}
//Queue模板类中的copy_elems成员函数
template <class Type> void Queue<Type>::copy_elems(const Queue &object)
{
QueueItem<Type> *p;
for(p=object.head;p&&p!=object.tail;p=p->next)
{
push(p->item);
}
}
//赋值操作符
template <class Type> Queue<Type>& Queue<Type>::operator=(const Queue &rhs)
{
if(&rhs != this)
{
destroy();
copy_elems(rhs);
}
return *this;
}
/*
//第二种用链表直接实现赋值操作符
template <class Type> Queue<Type>& Queue<Type>::operator=(const Queue &rhs)
{
QueueItem<Type> *p = rhs.head;
while(p)
{
QueueItem<Type> *q = new QueueItem<Type>(p->item);
if(p == rhs.head)
{
head = tail = q;
}else{
tail->next = q;
tail = q;
p=p->next;
}
}
return *this;
}
*/
//push操作
template <class Type> void Queue<Type>::push(const Type &value)
{
QueueItem<Type> *p = new QueueItem<Type>(value);
if(this->empty())
{
head = p;
tail = p;
}else{
tail->next = p;
tail = p;
}
}
//pop操作
template <class Type> void Queue<Type>::pop()
{
QueueItem<Type> *p;
p=head;
head = head->next;
delete p;
}
//front操作
template <class Type> Type& Queue<Type>::front()
{
return head->item;
}
//destory操作
template <class Type> void Queue<Type>::destroy()
{
while(!empty())
{
pop();
}
}

C++ primer 模板与泛型编程的更多相关文章

  1. C++ Primer(6) 模板和泛型编程(上)

    问题聚焦: 泛型编程是独立于变量类型的方式编写代码: 模板是泛型编程的基础. 本篇主要介绍模板的基础知识,包括:模板的定义和模板的实例化. 1 模版定义 必要性: Demo int compare(c ...

  2. C++ Primer 学习笔记_76_模板与泛型编程 --模板定义[续]

    模板与泛型编程 --模板定义[续] 四.模板类型形參 类型形參由keywordclass或 typename后接说明符构成.在模板形參表中,这两个keyword具有同样的含义,都指出后面所接的名字表示 ...

  3. C++ Primer 学习笔记_84_模板与泛型编程 --模板特化

    模板与泛型编程 --模板特化 引言: 我们并不总是能够写出对全部可能被实例化的类型都最合适的模板.某些情况下,通用模板定义对于某个类型可能是全然错误的,通用模板定义或许不能编译或者做错误的事情;另外一 ...

  4. C++ Primer 学习笔记_77_模板与泛型编程 --实例化

    模板与泛型编程 --实例化 引言: 模板是一个蓝图,它本身不是类或函数.编译器使用模板产生指定的类或函数的特定版本号.产生模板的特定类型实例的过程称为实例化. 模板在使用时将进行实例化,类模板在引用实 ...

  5. C++ Primer 学习笔记_85_模板与泛型编程 --模板特化[续]

    模板与泛型编程 --模板特化[续] 三.特化成员而不特化类 除了特化整个模板之外,还能够仅仅特化push和pop成员.我们将特化push成员以复制字符数组,而且特化pop成员以释放该副本使用的内存: ...

  6. C++ Primer 学习笔记_75_模板与泛型编程 --模板定义

    模板与泛型编程 --模板定义 引言: 所谓泛型程序就是以独立于不论什么特定类型的方式编写代码.使用泛型程序时,我们须要提供详细程序实例所操作的类型或值. 模板是泛型编程的基础.使用模板时能够无须了解模 ...

  7. C++ Primer 学习笔记_76_模板和泛型编程 --模板定义[继续]

    模板和泛型编程 --模板定义[续] 四.模板类型形參 类型形參由keywordclass或 typename后接说明符构成.在模板形參表中,这两个keyword具有同样的含义,都指出后面所接的名字表示 ...

  8. C++ Primer 学习笔记_79_模板与泛型编程 --模板编译模型

    模板与泛型编程 --模板编译模型 引言: 当编译器看到模板定义的时候,它不马上产生代码.仅仅有在用到模板时,假设调用了函数模板或定义了模板的对象的时候,编译器才产生特定类型的模板实例. 一般而言,当调 ...

  9. C++ 模板与泛型编程

    <C++ Primer 4th>读书笔记 所谓泛型编程就是以独立于任何特定类型的方式编写代码.泛型编程与面向对象编程一样,都依赖于某种形式的多态性. 面向对象编程中的多态性在运行时应用于存 ...

随机推荐

  1. 洛谷P3961 图的遍历

    题目来源 做这道题的方法不少. 在这里我只提一种 就是大法师. 可以采用反向建边,从最大的点开始dfs 我们考虑每次从所剩点中最大的一个点出发,我们暂且称它为i,而凡是i这个点所能到达的点,可以到达的 ...

  2. 高性能MySQL(第三版)

    一.MySQL架构与历史 1.2.2 锁粒度 表锁:写锁的优先级高于读锁:写锁的请求可以插入到读锁的前面,但读锁的请求却不能插入到写锁的前面: 行级锁:行级锁只在存储引擎层实现,在服务器层没有实现: ...

  3. java 协程框架kilim

    http://phl.iteye.com/blog/2247112 http://chen-tao.github.io/2015/10/02/kilim-work-way/ 待丰富

  4. linux 系统备份还原

    操作系统或文件备份 tar cvpzf backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude ...

  5. 【03】github的markdown语法

    [03]github的markdown语法 https://guides.github.com/features/mastering-markdown/(下图)(魔芋:已录入)   http://ma ...

  6. Linux cp复制

    复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir2.怎样才能将dir1下所有文件复制到dir2下 ...

  7. Leetcode 402.移掉k位数字

    移调k位数字 给定一个以字符串表示的非负整数 num,移除这个数中的 k 位数字,使得剩下的数字最小. 注意: num 的长度小于 10002 且 ≥ k. num 不会包含任何前导零. 示例 1 : ...

  8. Git x SVN rebase事故

    Git x SVN rebase事故 @author ixenos 2019-01-09 14:21:21 前言: 昨天在Git x SVN 中进行git svn dcommit的时候,提示需要再进行 ...

  9. TOJ 4475: The Coolest Sub-matrix

    4475: The Coolest Sub-matrix  Time Limit(Common/Java):4000MS/12000MS     Memory Limit:65536KByteTota ...

  10. Replication and Triggers

    参考官网:https://dev.mysql.com/doc/refman/5.7/en/replication-features-triggers.html 需要了解复制和触发器关系的背景: 程序变 ...