STL_ Stack container


1、 Introduction to stack

Stack is a first in last out (Filo) data structure with only one exit. The stack container allows adding elements, removing elements, and getting the top elements of the stack, but there is no other way to access other elements of the stack except the top. In other words, stack does not allow traversal behavior.

  • The operation of pushing elements into the stack is called push
  • Pushing an element out of a stack is called a pop

Stack simply decorates deque container to become another kind of container.


2、 The default construction of the stack object

Stack is implemented by template class, and the default construction form of stack object is stack stkT;

Stack stkint; // a stack container for holding ints.
Stack stkflot; // a stack container for storing floats.
Stack stkstring; // a stack container for storing strings.
//You can also set pointer type or custom type in angle brackets.

3、 Push() and pop() methods of stack

stack.push / / add elements to the stack head; (elem)

stack.pop (); / / remove the first element from the stack head

stack stkInt;  

//At this time, the element stkint stores is 1

4、 Copy construction and assignment of stack object

Stack (const Stack & STK); / / copy constructor

Stack & operator = (const Stack & STK); / / overload equal sign operator

stack stkIntA;
Stack stkintb (stkinta); // copy construction
stack stkIntC;
Stkintc = stkinta; // assignment

5、 Data access of stack (); / / returns the last pushed element, that is, the top element

stack stkIntA;
int iTop =;      //9

6、 The size of the stack

stack.empty (); / / judge whether the stack is empty

stack.size (); / / returns the size of the stack

stack stkIntA;
if (!stkIntA.empty()){
	int iSize = stkIntA.size();      //2

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]