C++primer Queue模板的構建

來源:互聯網
上載者:User

標籤:front   string   ems   public   const   names   blog   name   ssi   

  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 >
請按任意鍵繼續. . .

 

C++primer Queue模板的構建

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.