Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::stack

      From cppreference.com
      <cpp‎ |container
       
       
       
       
      Defined in header<stack>
      template<

         class T,
         class Container=std::deque<T>

      >class stack;

      Thestd::stack class is acontainer adaptor that gives the programmer the functionality of astack - specifically, a LIFO (last-in, first-out) data structure.

      The class template acts as a wrapper to the underlying container - only a specific set of functions is provided. The stack pushes and pops the element from the back of the underlying container, known as the top of the stack.

      All member functions ofstd::stack areconstexpr: it is possible to create and usestd::stack objects in the evaluation of a constant expression.

      However,std::stack objects generally cannot beconstexpr, because any dynamically allocated storage must be released in the same evaluation of constant expression.

      (since C++26)

      Contents

      [edit]Template parameters

      T - The type of the stored elements. The program is ill-formed ifT is not the same type asContainer::value_type.
      Container - The type of the underlying container to use to store the elements. The container must satisfy the requirements ofSequenceContainer. Additionally, it must provide the following functions with theusual semantics:

      The standard containersstd::vector (includingstd::vector<bool>),std::deque andstd::list satisfy these requirements. By default, if no container class is specified for a particular stack class instantiation, the standard containerstd::deque is used.

      [edit]Member types

      Type Definition
      container_typeContainer[edit]
      value_typeContainer::value_type[edit]
      size_typeContainer::size_type[edit]
      referenceContainer::reference[edit]
      const_referenceContainer::const_reference[edit]

      [edit]Member objects

      Member Description
      Container c
      the underlying container
      (protected member object)[edit]

      [edit]Member functions

      constructs thestack
      (public member function)[edit]
      destructs thestack
      (public member function)[edit]
      assigns values to the container adaptor
      (public member function)[edit]
      Element access
      accesses the top element
      (public member function)[edit]
      Capacity
      checks whether the container adaptor is empty
      (public member function)[edit]
      returns the number of elements
      (public member function)[edit]
      Modifiers
      inserts element at the top
      (public member function)[edit]
      (C++23)
      inserts a range of elements at the top
      (public member function)[edit]
      (C++11)
      constructs element in-place at the top
      (public member function)[edit]
      removes the top element
      (public member function)[edit]
      (C++11)
      swaps the contents
      (public member function)[edit]

      [edit]Non-member functions

      lexicographically compares the values of twostacks
      (function template)[edit]
      specializes thestd::swap algorithm
      (function template)[edit]

      [edit]Helper classes

      specializes thestd::uses_allocator type trait
      (class template specialization)[edit]
      formatting support forstd::stack
      (class template specialization)[edit]

      Deduction guides

      (since C++17)

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_containers_ranges202202L(C++23)Ranges construction and insertion for containers
      __cpp_lib_constexpr_stack202502L(C++26)constexprstd::stack

      [edit]Example

      This section is incomplete
      Reason: no example

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 307C++98Container could not bestd::vector<bool>allowed
      LWG 2566C++98Missing the requirement forContainer::value_typeill-formed ifT is not the same type asContainer::value_type

      [edit]See also

      resizable contiguous array
      (class template)[edit]
      space-efficient dynamic bitset
      (class template specialization)[edit]
      double-ended queue
      (class template)[edit]
      doubly-linked list
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/stack&oldid=182871"

      [8]ページ先頭

      ©2009-2025 Movatter.jp