[C++] STL - introduction and use of stack, introduction and use of push and pop functions of stack, other member functions of stack

1. Introduction to stack

Introduction to stack

insert image description here

  1. stack is a kind of container adapter, which is specially used in the context environment with last-in-first-out operation, and its deletion can only insert and extract elements from one end of the container.

  2. The stack is implemented as a container adapter, which encapsulates a specific class as its underlying container, and provides a set of specific member functions to access its elements, using a specific class as its underlying, element-specific container The tail (i.e. the top of the stack) is pushed and popped.

  3. The underlying container of the stack can be any standard container class template or some other specific container classes. These container classes should support the following operations: empty: null operation back: get
  tail
  element operation
  push_back: tail insert element operation
  pop_back: tail delete element operation

  4. The standard containers vector, deque, and list all meet these requirements. By default, if no specific underlying container is specified for the stack, deque is used by default.

insert image description here

2. The use of stack

insert image description here

2.1stack constructor

insert image description here
insert image description here
  The stack class is a container adapter that is implemented based on other container types (deque by default). The stack class provides a last-in-first-out (LIFO) data structure, similar to a real-life stack.

#include <iostream>
#include <stack>
#include <vector>

int main() {
    
    
    // 示例1:使用默认构造函数创建一个空的stack对象。
    std::stack<int> myStack;

    // 示例2:使用拷贝构造函数创建一个与现有stack对象相同的新对象。
    std::stack<int> otherStack;
    otherStack.push(1);
    otherStack.push(2);
    std::stack<int> newStack(otherStack);

    // 示例3:使用构造函数模板创建一个基于vector的stack对象。
    std::stack<int, std::vector<int>> intStack;
    intStack.push(10);
    intStack.push(20);

    // 输出示例2中新创建的stack对象的元素
    std::cout << "示例2中新创建的stack对象的元素:";
    while (!newStack.empty()) {
    
    
        std::cout << newStack.top() << " ";
        newStack.pop();
    }
    std::cout << std::endl;

    // 输出示例3中基于vector的stack对象的元素
    std::cout << "示例3中基于vector的stack对象的元素:";
    while (!intStack.empty()) {
    
    
        std::cout << intStack.top() << " ";
        intStack.pop();
    }
    std::cout << std::endl;

    return 0;
}

//示例2中新创建的stack对象的元素:2 1 
//示例3中基于vector的stack对象的元素:20 10 

2.1stack member function

(1) empty() detects whether the stack is empty

  empty() is a member function of the stack class, used to check whether the current stack is empty. When there is no element in the stack, the empty() function returns true; when there is at least one element in the stack, the empty() function returns false.

  The use of the empty() function is very simple, just call the function on a stack object. The following is an example usage of the empty() function:

#include <iostream>
#include <stack>

int main() {
    
    
    std::stack<int> myStack;

    // 检查stack是否为空
    if (myStack.empty()) {
    
    
        std::cout << "Stack is empty" << std::endl;
    } else {
    
    
        std::cout << "Stack is not empty" << std::endl;
    }

    // 将元素压入stack
    myStack.push(10);
    myStack.push(20);

    // 再次检查stack是否为空
    if (myStack.empty()) {
    
    
        std::cout << "Stack is empty" << std::endl;
    } else {
    
    
        std::cout << "Stack is not empty" << std::endl;
    }

    return 0;
}

//Stack is empty
//Stack is not empty

(2) size() returns the number of elements in the stack

  size() is a member function of the stack class, which is used to get the number of elements in the current stack. The size() function returns an integer representing the number of elements in the stack.

  The use of the size() function is very simple, just call the function on a stack object. The following is an example usage of the size() function:

#include <iostream>
#include <stack>

int main() {
    
    
    std::stack<int> myStack;

    // 添加元素到stack
    myStack.push(10);
    myStack.push(20);
    myStack.push(30);

    // 获取stack中的元素数量
    int stackSize = myStack.size();
    std::cout << "Stack size: " << stackSize << std::endl;

    // 弹出一个元素
    myStack.pop();

    // 再次获取stack中的元素数量
    stackSize = myStack.size();
    std::cout << "Stack size after pop: " << stackSize << std::endl;

    return 0;
}

//Stack size: 3
//Stack size after pop: 2

(3) top() returns a reference to the top element of the stack

  top() is a member function of the stack class, which is used to obtain the value of the element at the top of the stack (that is, the last push), without removing the element from the stack.

  The use of the top() function is very simple, just call the function on a stack object. Following is an example usage of top() function:

#include <iostream>
#include <stack>

int main() {
    
    
    std::stack<int> myStack;

    // 添加元素到stack
    myStack.push(10);
    myStack.push(20);
    myStack.push(30);

    // 获取stack顶部的元素值
    int topElement = myStack.top();
    std::cout << "Top element: " << topElement << std::endl;

    return 0;
}

//Top element: 30

(4) push() pushes the element val into the stack

  push() is a member function of the stack class, which is used to push (that is, add) an element to the top of the stack.

  The use of the push() function is very simple, just call the function on a stack object and pass the element to be pushed as a parameter. The following is an example usage of the push() function:

#include <iostream>
#include <stack>

int main() {
    
    
    std::stack<int> myStack;

    // 将元素压入stack
    myStack.push(10);
    myStack.push(20);
    myStack.push(30);

    // 获取stack顶部的元素值
    int topElement = myStack.top();
    std::cout << "Top element: " << topElement << std::endl;

    return 0;
}

//Top element: 30

(5) pop() pops up the elements at the end of the stack

  pop() is a member function of the stack class, which is used to remove the element at the top of the stack (that is, the last push), but does not return the value of the element.

  The use of the pop() function is very simple, just call the function on a stack object. Following is an example usage of pop() function:

#include <iostream>
#include <stack>

int main() {
    
    
    std::stack<int> myStack;

    // 添加元素到stack
    myStack.push(10);
    myStack.push(20);
    myStack.push(30);

    // 移除stack顶部的元素
    myStack.pop();

    // 获取新的stack顶部的元素值
    int topElement = myStack.top();
    std::cout << "Top element after pop: " << topElement << std::endl;

    return 0;
}

//Top element after pop: 20

Guess you like

Origin blog.csdn.net/Crocodile1006/article/details/132012532