[STL] stack
Updated:
C++의 STL stack에 대해 정리해 보았다.
stack 선언
stack<int> new_stack;와 같은 형식으로 선언한다. int에는 다른 타입도 올 수 있다.
stack 함수
추가 / 삭제
- push(element): element를 top에 추가
- pop(): top에 있는 element 삭제
조회
- top(): top에 있는 element를 반환
기타
- empty(): 스택이 empty이면 true, 그렇지 않으면 false 반환
- size(): 스택의 size 반환
구현
#include <iostream>
#include <stack>
int main()
{
    stack<int> s;
    s.push(1);  // 1
    s.push(2);  // 1 2
    s.push(3);  // 1 2 3
    cout << "top: " << s.top() << '\n'; // top: 3
    s.pop();    // 1 2
    s.pop();    // 1
    s.push(5);  // 1 5
    cout << "top: " << s.top() << '\n';     // top: 5
    cout << "size: " << s.size() << '\n';   // size: 2
    cout << "empty: " << s.empty() << '\n'; // empty: 0
    return 0;
}
Leave a comment