Stack summary in STL

Source: Internet
Author: User

(1) To use the stack, you must include the header file <stack >:# include <stack> (2) the stack definition in the header file is as follows: namespace std {template <class T, class Container = deque <T> class stack;} The first template parameter represents the element type. The second parameter with the default value is used to define the actual Container for storing elements in the stack, deque is used by default. Deque is used instead of vector because deque releases the memory when removing elements and does not have to copy all elements during reallocation. In fact, stack simply converts operations into the corresponding calls of internal containers. You can use any sequential container to support stacks, as long as they support back (), push_back (), pop_back () and other actions. (3) stack operations: stack () default constructor generates an empty stackexplicit stack: stack (const Container & cont) to generate a stack, take the element in the cont of the container as the initial value (copy) size_type stack: size () const to return the number of elements bool stack: empty () const to determine whether the stack is empty void stack :: push (const value_type & elem) inserts a copy of elem into the stack to create the first element value_type & stack: top () const value_type & stack: top () const returns the top element of the stack. The caller must ensure that the stack is not empty. Otherwise, it may be undefined. void stack: pop () removes the top element of the stack, the caller must ensure that the stack is not empty. Otherwise It may be an undefined action. bool comparison (const stack & st1, const stack & st2) returns the comparison results of two identical stacks. comparison can be one of the following operations: operator =, operator! =, Operator <, operator>, operator <=, operator> =

  

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.