Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

stack::push() và stack::pop() trong C++ STL

push() thêm phần tử vào đỉnh stack. pop() loại bỏ phần tử đỉnh stack.

Cú pháp

stack.push(value);
stack.pop();

Ví dụ

#include <iostream>
#include <stack>
using namespace std;

int main() {
    stack<int> st;
    st.push(1); st.push(2); st.push(3); st.push(4); st.push(5); st.push(6);
    while (!st.empty()) {
        cout << "Size: " << st.size() << endl;
        st.pop();
    }
    return 0;
}

Đầu ra

Size: 6
Size: 5
Size: 4
Size: 3
Size: 2
Size: 1