标准库标头 <stack>

来自cppreference.com


 
 
标准库头
 

此头文件是容器库的一部分。

包含

(C++20)
三路比较运算符支持[编辑]
std::initializer_list 类模板[编辑]

适配一个容器以提供栈(LIFO 数据结构)
(类模板) [编辑]
特化 std::uses_allocator 类型特征
(类模板特化) [编辑]

函数

按照字典顺序比较两个 stack 的值
(函数模板) [编辑]
特化 std::swap 算法
(函数模板) [编辑]

概要

#include <compare>
#include <initializer_list>

namespace std {
  // 类模板 stack
  template<class T, class Container = deque<T>> class stack;

  template<class T, class Container>
  constexpr bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
  template<class T, class Container>
  constexpr bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
  template<class T, class Container>
  constexpr bool operator<(const stack<T, Container>& x, const stack<T, Container>& y);
  template<class T, class Container>
  constexpr bool operator>(const stack<T, Container>& x, const stack<T, Container>& y);
  template<class T, class Container>
  constexpr bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y);
  template<class T, class Container>
  constexpr bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
  template<class T, three_way_comparable Container>
  constexpr compare_three_way_result_t<Container> operator<=>(
    const stack<T, Container>& x, const stack<T, Container>& y);

  template<class T, class Container>
  constexpr void swap(stack<T, Container>& x,
                      stack<T, Container>& y) noexcept(noexcept(x.swap(y)));
  template<class T, class Container, class Alloc>
  struct uses_allocator<stack<T, Container>, Alloc>;

  // formatter 对 stack 的特化
  template<class CharT, class T, formattable<CharT> Container>
  struct formatter<stack<T, Container>, CharT>;

  template<class T, class Container>
  constexpr bool enable_nonlocking_formatter_optimization<stack<T, Container>> = false;
}

类模板 std::stack

namespace std {
  template<class T, class Container = deque<T>> class stack
  {
  public:
    using value_type = Container::value_type;
    using reference = Container::reference;
    using const_reference = Container::const_reference;
    using size_type = Container::size_type;
    using container_type = Container;

  protected:
    Container c;

  public:
    constexpr stack()
      : stack(Container())
    {
    }
    constexpr explicit stack(const Container&);
    constexpr explicit stack(Container&&);
    template<class InputIter> constexpr stack(InputIter first, InputIter last);
    template<container-compatible-range<T> R> constexpr stack(from_range_t, R&& rg);
    template<class Alloc> constexpr explicit stack(const Alloc&);
    template<class Alloc> constexpr stack(const Container&, const Alloc&);
    template<class Alloc> constexpr stack(Container&&, const Alloc&);
    template<class Alloc> constexpr stack(const stack&, const Alloc&);
    template<class Alloc> constexpr stack(stack&&, const Alloc&);
    template<class InputIter, class Alloc>
    constexpr stack(InputIter first, InputIter last, const Alloc&);
    template<container-compatible-range<T> R, class Alloc>
    constexpr stack(from_range_t, R&& rg, const Alloc&);

    constexpr bool empty() const { return c.empty(); }
    constexpr size_type size() const { return c.size(); }
    constexpr reference top() { return c.back(); }
    constexpr const_reference top() const { return c.back(); }
    constexpr void push(const value_type& x) { c.push_back(x); }
    constexpr void push(value_type&& x) { c.push_back(std::move(x)); }
    template<container-compatible-range<T> R> constexpr void push_range(R&& rg);
    template<class... Args> constexpr decltype(auto) emplace(Args&&... args)
    {
      return c.emplace_back(std::forward<Args>(args)...);
    }
    constexpr void pop() { c.pop_back(); }
    constexpr void swap(stack& s) noexcept(is_nothrow_swappable_v<Container>)
    {
      using std::swap;
      swap(c, s.c);
    }
  };

  template<class Container>
  stack(Container) -> stack<typename Container::value_type, Container>;

  template<class InputIter>
  stack(InputIter, InputIter) -> stack</*iter-value-type*/<InputIter>>;

  template<ranges::input_range R>
  stack(from_range_t, R&&) -> stack<ranges::range_value_t<R>>;

  template<class Container, class Allocator>
  stack(Container, Allocator) -> stack<typename Container::value_type, Container>;

  template<class InputIter, class Allocator>
  stack(InputIter, InputIter, Allocator)
    -> stack</*iter-value-type*/<InputIter>,
             deque</*iter-value-type*/<InputIter>, Allocator>>;

  template<ranges::input_range R, class Allocator>
  stack(from_range_t, R&&, Allocator)
    -> stack<ranges::range_value_t<R>, deque<ranges::range_value_t<R>, Allocator>>;

  template<class T, class Container, class Alloc>
  struct uses_allocator<stack<T, Container>, Alloc>
    : uses_allocator<Container, Alloc>::type
  {};
}