STL Learning Series IV: Stack container

Source: Internet
Author: User

About Stack

    • Stacks are stack containers, which are "advanced back-out" containers.
    • The stack is simply decorated with deque containers and becomes another container.
    • #include <stack>

Default construction of 1.stack objects

Stack is implemented as a template class, with the default constructor for stack objects: Stack <T> StkT;

Stack <int> stkint; A stack container that holds int.

Stack <float> stkfloat; A stack container that holds float.

Stack <string> stkstring; A stack container that holds a string.

You can also set pointer types or custom types within angle brackets.

2.stack push () and pop () methods

    • Stack.push (Elem); Adding elements to the stack header
    • Stack.pop (); Remove the first element from the stack header
#include <iostream>using namespacestd; #include<stack>voidobjPlay2 () {stack<int>Stkint; Stkint.push (1);//put it in 1 .Stkint.push (3);//put it in 3 .Stkint.pop ();//pop up an elementStkint.push (5);//put it in 5 .Stkint.push (7);//put it in 7 .Stkint.push (9);//put it in 9. This element is 1,5,7,9 .Stkint.pop ();//pop up an elementStkint.pop ();//an element pops up and the element is 1,5.}intMain () {objPlay2 ();return 0; }
copy Construction and assignment of 3.stack objects
    • Stack (const stack &STK); Copy constructor
    • stack& operator= (const stack &STK); Overloaded equals operator
voidobjPlay3 () {stack<int>Stkinta; Stkinta.push (1); Stkinta.push (3); Stkinta.push (5); Stkinta.push (7); Stkinta.push (9); Stack<int> Stkintb (Stkinta);//Copy Constructionstack<int>STKINTC; STKINTC= Stkinta;//Assign Value}

4.stack of data access

    • Stack.top (); Returns the last pressed-in stack element
voidObjPlay4 () {stack<int>Stkinta; Stkinta.push (1); Stkinta.push (3); Stkinta.push (5); Stkinta.push (7); Stkinta.push (9); intITop = Stkinta.top ();//gets the top element of the stack, that is, 9,top just gets the top element of the stack, pop is the top element of the popup stackStkinta.top () = +;// +}
Size of 5.stack
    • stack.empty ();  //Determine if the stack is empty
    • stack.size ();              //returns the size of the stack
 void   ObjPlay5 () {stack  <int  > Stkinta;    Stkinta.push ( 1  );    Stkinta.push ( 3  );    Stkinta.push ( 5  );    Stkinta.push ( 7  );    Stkinta.push ( 9  );  if  (! int  isize = Stkinta.size (); //  5 elements   

Here is all the code above:

#include <iostream>using namespacestd; #include<stack>voidobjPlay2 () {stack<int>Stkint; Stkint.push (1);//put it in 1 .Stkint.push (3);//put it in 3 .Stkint.pop ();//pop up an elementStkint.push (5);//put it in 5 .Stkint.push (7);//put it in 7 .Stkint.push (9);//put it in 9. This element is 1,5,7,9 .Stkint.pop ();//pop up an elementStkint.pop ();//an element pops up and the element is 1,5.}voidobjPlay3 () {stack<int>Stkinta; Stkinta.push (1); Stkinta.push (3); Stkinta.push (5); Stkinta.push (7); Stkinta.push (9); Stack<int> Stkintb (Stkinta);//Copy Constructionstack<int>STKINTC; STKINTC= Stkinta;//Assign Value}voidObjPlay4 () {stack<int>Stkinta; Stkinta.push (1); Stkinta.push (3); Stkinta.push (5); Stkinta.push (7); Stkinta.push (9); intITop = Stkinta.top ();//gets the top element of the stack, that is, 9,top just gets the top element of the stack, pop is the top element of the popup stackStkinta.top () = +;// +}voidObjPlay5 () {stack<int>Stkinta; Stkinta.push (1); Stkinta.push (3); Stkinta.push (5); Stkinta.push (7); Stkinta.push (9); if(!Stkinta.empty ()) {        intIsize = Stkinta.size ();//5 Elements    }}intMain () {objPlay2 ();    ObjPlay3 ();    ObjPlay4 ();    ObjPlay5 (); return 0;}

STL Learning Series IV: Stack container

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.