C++ primer 模板与泛型编程
2024-08-28 06:09:24
继续浏览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();
}
}
最新文章
- 生物信息大数据&;数据库(NCBI、EBI、UCSC、TCGA)
- C++ 排序函数 sort(),qsort()的含义与用法 ,字符串string 的逆序排序等
- 用PHP实现一个高效安全的ftp服务器(一)
- 【转】ArrayList遍历的同时删除----不错
- angular 选中切换面板
- ASP.NET 初识Cookie
- 洛谷U19464 山村游历(Wander)(LCT,Splay)
- 从a文件判断是否删除b文件中的行(sed示例)
- A1024. Palindromic Number
- PCB封装步骤教程
- 使用 JdbcTemplate 查询数据时报错:列名无效(已解决)
- 在js中保存数据
- SDOI2018一轮NOI培训 题目整理
- ScreenCapturePro2 for Joomla_3.4.7-tinymce4x
- 地精排序Gnome Sort
- 互联网公司面试必问的mysql题目(下)
- 1136 A Delayed Palindrome (20 分)
- 「2017 山东三轮集训 Day7」Easy
- python标准库:Configparser模块
- NS3网络仿真(7): Wifi节点
热门文章
- Maven运行报错:-Dmaven.multiModuleProjectDirectory system propery is not set.
- HDU 3506 DP 四边形不等式优化 Monkey Party
- luogu1131 [ZJOI2007]时态同步
- How To Configure VMware fencing using fence
- C#Windows服务安装
- SQL server 数据库备份至服务器本地磁盘和其他服务器磁盘
- PHP优化之批量操作MySQL
- TOJ 4008 The Leaf Eaters
- C#中类的实例是不能 获取到类中的静态方法和静态变量(Static)的,及原因
- BZOJ 4810 [Ynoi2017]由乃的玉米田 ——Bitset 莫队算法