命名空间
变体
操作

标准库头文件 <stack>

来自 cppreference.com
< cpp‎ | header
 
 
标准库头文件
语言支持
概念
<concepts> (C++20)
诊断
<system_error> (C++11)

内存管理
<memory_resource> (C++17)  
元编程
<type_traits> (C++11)
<ratio> (C++11)
通用实用程序
<utility>
<tuple> (C++11)
<optional> (C++17)
<variant> (C++17)
<any> (C++17)
<debugging> (C++26)
<expected> (C++23)
<bitset>
<charconv> (C++17)
<format> (C++20)
<bit> (C++20)

字符串
<cuchar> (C++11)

容器
<unordered_set> (C++11)
<queue>
<stack>
<flat_map> (C++23)
<flat_set> (C++23)
<span> (C++20)
<mdspan> (C++23)
<inplace_vector> (C++26)
迭代器
<iterator>
范围
<ranges> (C++20)
<generator> (C++23)
算法
数值
<cfenv> (C++11)
<complex>
<cmath>
<linalg> (C++26)
<numbers> (C++20)

时间
<chrono> (C++11)
本地化
<codecvt> (C++11/17/26*)
<text_encoding> (C++26)
输入/输出
<filesystem> (C++17)
<cstdio>
<cinttypes> (C++11)
<strstream> (C++98/26*)
正则表达式
<regex> (C++11)
并发支持
<stop_token> (C++20)
<thread> (C++11)
<atomic> (C++11)
<rcu> (C++26)
<stdatomic.h> (C++23)
<mutex> (C++11)
<shared_mutex> (C++14)

<condition_variable> (C++11)  
<semaphore> (C++20)
<latch> (C++20)

<barrier> (C++20)
<future> (C++11)
<hazard_pointer> (C++26)

C 兼容性
<cstdbool> (C++11/17/20*)  
<ccomplex> (C++11/17/20*)
<ctgmath> (C++11/17/20*)

<cstdalign> (C++11/17/20*)

<ciso646> (直到 C++20)

 

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

内容

包含

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

使容器适应以提供堆栈(后进先出数据结构)
(类模板) [edit]
专门针对 std::uses_allocator 类型特征
(类模板专门化) [edit]

函数

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

[编辑] 概要

#include <compare>
#include <initializer_list>
 
namespace std {
  // class template stack
  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>;
 
  // formatter specialization for stack
  template<class CharT, class T, formattable<CharT> Container>
  struct formatter<stack<T, Container>, CharT>;
}

[编辑] 类模板 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 InputIter>
    stack(InputIter first, InputIter last);
    template<container-compatible-range<T> R>
    stack(from_range_t, R&& rg);
    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 InputIter, class Alloc>
    stack(InputIter first, InputIter last, const Alloc&);
    template<container-compatible-range<T> R, class Alloc>
    stack(from_range_t, R&& rg, const Alloc&);
 
    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<container-compatible-range<T> R>
    void push_range(R&& rg);
    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 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
  {};
}