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();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值