2. c++ STL stack

han811·2021년 2월 7일
0

c++

목록 보기
2/14
post-thumbnail
  • stack은 container가아니라 기존의 container들을 조합하여 만든 interface

  • deque, list, vector와 같은 sequence container를 포함하여 사용하며 default는 deque

  • 기본 함수

    empty() : 비어있는지 여부 true or false 반환
    size() : 원소 수 반환
    push(n) : n을 맨 위에 원소 추가
    top() : 맨 위의 원소 반환 - 비어있을 시 segment fault 발생
    pop() : 맨 위의 원소 삭제 - 비어있을 시 segment fault 발생

  • 사용법

// <stack> 헤더 추가
#include <stack>

// 비어있는 stack 생성
stack<int> s1;

// {1, 2, 3, 4, 5}로 초기화 된 stack 생성. s2.top()하면 5 나옴.
stack<int> s2({ 1, 2, 3, 4, 5 });

// deque를 복사하여 stack을 생성
deque<int> d1(5, 10);
stack<int> s3(d1);

// first parameter: type of element the stack holds
// second parameter: the container type used to implement the stack.
// vector와 deque로 만드는 stack
stack <int, vector<int>> s4; - sequence container를 vector로 지정
stack <int, deque<int>> s5;
reference
profile
han811

0개의 댓글