Note: This article is only for learning and communication. For more information, see the source!
In the previous article, the implementation framework of STL container adapter Stack has introduced how STL uses basic containers to implement a common data structure stack ), this article introduces another STL container adapter internally defined by STL.Queue(Queue ).
A queue is a first-in-first-out data structure. Compared with the stackDifferencesIs:(1) A queue is a data structure of first-in-first-out mode, while a stack is a data structure of Second-in-first-out mode. (2) A queue supports access at both ends and end, the stack only supports access at one end (that is, the top). (3) The queue is inserted at the end of the team and popped up at the beginning of the team. The stack insertion and pop-up are all at the top of the stack.. Of course, the container adapter queue and stack haveCommonalitiesYes. Neither of them supports traversal and does not provide an iterator internally.
From the comparison above, we can infer that the two adapters have different requirements for the basic container, as shown in the difference in (3) mentioned above: the basic container of queue must support pop-up from the header, while the basic container of stack must support the pop-up at the end. In other words, the basic container of queue must support the pop_front () operation, while the basic container of stack must support the pop_back () operation. Because of this, vector, list, And deque containers can be used as the basic containers of the stack, while list and deque can be used as the basic containers of the queue, vector cannot be used as the basic container of queue (because vector does not provide the pop_front () operation,The default basic containers of the two adapters are deque..
Based on the Differences Between Stack and queue operations, we can compare and analyze the differences between stack and queue operations.
The operations provided by queue are as follows:
(1)Obtains the number of elements in the current queue.. Size_type size (),Stack also provides this operation.
(2)Obtain the first element of a team (not displayed). T & Front (),Stack does not provide this operation.
(3)Get the tail element (not displayed). T & back (),The function corresponding to the stack is T & Top ().
(4)Team-up operations. Void push (const T & T ),Stack also provides the same function, corresponding to the inbound stack operation.
(5)Team-out operations. Void POP (),Stack also provides the same function, CorrespondingOut-of-stack operations.
(6)Determine whether the queue is empty. Bool empty (),Stack also provides the same function.
If you want to use the queue adapter defined in STL, You need to reference the queue header file, # include <queue>.
The following shows the implementation code and test code of the queue adapter:
# Include <iostream> # include <deque> using namespace STD; /*************** queque definition **************/template <class t, class sequence = deque <t> class queue {friend bool operator = (const queue & X, const queue & Y); friend bool operator <(const queue & X, const queue & Y ); /*************** container adapter queue public attributes ****************** * **/public: typedef typename sequence: value_type; // container element type typedef typename sequence: Size _ Type size_type; // size type typedef typename sequence: Reference reference; // reference type typedef typename sequence: const_reference; // protected: sequence C; // basic container /*: bool empty () const; // judge whether it is empty size_type size () const; // number of elements reference Front (); // obtain the first element const_reference Front () const; reference back (); // get the team end element const_reference back () const; void push (const value _ Type & X); // enter the queue void POP (); // team-out operation }; /****************************/template <class t, class seq> bool queue <t, seq>: Empty () const // determines whether the queue is empty. Const cannot save {return C. empty ();} template <class T, class seq> typename queue <t, seq >:: size_type queue <t, seq >:: size () const // return the number of elements in the queue {return C. size ();} template <class T, class seq> typename queue <t, seq >:: reference queue <t, seq >:: Front () // obtain the first element {return C. fro NT ();} template <class T, class seq> typename queue <t, seq >:: const_reference queue <t, seq >:: Front () const // return the regular reference of the first element of the team {return C. front ();} template <class T, class seq> typename queue <t, seq >:: reference queue <t, seq >:: back () // retrieve the reference of the team end element {return C. back ();} template <class T, class seq> typename queue <t, seq >:: const_reference queue <t, seq >:: back () const // get reference of team end element {return C. back ();} template <class T, class seq> void queue <t, seq>: Push (const value_type & T) // pressure queue {C. push_back (t);} template <class T, class seq> void queue <t, seq >:: POP () // output queue {C. pop_front ();} int main () {queue <int> q; q. push (1); q. push (2); q. push (3); q. push (4); While (! Q. empty () {cout <"size =" <q. size () <"; cout <q. front () <Endl; q. pop () ;}return 0 ;}
References
[1] STL source code analysis Hou Jie
[2] C ++ primer 4th