Introduction to C + + STL stack container stack usage

Source: Internet
Author: User

Stack stacks container

The stack is a linear table, and insertions and deletions are made only at one end of the table. This end is called the top of the stack, and the other end is the stack bottom (stack Bottom). The element insertion of the stack is called the stack, and the deletion of the element is called the stack. Because the element's stack and stack are always on top of the stack, the stack is a LIFO table, the LIFO table.
The C + + STL stack generalization is implemented directly through the existing Sequence container. The default use of DEQUE data structure of the two-terminal queue, of course, you can use other linear structure (vector or list, etc.), as long as the stack to provide stacks, stack, stack top element access and determine whether the operation can be empty. Because the bottom of the stack uses other containers, the stack can be viewed as an adapter that converts a container into another container (the stack container).
To strictly follow the data LIFO principle of the stack, stack does not provide any iterator action for the element, so the stack container does not provide an externally available forward or reverse iterator type.
The C + + standard header file for the stack stack container is stack and must be included in the macro statement "#include <stack>" to compile the stack stack's program.

Creating a Stack Object
Before using the stack, the constructor is used to initialize and create a stack object for the elements to stack, stack, and so on.
1. Stack ()
Default constructor, which creates an empty stack object.
For example, the following line of code uses the default deque for the underlying container, creating an empty stack object s.
Stack<int> s;

2. Stack (const stack&)
Copy the constructor and create a new stack with a stack heap.
For example, the following code uses S1 to create an empty stack object s2 with a doubly linked list as the underlying container.
Stack<int, list<int> > S1;
Stack<int, list<int> > s2 (S1);


element into stack
The element in the stack stack container functions as a push function. Because the C + + STL stack function is not preset size, therefore, the stack function does not take into account the stacking space is full, the elements are pressed into the stack, so that the function does not indicate the success of the stack return value.
His use of the prototype is as follows:
void push (const value_type& x)


Element out Stack
The stack container's element stack function is a pop function, because the function does not determine whether the stack is empty before the element is ejected, so it is necessary to determine whether the stack is empty to perform pop functions.
void Pop ()

The following example code, all the elements of the stack out of the stack
Stack<int> s;
while (!s.empty ())
{
S.pop ();/out Stack
}


Take the top element of the stack
The read function of the top element of the stack container is the POP function, which will take out the last element in the stack, as follows is its use prototype
value_type& Top ()

Stack non-null judgment
As stack elements continue to stack, the stack may appear empty, therefore, it is generally necessary to call the empty function to determine whether it is not empty, to make the element out of the stack and take the top of the stack elements of the operation.
BOOL Empty ()
Determines whether the stack is empty, and returns true to indicate that the stack is empty and false indicates that the stack is not empty.




C + + STL Stack Introduction


C + + stack (stack) is an adaptation of a container class, providing the programmer with all the functionality of the stack-that is, an advanced FILO data structure.

the header files for C + + STL stack stacks are:

#include <stack>

Introduction to member functions of C + + STL stack

Operation comparison and allocation stack

Returns True if the empty () stack is empty

Pop () remove top element from stack

Push () adds elements to the top of the stack

Size () returns the number of elements in the stack

Top () returns the Stack element

Stack top element-----------------------------------------read stack
#include <stack>
#include <iostream>
using namespace std;
int main ()
{
    //Create Stack object
    stack<int> s;
    Elements into the stack
    s.push (3);
    S.push (a);
    S.push (a);
    S.push ();
    S.push (m);
    S.push (4);

    Elements sequentially out
    of Stack while (!s.empty ())
    {
        //print stack top elements, printed: 4 3
        cout << s.top () << endl;
  
   //out Stack
        s.pop ();
    }

    return 0;
}
  


/* The
    number of elements in the size stack of the stack can be obtained by the size function. Each time the element goes into the stack, the size of the current stack is checked, exceeding a threshold value, and the element is not allowed into the stack, which enables a stack with a certain capacity limit.
    size_type  size ()
    returns the number of elements in the current stack the
    
    following example program sets the size of the stack to 100 int elements, and uses the list doubly linked list as the underlying container for the stack. Each time the element is pressed, it is determined whether the stack is larger than the bounds of 100 elements, thereby implementing a stack with a capacity limit.
* *

-----------------------------------------limit stack size
#include <stack>
#include <list >
#include <iostream>
#define STACK_SIZE   //stack maximum capacity
using namespace std;
int main ()
{
    //The underlying structure of the stack using a two-way linked list
    stack<int, list<int> >   s; 
    The stack is not full, the element can be in stack
    if (s.size () < stack_size)
        S.push ();
    if (S.size () < stack_size)
        S.push (1);
    if (S.size () < stack_size)
        S.push ();
    Element stack while
    (!s.empty ())
    {
        //print 1
        cout << s.top () << Endl;
        S.pop ();
    }

    return 0;
}


The

-----------------------Stack summary
     Stack is a widely used data structure. The C + + STL encapsulates this data structure and several of its restricted operations in a generic class stack container, which is easy to apply, including stack initialization, element-stack, top-stack elements, element stacks, determining whether the stack is non-null, and getting the current stack size. The
    stack element stack operation is not returned to the top of the stack, and needs to be obtained by taking the stack top function separately. This separation implementation takes into account that if the stack function returns directly to the top of the stack, it will result in the return value of the data referring to the security issue or unnecessary inefficient copy function invocation.
    
     The stack stack is not set to the maximum capacity from the inside of the stack, but the size function can be used to get the current stack sized to determine whether the element is allowed to continue to stack. Implements a stack with a maximum capacity limit.

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.