C++primer the construction of the queue template

Source: Internet
Author: User

1#include <iostream>2#include <vector>3#include <string>4 using namespacestd;5 6 7template<classType>classQueue;8 9 TenTemplate <classT> OneOstream &operator<< (Ostream&,Constqueue<t>&); A  -Template <classType>classQueueitem - { theFriendclassQueue<type>; -Friend ostream& -         operator<< <Type> (Ostream&,Constqueue<type>& ); -Queueitem (ConstType &t): item (t), Next (0){} + Type Item; -Queueitem *Next; +  A }; at  -Template <classType>classQueue - { -Friend ostream& -         operator<< <Type> (Ostream&,Constqueue<type>& ); -  Public: inQueue (): Head (0), Tail (0){} -Template <classIt> to Queue (It beg,it end): +Head0), Tail (0) {copy_elems (beg,end);} -Queue (ConstQueue &q): Head (0), Tail (0) the {copy_elems (Q);} *~Queue () {Destroy ();} $Template <classIter>voidassign (iter,iter);Panax Notoginsengtype& Front () {returnHead->item;} -     ConstType &front ()Const{returnHead->item;} the     voidPushConstType &); +     voidpop (); A     BOOLEmpty ()Const{ the         returnhead==0; } +  - Private: $Queueitem<type> *head; $Queueitem<type> *tail; -     voiddestroy (); -     voidCopy_elems (Constqueue&); theTemplate <classIter>voidCopy_elems (iter,iter); - Wuyi  the }; -  WuTemplate <classType>voidQueue<type>::d Estroy () - { About      while(!empty ()) $ pop (); -  - } -Template <classType>voidQueue<type>::p op () A { +Queueitem<type> *p=head; theHead=head->Next; -     Deletep; $  the } the  theTemplate <classType>voidQueue<type>::p Ush (ConstType &val) the { -Queueitem<type> *pt=NewQueueitem<type>(val); in     if(Empty ()) theHead=tail=pt; the     Else{ Abouttail->next=pt; theTail=pt; the     } the  +  - } the BayiTemplate <classType> the voidQueue<type>::copy_elems (ConstQueue &orig) the{ for(Queueitem<type> *pt=orig.head;pt;pt=pt->next) -Push (pt->item); -  the } the  thetemplate<classT> template <classIter> the voidQueue<t>:: Assign (Iter beg,iter end) - { the destroy (); the Copy_elems (beg,end); the }94  thetemplate<classType> template <classIt> the voidQueue<type>:: Copy_elems (It beg,it end) the {98      while(beg!=end) About     { -Push (*beg);101++Beg;102     }103 104 } thetemplate<classType>106ostream&operator<< (Ostream &os,ConstQueue<type> &q)107 {108os<<"<";109queueitem<type>*p; the      for(p=q.head;p;p=p->next)111os<<p->item<<" "; theos<<">";113     returnos; the  the } the 117 118 intMain ()119 { -vector<int>iter;121Iter.push_back (1);122Iter.push_back (2);123Iter.push_back (3);124Iter.push_back (5); thecout<<"vector<int> Test"<<Endl;126queue<int>Qi (iter.begin (), Iter.end ());127     //Cout<<qi.front () <<endl; -cout<<qi<<Endl;129  thecout<<"vector<string> Text"<<Endl;131vector<string>ster; theSter.push_back ("my");133Ster.push_back (" First");134Ster.push_back (" Program");135Ster.push_back ("!");136queue<string>Qs (Ster.begin (), Ster.end ());137     //Cout<<qs.front () <<endl;138cout<<qs<<Endl;139  $ 141 142 143cout<<"Assign Test"<<Endl;144Qi.assign (Iter.begin (), Iter.end ()-1);145cout<<qi<<Endl;146 147     148 149  Max 151  the 153     return 0;154}

Output:

Vector<int> Test
<1 2 3 5 >
vector<string> text
<my First Program! >
Assign Test
<1 2 3 >
Please press any key to continue ...

C++primer the construction of the queue template

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.