STL source code analysis-stl_queue.h

Source: Internet
Author: User
// Filename: stl_queue.h // comment by: Cream // E-mail: mdl2009@vip.qq.com // blog: http://blog.csdn.net/mdl13412////////////////////////////////////////////////////////////////////////////////// queue is an advanced FIFO (first in first out, FIFO) the data structure // It has two exits at the front and back, which are respectively the head and end of the team. // queue allows the append element and access element at the end of the team, retrieving and removing elements from the queue header // other elements cannot be accessed ////////////////////// //////////////////////////////////////// //////////////// the layout when deque is used. //// Front () is supported () and pop () support back () and push () // zookeeper // team head, team end // zookeeper // queue // ||| ...... | ...... | ...... | x | // reserved memory | // ------------------------ --------------------------- // The reserved memory is not used yet, it may be 0. The reserved memory is not used yet, it may be 0 ///////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ////// priority_queue is a method that allows users to push any element into the container in any order, // when an element is retrieved, it must start with the highest priority element. // by default, vector is used as the container, the default priority is less // The algorithm is binary-heap ///////////////////////// //////////////////////////////////////// /////////////// The following figure shows the working model, the container uses vector, and the priorities are determined by less /// team end. Push () // queues // ---------------------------------------------------------- // supports POP () and top () ---> | ...... | ...... | x | // -------------------------------------------------------- // reserved memory usage // | ----------------------- // The internal heap memory is not used yet, it may be 0 ///////////////////////////////////// //////////////////////////////////////// ///// The following is the vector used by the container, when the priority is determined by comparison, it is a Implementation Technique in the case of less, // borrow the example of Hou Jie, this technique is not used in this tutorial. // [a] // | // samples // | // [B] [C] // | //---------------------------------------------- // | // [d] [E] [f] [g] // | // ----------- | // [H] [I] [J] // reserved memory in the vector, it can be 0 // ------------------ // please wait // please // | not use | A | B | c | d | E | f | G | H | I | j | ...... | End | // -------------------------------------------------------------------------- // For details about the algorithm, see any algorithm book. If not, throw it :-)//////////////////////////////////// //////////////////////////////////////// //// *** copyright (c) 1994 * Hewlett-Packard Company ** permission to use, copy, modify, distribute and submit this software * and its documentation for any purpose is hereby granted without plugin, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. hewlett-Packard Company makes no * representations about the suitability of this software for any * purpose. it is provided "as is" without express or implied warranty. * ** copyright (c) 1996,1997 * Silicon Graphics Computer Systems, Inc. ** permission to use, copy, modify, distribute and merge this software * and its documentation for any purpose is hereby granted without tables, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. silicon Graphics makes no * representations about the suitability of this software for any * purpose. it is provided "as is" without express or implied warranty. * // * Note: This is an internal header file, encoded ded by other STL headers. * You shoshould not attempt to use it directly. */# ifndef _ sgi_stl_internal_queue_h # DEFINE _ types // If the compiler cannot deduce the default parameter type used later based on the preceding template parameters, you must manually specify it, in this tutorial, deque is used by default for internal queue containers. // As queue requires appending elements at the end of the team, get and remove elements from the queue header // It is very suitable to use deque # ifndef _ stl_limited_default_templatestemplate <class T, class sequence = deque <t> # elsetemplate <class t, class sequence> # endifclass queue {// For more information, see operator Analysis in <stl_pair.h>: Friend bool operator ==_ _ stl_null_tmpl_args (const queue & X, const queue & Y ); friend bool operator <_ stl_null_tmpl_args (const queue & X, const queue & Y); Public: // Since queue only supports operations on the head and end of the team, so do not define STL requirements // pointer, iterator, difference_type typedef typename sequence: value_type; typedef typename sequence: size_type; typedef typename sequence: Reference reference; typedef typename sequence: const_reference; protected: sequence C; // This is the container we actually maintain public: // These are the standard interfaces of STL queue, all call the member functions of the container for implementation // its interface is very similar to the stack implementation. For more information, see <stl_stack.h> bool empty () const {return C. empty ();} size_type size () const {return C. size ();} reference Front () {return C. front ();} const_reference Front () const {return C. front ();} reference back () {return C. back ();} const_reference back () const {return C. back ();} void push (const value_type & X) {C. push_back (x);} void POP () {C. pop_front () ;}}; // For more information, see <stl_pair.h> template <class T, class sequence> bool operator ==( const queue <t, sequence> & X, const queue <t, sequence> & Y) {return X. C = y. c;} template <class T, class sequence> bool operator <(const queue <t, sequence> & X, const queue <t, sequence> & Y) {return X. c <Y. c ;}# ifndef _ stl_limited_default_templatestemplate <class T, class sequence = vector <t>, class compare = less <typename sequence: value_type >># elsetemplate <class t, class sequence, class compare> # endifclass sequence {public: typedef typename sequence: value_type; typedef typename sequence: size_type sequence; typedef typename sequence: Reference reference; typedef typename sequence:: const_reference; protected: sequence C; // internally maintained container compare comp; // priority decision criterion public: priority_queue (): C () {} // you can specify your own priority decision making function (const compare & X): C (), comp (x) {} // use [first, last) priority_queue # ifdef _ stl_member_templates template <class inputiterator> priority_queue (inputiterator first, inputiterator last, const compare & X): C (first, last), comp (X) {make_heap (C. begin (), C. end (), comp);} template <class inputiterator> priority_queue (inputiterator first, inputiterator last): C (first, last) {make_heap (C. begin (), C. end (), comp) ;}# else/* _ stl_member_templates */priority_queue (const value_type * First, const value_type * Last, const compare & X): C (first, last), comp (x) {make_heap (C. begin (), C. end (), comp);} priority_queue (const value_type * First, const value_type * Last): C (first, last) {make_heap (C. begin (), C. end (), comp) ;}# endif/* _ stl_member_templates * // STL priority_queue standard interface bool empty () const {return C. empty ();} size_type size () const {return C. size ();} // returns the highest priority element const_reference top () const {return C. front ();} // Insert the element and adjust heap void push (const value_type & X) {_ stl_try {C. push_back (x); // for detailed analysis, see <stl_heap.h> push_heap (C. begin (), C. end (), comp);} _ stl_unwind (C. clear ();} // The element void POP () {_ stl_try {// for detailed analysis, see <stl_heap.h> pop_heap (C. begin (), C. end (), comp); C. pop_back ();} _ stl_unwind (C. clear () ;}}; // comparison operation _ stl_end_namespace # endif/* _ sgi_stl_internal_queue_h * // local variables: // mode: c ++ // end:

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.