Stack
Stack stack is an advanced (first in last out, Filo) data structure. In STL, the underlying container of the stack uses deque by default. You can also specify a vector or list container and adapt its interface to the stack interface. Some source code:
// Stack source code (partial) template <class _ TP, class _ sequence _ stl_dependent_default_tmpl (deque <_ TP>)> class Stack {public: typedef typename _ sequence :: value_type; typedef typename _ sequence: size_type; typedef _ Sequence sequence; typedef typename _ sequence: Reference reference; typedef typename _ sequence: const_reference; protected: _ Sequence C; // The internal maintenance container. Default Value: dequepublic: Stack (): C () {} explicit stack (const _ Sequence & _ s ): C (_ s) {} bool empty () const {return C. empty ();} // whether the size_type size () const {return C. size ();} // stack size reference top () {return C. back ();} // access the top element of the stack, const_reference top () const {return C. back ();} // access the top element of the stack (read-only) void push (const value_type & _ x) {C. push_back (_ x);} // inbound stack void POP () {C. pop_back ();} // out stack };
Queue
The queue is a data structure of first in first out (FIFO. In STL, the underlying queue container uses deque by default, and then its interface is adapted to the queue interface. Some source code:
// Queue source code (partial) template <class _ TP, class _ sequence _ stl_dependent_default_tmpl (deque <_ TP>)> class queue {public: typedef typename _ sequence :: value_type; typedef typename _ sequence: size_type; typedef _ Sequence sequence; typedef typename _ sequence: Reference reference; typedef typename _ sequence: const_reference; protected: _ Sequence C; // The internal maintenance container. Default Value: dequepublic: Queue (): C () {} explicit Queue (const _ Sequence & _ C ): C (_ C) {} bool empty () const {return C. empty ();} // whether the size_type size () const {return C. size ();} // queue size reference Front () {return C. front ();} // the first element of the access team const_reference Front () const {return C. front ();} // The first element (read-only) reference back () {return C. back ();} // access the team end element const_reference back () const {return C. back ();} // access the team end element (read-only) void push (const value_type & _ x) {C. push_back (_ x);} // void POP () {C. pop_front ();} // enter the team };
Priority_queue
Priority_queue is a priority queue that allows users to put elements into the container in any order, but the elements must be retrieved from the highest priority element. The default value is large, and the priority is high. priority_queue maintains a maximum heap with the vector container by default.
A Brief Introduction to heap is generally an implicit representation (implicit representation). In short, heap is implemented by another container (vector here. Binary-heap of a binary heap is or is approximately a Complete Binary Tree. It is usually stored in arrays because the traversal of sequences exactly corresponds to arrays one by one. There are two types of heap:
- The maximum heap value when the key value of the parent node is always greater than or equal to the key value of any child node
- The minimum heap value when the key value of the parent node is always smaller than or equal to the key value of any child node
The operations in the heap are based on the search of parent and child nodes. If arrays are used, you can easily implement them without additional storage space:
- Left subnode number = parent node number * 2 + 1
- Right child node number = parent node number * 2 + 2
- Parent node number = (child node number-1)/2
In addition, the heap requires dynamic array length, so it is implemented using vector. Some source code:
// Priority_queue source code (partial) template <class _ TP, class _ sequence _ stl_dependent_default_tmpl (vector <_ TP> ), class _ compare _ stl_dependent_default_tmpl (less <typename _ sequence: value_type>)> class priority_queue {public: typedef typename _ sequence: value_type; typedef typename _ sequence :: size_type; typedef _ Sequence container_type; typedef typename _ sequence: Reference reference; typedef typename _ sequence: const_reference; protected: _ Sequence C; // internal maintenance container, default Value: vector _ compare comp; // public: priority_queue (): C () {} explicit priority_queue (const _ compare & _ x): C (), comp (_ x) {}// you can specify your priority decision-making function priority_queue (const _ compare & _ x, const _ Sequence & _ s ): C (_ s), comp (_ x) {make_heap (C. begin (), C. end (), comp);} bool empty () const {return C. empty ();} // whether the size_type size () const {return C. size ();} // queue size const_reference top () const {return C. front ();} // element of the first access team (read-only, heap top element) // void push (const value_type & _ x) {_ stl_try {C. push_back (_ x); // put the push_heap (C. begin (), C. end (), comp); // push_heap generic algorithm. The element is inserted to the end first and then traced back to the end. The complexity is O (logn) }__ stl_unwind (C. clear ();} // void POP () {_ stl_try {pop_heap (C. begin (), C. end (), comp); // (1) Save the value at the end of the vector to the temporary variable; // (2) Place the value at the top of the heap at the end of the vector, the heap top is changed to an empty node (it can be understood as an infinitely small number for the largest heap); // (3) the hole at the top of the heap is traced down until it is moved to the bottom layer to become a leaf node; // (4) Place the original tail value in the empty hole that has been moved to the bottom layer; // (5) Perform a back-up operation on the original tail value of the empty hole to maintain the maximum heap characteristic; c. pop_back (); // After shuffling, delete the last element} _ stl_unwind (C. clear ());}};
STL note (3) Container adapter stack, queue, priority_queue