rim 构造 ostream sig new template str 函数
继续浏览 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 模板与泛型编程
来源: http://www.bubuko.com/infodetail-2053116.html