STL source code analysis-stl_stack.h

Source: Internet
Author: User
// Filename: stl_stack.h // comment by: Gel // E-mail: mdl2009@vip.qq.com // blog: http://blog.csdn.net/mdl13412////////////////////////////////////////////////////////////////////////////////// stack is a kind of advanced back-out (first in last out, Filo) data structure, which has only one exit // supports append to the top element of the stack, pop up, get, however, access to elements in other locations is not provided /////////////////////////////// //////////////////////////////////////// ///////// The following figure shows the layout when deque is used. /// the memory boundary reserved at the top of the current stack at the bottom of the stack // Please wait // ---------------------------------------------------------------------- // | ...... | ...... | x | // reserved memory | // | ----------------------------------- // | the reserved memory is not used yet, it may be 0 // | // only supports push (), pop (), top () operation /////////////////////////////////////// //////////////////////////////////////// // *** copyrig HT (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 handle, * 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 AB Out 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_ I Nternal_stack_h # DEFINE _ sgi_stl_internal_stack_h1_stl_begin_namespace // If the compiler cannot deduce the default parameter type used later based on the preceding template parameters, // You must manually specify the parameter type, by default, deque is used in the internal container of the stack. // deque can be used dynamically when the storage space is insufficient and the cost is low # ifndef _ stl_limited_default_templatestemplate <class T, class sequence = deque <t> # elsetemplate <class T, class sequence> # endifclass stack {// special global operator, provide operator = and <overload to construct all operators // For details, see the description in <stl_pair.h> friend bool Operator ==_ _ Stl_null_tmpl_args (const stack &, const stack &); friend bool operator <_ stl_null_tmpl_args (const stack &, const stack &); public: // because the stack only supports operations on the top elements of the stack, do not define the STL required // pointer, iterator, difference_type typedef typename sequence: value_type; typedef typename sequence :: size_type; typedef typename sequence: Reference reference; typedef typename sequence: const_reference cons T_reference; protected: sequence C; // This is the public container we actually maintain: // The following operations are fully implemented using the member functions of the Internal container // This reflects the high reusability of STL again :-) // determine whether the stack is empty bool empty () const {return C. empty ();} // number of elements in the stack size_type size () const {return C. size () ;}// return the top element of the stack. Note that reference is returned here !!! Reference top () {return C. back ();} const_reference top () const {return C. back ();} // append the new element void push (const value_type & X) {C. push_back (x);} // remove the top element of the stack. Note that reference of the element is not returned. // many beginners often mistakenly think of POP () when using this container at random () the Operation also returns the reference void POP () {C. pop_back () ;}}; // to determine whether two stacks are equal, test whether internal maintenance containers are equal. // X. C = y. C will call the operator = template <class T, class sequence> bool operator = (const stack <t, sequence> & X, const stack <t, sequence> & Y) {return X. C = y. c;} template <class T, class sequence> bool operator <(const stack <t, sequence> & X, const stack <t, sequence> & Y) {return X. c <Y. c ;:__ stl_end_namespace # endif/* _ sgi_stl_internal_stack_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.