第16章 模板与泛型编程(12)
16.4.6 完整的Queue类
//Queue.h
#ifndef QUEUE_H
#define QUEUE_H
#include "stdafx.h"
#include "QueueItem.h"
#include <iostream>
using namespace std;
template <class Type>
class Queue{
public:
Queue():head(0), tail(0){}
Queue(const Queue &Q):head(0), tail(0)
{copy_elems(Q);}
Queue& operator=(const Queue&);
~Queue(){destroy();}
Type& front()
{return head->item;}
const Type &front() const{return head->item;}
void push(const Type &);
void pop();
bool empty() const
{return head==0;}
friend ostream &operator<< <Type>(ostream &os, const Queue<Type> &q);
template<class It>
Queue(It beg, It end):head(0), tail(0){copy_elems<It>(beg,end)}
template<class Iter>
void assign(Iter, Iter);
private:
QueueItem<Type> *head;
QueueItem<Type> *tail;
void destroy();
void copy_elems(const Queue&);
template <class Iter>
void copy_elems(Iter,Iter);
};
template<class Type> void Queue<Type>::destroy(){
while(!empty())
pop();
}
template<class Type> void Queue<Type>::pop(){
QueueItem<Type> *p=head;
this->head=this->head->next;
delete p;
}
template<class Type> void Queue<Type>::push(const Type &p){
QueueItem<Type> *pt=new QueueItem<Type>(p);
if(empty()){
pt->next=0;
head=tail=pt;
}
else{
this->tail->next=pt;
tail=pt;
}
}
template <class Type>
void Queue<Type>::copy_elems(const Queue &orig){
for(QueueItem<Type> *pt=orig.head;pt==0;pt=pt->next){
push(pt->item);
}
}
template <class Type>
ostream& operator<<(ostream &os, const Queue<Type> &q){
os<<"< ";
QueueItem<Type> *p;
for(p=q.head;p!=0;p=p->next){
os<<p->item<<" ";
}
os<<">";
return os;
}
template<class Type>
template<class Iter>
void Queue<Type>::assign(Iter beg, Iter end){
destroy();
copy_elems(beg,end);
}
#endif
//Queue.h
#ifndef QUEUE_H
#define QUEUE_H
#include "stdafx.h"
#include "QueueItem.h"
#include <iostream>
using namespace std;
template <class Type>
class Queue{
public:
Queue():head(0), tail(0){}
Queue(const Queue &Q):head(0), tail(0)
{copy_elems(Q);}
Queue& operator=(const Queue&);
~Queue(){destroy();}
Type& front()
{return head->item;}
const Type &front() const{return head->item;}
void push(const Type &);
void pop();
bool empty() const
{return head==0;}
friend ostream &operator<< <Type>(ostream &os, const Queue<Type> &q);
template<class It>
Queue(It beg, It end):head(0), tail(0){copy_elems<It>(beg,end)}
template<class Iter>
void assign(Iter, Iter);
private:
QueueItem<Type> *head;
QueueItem<Type> *tail;
void destroy();
void copy_elems(const Queue&);
template <class Iter>
void copy_elems(Iter,Iter);
};
template<class Type> void Queue<Type>::destroy(){
while(!empty())
pop();
}
template<class Type> void Queue<Type>::pop(){
QueueItem<Type> *p=head;
this->head=this->head->next;
delete p;
}
template<class Type> void Queue<Type>::push(const Type &p){
QueueItem<Type> *pt=new QueueItem<Type>(p);
if(empty()){
pt->next=0;
head=tail=pt;
}
else{
this->tail->next=pt;
tail=pt;
}
}
template <class Type>
void Queue<Type>::copy_elems(const Queue &orig){
for(QueueItem<Type> *pt=orig.head;pt==0;pt=pt->next){
push(pt->item);
}
}
template <class Type>
ostream& operator<<(ostream &os, const Queue<Type> &q){
os<<"< ";
QueueItem<Type> *p;
for(p=q.head;p!=0;p=p->next){
os<<p->item<<" ";
}
os<<">";
return os;
}
template<class Type>
template<class Iter>
void Queue<Type>::assign(Iter beg, Iter end){
destroy();
copy_elems(beg,end);
}
#endif16.4.7 类模板的static成员
类模板可以像任何其他类一样声明static成员。
template<class T>
class Foo{
public:
static T count(){return ctr;}
private:
static T ctr;
};
template<class T>
T Foo<T>::ctr;
template<class T>
class Foo{
public:
static T count(){return ctr;}
private:
static T ctr;
};
template<class T>
T Foo<T>::ctr;
静态数据成员需要在类外初始化。
1. 使用类模板的static成员
Foo<int> fi, fi2;
Foo<int>::count();
size_t ct=fi.count();
ct=fi2.count();
Foo<int> fi, fi2;
Foo<int>::count();
size_t ct=fi.count();
ct=fi2.count();可以通过类类型的对象访问模板的static成员,或者通过使用作用域操
补充:软件开发 , C++ ,