标准库头文件 <stack>

来自cppreference.com
< cpp‎ | header
 
 
 

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

包含

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

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

函数

按照字典顺序比较 stack 中的值
(函数模板)
特化 std::swap 算法
(函数模板)

概要

#include <compare>
#include <initializer_list>
 
namespace std {
  template<class T, class Container = deque<T>> class stack;
 
  template<class T, class Container>
    bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
  template<class T, class Container>
    bool operator!=(const stack<T, Container>& x, const stack<T, Container>& y);
  template<class T, class Container>
    bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
  template<class T, class Container>
    bool operator> (const stack<T, Container>& x, const stack<T, Container>& y);
  template<class T, class Container>
    bool operator<=(const stack<T, Container>& x, const stack<T, Container>& y);
  template<class T, class Container>
    bool operator>=(const stack<T, Container>& x, const stack<T, Container>& y);
  template<class T, three_way_comparable Container>
    compare_three_way_result_t<Container>
      operator<=>(const stack<T, Container>& x, const stack<T, Container>& y);
 
  template<class T, class Container>
    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>;
}

类模板 std::stack

namespace std {
  template<class T, class Container = deque<T>>
  class stack {
  public:
    using value_type      = typename Container::value_type;
    using reference       = typename Container::reference;
    using const_reference = typename Container::const_reference;
    using size_type       = typename Container::size_type;
    using container_type  = Container;
 
  protected:
    Container c;
 
  public:
    stack() : stack(Container()) {}
    explicit stack(const Container&);
    explicit stack(Container&&);
    template<class InputIt> stack(InputIt first, InputIt last);
    template<class Alloc> explicit stack(const Alloc&);
    template<class Alloc> stack(const Container&, const Alloc&);
    template<class Alloc> stack(Container&&, const Alloc&);
    template<class Alloc> stack(const stack&, const Alloc&);
    template<class Alloc> stack(stack&&, const Alloc&);
    template<class InputIt, class Alloc>
      stack(InputIt first, InputIt last, const Alloc&);
 
    [[nodiscard]] bool empty() const    { return c.empty(); }
    size_type size()  const             { return c.size(); }
    reference         top()             { return c.back(); }
    const_reference   top() const       { return c.back(); }
    void push(const value_type& x)      { c.push_back(x); }
    void push(value_type&& x)           { c.push_back(std::move(x)); }
    template<class... Args>
      decltype(auto) emplace(Args&&... args)
        { return c.emplace_back(std::forward<Args>(args)...); }
    void pop()                          { c.pop_back(); }
    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 InputIt>
    stack(InputIt, InputIt) -> stack</*iter-value-type*/<InputIt>>;
 
  template<class Container, class Allocator>
    stack(Container, Allocator) -> stack<typename Container::value_type, Container>;
 
  template<class InputIt, class Allocator>
    stack(InputIt, InputIt, Allocator)
      -> stack</*iter-value-type*/<InputIt>, deque</*iter-value-type*/<InputIt>,
               Allocator>>;
 
  template<class T, class Container, class Alloc>
    struct uses_allocator<stack<T, Container>, Alloc>
      : uses_allocator<Container, Alloc>::type { };
}