c++/개념
스택 컨테이너 사용법
백수진
2021. 3. 21. 01:47

<stack container사용법>
1. stack 헤더파일 불러오기
#include <stack>
2. stack <type> 스택이름;
- vector와 동일한 방법으로 사용할 수 있음
3. push와 pop을 제공
- push
stack.push(값)
-pop
stack.pop()을 통해 값을 제거, stack.top()을 통해 last input값을 출력(이때, 제거되지는 않음)
#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack<int> st;
st.push(10);
st.push(20);
st.push(30);
while (!st.empty())
{
cout << st.top() << '\n';//맨위의 원소 제거 안 하고 값만 반환
st.pop();//원소제거
}
return 0;
}