1 #include<iostream> 2 #include<vector> 3 #include<string> 4 using namespace std; 5 6 7 template<class Type> class Queue; 8 9 10 template <class T> 11 ostream & operator<<(ostream&,const Queue<T>&); 12 13 template <class Type> class QueueItem 14 { 15 friend class Queue<Type>; 16 friend ostream& 17 operator << <Type>(ostream&,const Queue<Type>& ); 18 QueueItem(const Type &t):item(t),next(0){} 19 Type item; 20 QueueItem *next; 21 22 }; 23 24 template <class Type> class Queue 25 { 26 friend ostream& 27 operator << <Type>(ostream&,const Queue<Type>& ); 28 public: 29 Queue():head(0),tail(0){} 30 template <class It> 31 Queue(It beg,It end): 32 head(0),tail(0){copy_elems(beg,end);} 33 Queue(const Queue &Q):head(0),tail(0) 34 {copy_elems(Q);} 35 ~Queue(){destroy();} 36 template <class Iter> void assign(Iter,Iter); 37 Type& front(){return head->item;} 38 const Type &front() const {return head->item;} 39 void push(const Type &); 40 void pop(); 41 bool empty() const{ 42 return head==0; } 43 44 private: 45 QueueItem<Type> *head; 46 QueueItem<Type> *tail; 47 void destroy(); 48 void copy_elems(const Queue&); 49 template <class Iter> void copy_elems(Iter,Iter); 50 51 52 }; 53 54 template <class Type> void Queue<Type>::destroy() 55 { 56 while(!empty()) 57 pop(); 58 59 } 60 template <class Type> void Queue<Type>::pop() 61 { 62 QueueItem<Type> *p=head; 63 head=head->next; 64 delete p; 65 66 } 67 68 template <class Type> void Queue<Type>::push(const Type &val) 69 { 70 QueueItem<Type> *pt=new QueueItem<Type>(val); 71 if(empty()) 72 head=tail=pt; 73 else{ 74 tail->next=pt; 75 tail=pt; 76 } 77 78 79 } 80 81 template <class Type> 82 void Queue<Type>::copy_elems(const Queue &orig) 83 { for(QueueItem<Type> *pt=orig.head;pt;pt=pt->next) 84 push(pt->item); 85 86 } 87 88 template<class T> template <class Iter> 89 void Queue<T>::assign(Iter beg,Iter end) 90 { 91 destroy(); 92 copy_elems(beg,end); 93 } 94 95 template<class Type> template <class It> 96 void Queue<Type>::copy_elems(It beg,It end ) 97 { 98 while(beg!=end) 99 { 100 push(*beg); 101 ++beg; 102 } 103 104 } 105 template<class Type> 106 ostream& operator <<(ostream &os,const Queue<Type> &q) 107 { 108 os<<"<"; 109 QueueItem<Type>*p; 110 for(p=q.head;p;p=p->next) 111 os<<p->item<<" "; 112 os<<">"; 113 return os; 114 115 } 116 117 118 int main() 119 { 120 vector<int> iter; 121 iter.push_back(1); 122 iter.push_back(2); 123 iter.push_back(3); 124 iter.push_back(5); 125 cout<<" vector<int> test"<<endl; 126 Queue<int> qi(iter.begin(),iter.end()); 127 //cout<<qi.front()<<endl; 128 cout<<qi<<endl; 129 130 cout<<"vector<string> text"<<endl; 131 vector<string> ster; 132 ster.push_back("my"); 133 ster.push_back("first"); 134 ster.push_back("program"); 135 ster.push_back("!"); 136 Queue<string> qs(ster.begin(),ster.end()); 137 //cout<<qs.front()<<endl; 138 cout<<qs<<endl; 139 140 141 142 143 cout<<"assign test"<<endl; 144 qi.assign(iter.begin(),iter.end()-1); 145 cout<<qi<<endl; 146 147 148 149 150 151 152 153 return 0; 154 }
输出:
vector<int> test
<1 2 3 5 >
vector<string> text
<my first program ! >
assign test
<1 2 3 >
请按任意键继续. . .