stack_stack

stack_stack (std::stack::stack — 생성자)

std::stack 객체를 만드는 생성자들이에요. 기본 생성자부터 내부 컨테이너, 이터레이터 범위, 할당자, 복사·이동까지 다양한 방식으로 초기화할 수 있어요.

출처: cppreference

본문

주요 시그니처는 다음과 같아요.

stack() : stack(Container()) {}            // (1) (since C++11)
explicit stack( const Container& cont = Container() );   // (2)
explicit stack( Container&& cont );        // (3) (since C++11)
stack( const stack& other );               // (4) (암묵 선언)
stack( stack&& other );                    // (5) (since C++11, 암묵 선언)
template< class InputIt >
stack( InputIt first, InputIt last );      // (6) (since C++23)
template< class Alloc >
explicit stack( const Alloc& alloc );      // (7) (since C++11)
template< class Alloc >
stack( const Container& cont, const Alloc& alloc );   // (8) (since C++11)
template< class Alloc >
stack( Container&& cont, const Alloc& alloc );        // (9) (since C++11)
template< class Alloc >
stack( const stack& other, const Alloc& alloc );      // (10) (since C++11)

여러 오버로드가 있는데, 핵심은 다음과 같아요.

  • (1)(2) 기본 생성자 / 내부 컨테이너를 받는 생성자: 주어진 컨테이너로 스택을 만들어요.
  • (3) 내부 컨테이너를 이동해서 만들어요.
  • (6) 범위 [first, last)의 원소들로 스택을 만들어요.
  • (7~10) 할당자 관련 생성자들.

예제

#include <iostream>
#include <stack>
int main()
{
    std::stack<int> s;         // 기본 생성
    s.push(1); s.push(2);
    std::cout << s.top() << '\n';   // 2
}

더 알아보기 (Learn more)

cppreference