C++ Standard Library<stack>

In C++, the standard library provides a variety of containers and algorithms to help developers write programs more efficiently.

<stack>It is part of the C++ Standard Template Library (STL) and implements a Last In First Out (LIFO) data structure. This data structure is very suitable for scenarios where the "last added element is removed first."

<stack>The container adapter provides a stack interface, which is based on other containers (such asdequeorvector) to implement. The elements of the stack are arranged linearly, but addition and removal operations are only allowed at one end (the top of the stack).

Basic Operations

  • push(): Adds an element to the top of the stack.
  • pop(): Removes the top element of the stack.
  • top(): Returns a reference to the top element of the stack without removing it.
  • empty(): Checks whether the stack is empty.
  • size(): Returns the number of elements in the stack.

Syntax

The following is using<stack>basic syntax:

#include <iostream>
#include <stack>

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

    // 向栈中添加元素
    s.push(1);
    s.push(2);
    s.push(3);

    // 访问栈顶元素
    std::cout << "Top element is: " << s.top() << std::endl;

    // 移除栈顶元素
    s.pop();
    std::cout << "After popping, top element is: " << s.top() << std::endl;

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

    // 打印栈的大小
    std::cout << "Size of stack: " << s.size() << std::endl;

    return 0;
}

Example

Below is a complete example using<stack>, including the output result:

Example

#include <iostream>
#include <stack>

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

    // Add elements to the stack
    s.push(10);
    s.push(20);
    s.push(30);

    // Print the top element of the stack
    std::cout << "Top element is: " << s.top() << std::endl; // Output: Top element is: 30

    // Remove the top element of the stack
    s.pop();
    std::cout << "After popping, top element is: " << s.top() << std::endl; // Output: After popping, top element is: 20

    // Check if the stack is empty
    if (!s.empty()) {
        std::cout << "Stack is not empty." << std::endl; // Output: Stack is not empty.
    }

    // Print the size of the stack
    std::cout << "Size of stack: " << s.size() << std::endl; // Output: Size of stack: 2

    // Continue removing elements
    s.pop();
    s.pop();

    // Check if the stack is empty
    if (s.empty()) {
        std::cout << "Stack is empty." << std::endl; // Output: Stack is empty.
    }

    return 0;
}

Output result:

Top element is: 30
After popping, top element is: 20
Stack is not empty.
Size of stack: 2
Stack is empty.

Notes

  • <stack>It does not provide a method for direct access to elements in the stack; it can only be accessed throughtop()Access the top element of the stack.
  • Attempting to call on an empty stacktop()orpop()Will lead to undefined behavior.
  • <stack>The underlying container of the stack can be any sequence container that supports random access iterators, such asvectorordeque。
other extensions