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>读书笔记 所谓泛型编程就是以独立于任何特定类型的方式编写代码.泛型编程与面向对象编程一样,都依赖于某种形式的多态性. 面向对象编程中的多态性在运行时应用于存 ...
随机推荐
- Knockout v3.4.0 中文版教程-12-控制文本内容和外观-html绑定
3. html绑定 目的 html绑定会使关联的DOM元素显示你参数指定的html内容. 当你的视图模型里面的值是HTML标记字符串,而你想要呈现它,这时候用html绑定特别合适. 例子 <di ...
- Centos7 安装python3详细教程,解决升级后不兼容问题
一.确认当前python版本 [root@centos Python-3.6.1]# python Python 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC ...
- PL/SQL配置访问多个不同IP的oracle
第一步:打开Oracle的tnsnames.ora文件.添加 test = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = ...
- SPOJ-COLONY - Linearian Colony!简单二分思想
COLONY - Linearian Colony 一道很水的题却坑我两天!在CF上做过类似的题,用递归可以找到答案,但感觉不会这么麻烦,于是看看有没有什么规律,对Y分奇偶貌似可以找到规律,但WA了三 ...
- 11g自动分区超过最大限制
公司业务系统一张表按时间每天分区 写入数据时报错:ORA-14300: 分区关键字映射到超出允许的最大分区数的分区 ORA-14300: partitioning key maps to a part ...
- 【Luogu】P2422良好的感觉(单调栈)
题目链接 写代码能力需要极大提升.我在五分钟之内想到了单调栈,然后花了一个小时的时间去看我单调队列为啥写错了…… 首先这题需要转换自己的思维.枚举所有“最小点”,然后看它往左往右最大能扩展多少. 维护 ...
- Codeforces Round #361 (Div. 2)——B. Mike and Shortcuts(BFS+小坑)
B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
- P1136 迎接仪式 (动态规划)
题目描述 LHX教主要来X市指导OI学习工作了.为了迎接教主,在一条道路旁,一群Orz教主er穿着文化衫站在道路两旁迎接教主,每件文化衫上都印着大字.一旁的Orzer依次摆出“欢迎欢迎欢迎欢迎……”的 ...
- spring aop在mvc的controller中加入切面无效
spring aop在mvc的controller中加入切面无效 因为MVC的controller,aop默认使用jdk代理.要使用cglib代理. 在spring-mybatis.xml配置文件中加 ...
- 史上最详细的linux关于connect: network is unreachable 问题的解决方案
1.虚拟机常用连接网络方式有两种:桥接和NAT. 使用桥接模式:则保证虚拟机的网段与物理机的网段保持一致.如下: 虚拟机网卡配置: 物理机使用WiFi接入网络(我用的是WiFi,你们可能用的是有线道理 ...