C++ primer 模板与泛型编程
继续浏览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 模板与泛型编程的更多相关文章
- C++ Primer(6) 模板和泛型编程(上)
问题聚焦: 泛型编程是独立于变量类型的方式编写代码: 模板是泛型编程的基础. 本篇主要介绍模板的基础知识,包括:模板的定义和模板的实例化. 1 模版定义 必要性: Demo int compare(c ...
- C++ Primer 学习笔记_76_模板与泛型编程 --模板定义[续]
模板与泛型编程 --模板定义[续] 四.模板类型形參 类型形參由keywordclass或 typename后接说明符构成.在模板形參表中,这两个keyword具有同样的含义,都指出后面所接的名字表示 ...
- C++ Primer 学习笔记_84_模板与泛型编程 --模板特化
模板与泛型编程 --模板特化 引言: 我们并不总是能够写出对全部可能被实例化的类型都最合适的模板.某些情况下,通用模板定义对于某个类型可能是全然错误的,通用模板定义或许不能编译或者做错误的事情;另外一 ...
- C++ Primer 学习笔记_77_模板与泛型编程 --实例化
模板与泛型编程 --实例化 引言: 模板是一个蓝图,它本身不是类或函数.编译器使用模板产生指定的类或函数的特定版本号.产生模板的特定类型实例的过程称为实例化. 模板在使用时将进行实例化,类模板在引用实 ...
- C++ Primer 学习笔记_85_模板与泛型编程 --模板特化[续]
模板与泛型编程 --模板特化[续] 三.特化成员而不特化类 除了特化整个模板之外,还能够仅仅特化push和pop成员.我们将特化push成员以复制字符数组,而且特化pop成员以释放该副本使用的内存: ...
- C++ Primer 学习笔记_75_模板与泛型编程 --模板定义
模板与泛型编程 --模板定义 引言: 所谓泛型程序就是以独立于不论什么特定类型的方式编写代码.使用泛型程序时,我们须要提供详细程序实例所操作的类型或值. 模板是泛型编程的基础.使用模板时能够无须了解模 ...
- C++ Primer 学习笔记_76_模板和泛型编程 --模板定义[继续]
模板和泛型编程 --模板定义[续] 四.模板类型形參 类型形參由keywordclass或 typename后接说明符构成.在模板形參表中,这两个keyword具有同样的含义,都指出后面所接的名字表示 ...
- C++ Primer 学习笔记_79_模板与泛型编程 --模板编译模型
模板与泛型编程 --模板编译模型 引言: 当编译器看到模板定义的时候,它不马上产生代码.仅仅有在用到模板时,假设调用了函数模板或定义了模板的对象的时候,编译器才产生特定类型的模板实例. 一般而言,当调 ...
- C++ 模板与泛型编程
<C++ Primer 4th>读书笔记 所谓泛型编程就是以独立于任何特定类型的方式编写代码.泛型编程与面向对象编程一样,都依赖于某种形式的多态性. 面向对象编程中的多态性在运行时应用于存 ...
随机推荐
- webservice 测试地址
腾讯QQ在线状态 WEB 服务Endpoint: http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx Disco: http:// ...
- 【01】报错:webpack 不是内部或不可执行命令
[02] webpack 不是内部或不可执行命令 一般来安装完之后是可以直接执行的你可以执行 webpack -v 或者是 webpack --help 这样的就是正确的,我的问题的解决办法是 将 ...
- 面试准备——redis
https://blog.csdn.net/yangzhong0808/article/details/81196472 http://www.imooc.com/article/36399 http ...
- 【转】SQL索引一步到位
原文:http://www.cnblogs.com/AK2012/archive/2013/01/04/2844283.html SQL索引一步到位(此文章为“数据库性能优化二:数据库表优化”附属文章 ...
- 九度oj题目1008:最短路径问题
题目描述: 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. 输入: ...
- iOS第三方-百度地图地图SDK(一)
前言 最近项目忙完了准备把百度地图的方法都熟悉一遍,基于百度地图2.10.0,写demo的同时也写下博客来记录下 模拟器设置 我直接就复制我以前写过的一篇的图了,懒得截图... 获取百度地图KEY 让 ...
- 【Luogu】P2331最大子矩阵(DP)
题目链接 这题的状态转移方程真是粗鄙. f[i][j][k]表示前i行用了j个矩阵状态为k的时候的最大值. k=0:两列都不选. k=1:取左弃右. k=2:选右弃左. k=3:左右都选,但分属于两个 ...
- UVa10491 Cows and Cars
#include<iostream> #include<cstdio> #include<algorithm> int main(){ double a,b,c; ...
- POJ2560 Freckles
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu Description In an epis ...
- 【Git】Git 本地的撤销修改和删除操作
一:撤销操作 比如我现在在readme.txt文件里面增加一行 内容为555555555555,我们先通过命令查看如下: 在我未提交之前,我发现添加5555555555555内容有误,所以我得马上恢复 ...