In this article we will implement the simple operation of the queue;
One, the queue version 1;
Through this program, we can see pop, push, back, front, size of the simple operation of the implementation;
//queue.hpp#ifndef Queue_h_#defineQueue_h_#include<deque>Template<typename T, TypeName V =std::d eque<t>//default type parameters >classqueue{ Public: TypeDef typename V::SIZE_TYPE Size_type; Queue () {}voidPushConstT &t) {elems_.push_back (t); }//functions that call deque voidpop () {Elems_.pop_front ();} ConstT &back ()Const { returnelems_.back ();} T&Back () {returnelems_.back ();} ConstT &front ()Const { returnElems_.front ();} T&Front () {returnElems_.front ();} Size_type size ()Const { returnelems_.size ();} BOOLEmpty ()Const { returnelems_.empty ();} Private: V elems_;///Unique data members //We store elements of type T in Elemss_ (deque) and invoke Deque interfaces to implement each of these functions};#endif
Main.cpp
1#include"queue.hpp"2#include <iostream>3#include <string>4 using namespacestd;5 6 intMainintargcConst Char*argv[])7 {8queue<string>Qu;9Qu.push ("Hello");TenQu.push (" World"); One Acout <<qu.size () <<Endl; - -cout << qu.back () <<Endl; the - while( !qu.empty ()) - { -cout << Qu.front () <<Endl; + Qu.pop (); - } + A return 0; at}
Two, queue version 2:
Although the above program has done some work, but to encounter some problems, it is helpless, for example:
Existing types queue<int,deque<int> > q1;
1, the queue<double, deque<double>> Q2 type of queue copy or assign to Q1;(only int to double, deque not changed)
2, or queue<double, list<double> > Q3 copied or assigned to Q1;(transposition int to double, change deque default type parameter to list)
The following programs help us achieve this type of transformation:
Stack.hpp
1 #ifndef Queue_h_2 #defineQueue_h_3 4#include <deque>5 6Template <typename T, typename V =std::d eque<t> >7 classQueue8 {9 Public:Ten typedef typename V::size_type Size_type; One Queue () {} A //to complete two different types of conversions, you need to reset the two parameters - //such as queue<int>-->queue<double> | |-->queue<double, list<double> > -Template <typename T2, TypeName v2> theQueue<t, V> (ConstQueue<t2, V2> &Other ); -Template <typename T2, TypeName v2> -Queue<t, V> &operator=(ConstQueue<t2, V2> &Other ); - + voidPushConstT &t) - {elems_.push_back (t); } + voidpop () A {Elems_.pop_front ();} at - ConstT &back ()Const -{returnelems_.back ();} -T &Back () -{returnelems_.back ();} - ConstT &front ()Const in{returnElems_.front ();} -T &Front () to{returnElems_.front ();} + -Size_type size ()Const the{returnelems_.size ();} * $ BOOLEmpty ()ConstPanax Notoginseng{returnelems_.empty ();} - Private: the V Elems_; + }; A theTemplate <typename T, TypeName v>//note to declare with two templates +Template <typename T2, TypeName v2> -Queue<t, V>::queue (ConstQueue<t2, v2> &other)//copy Constructor $ { $QUEUE<T2, v2> tmp (other);//Copy the other - while( !tmp.empty ()) - { thePush (Tmp.front ());//assign value to current queue<t,v> - Tmp.pop ();Wuyi } the } - WuTemplate <typename T, TypeName v> -Template <typename T2, TypeName v2> AboutQueue<t, v> &queue<t,v>::operator=(ConstQueue<t2, V2> &Other ) $ { - //determine if the two are equal - if((void*) This== (void*) &Other ) - return* This; A //Delete Element + while( !empty ()) the pop (); - //copying elements $Queue<t2, v2>tmp (other); the while( !tmp.empty ()) the { the push (Tmp.front ()); the Tmp.pop (); - } in } the the #endif
Main.cpp
1#include"queue.hpp"2#include <iostream>3#include <list>4 using namespacestd;5 6 intMainintargcConst Char*argv[])7 {8queue<int>qt;9Qt.push ( +);TenQt.push ( at); OneQt.push (9); AQt.push ( About); -Qt.push ( to); - thecout << qt.size () <<Endl; - -queue<Double>Qt2 (QT); -cout <<"Qt2"<<Endl; + while(!qt2.empty ()) - { +cout <<qt2.front () <<Endl; A Qt2.pop (); at } - -queue<Double, list<Double> >Qt3; -QT3 =qt; -cout <<"Qt3"<<Endl; - while(!qt3.empty ()) in { -cout <<qt3.front () <<Endl; to Qt3.pop (); + } - the return 0;
C + + Template app-----queue