C++/STL
[STL] Stack
화요밍
2021. 1. 11. 23:06
728x90
반응형
Stack은 LIFO(Last In First Out, 후입선출)의 자료구조이다.
C++의 표준 템플릿 라이브러리로 정의되어 있다. #include <stack>
기본 함수
top() | Stack의 가장 위의 원소를 반환 |
push(element) | top에 원소 추가 |
pop() | top 원소를 반환하고 삭제 |
empty() | Stack이 비어있으면 true, 아니면 false를 반환 |
size() | Stack 사이즈를 반환 |
사용 예제
#include <iostream>
#include <stack>
using namespace std;
int main(int argc, const char * argv[]) {
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
cout << "now top = " << s.top() << endl;
s.pop();
cout << "now top = " << s.top() << endl;
s.pop();
cout << "now top = " << s.top() << endl;
cout << "size = " << s.size() << endl;
if(s.empty()) cout << "true" << endl;
else cout << "false" << endl;
return 0;
}
출력
now top = 3
now top = 2
now top = 1
size = 1
false
728x90
반응형