命名空间
变体
操作

标准库头文件 <queue>

来自 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)
三路比较运算符 支持[编辑]
std::initializer_list 类模板[编辑]

将容器改编为提供队列(FIFO 数据结构)
(类模板) [编辑]
将容器改编为提供优先级队列
(类模板) [编辑]
专门化 std::uses_allocator 类型特征
(类模板特化) [编辑]
专门化 std::uses_allocator 类型特征
(类模板特化) [编辑]

函数

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

[编辑] 概要

#include <compare>
#include <initializer_list>
 
namespace std {
  // class template queue
  template<class T, class Container = deque<T>>
  class queue;
 
  template<class T, class Container>
  bool operator==(const queue<T, Container>& x, const queue<T, Container>& y);
  template<class T, class Container>
  bool operator!=(const queue<T, Container>& x, const queue<T, Container>& y);
  template<class T, class Container>
  bool operator<(const queue<T, Container>& x, const queue<T, Container>& y);
  template<class T, class Container>
  bool operator>(const queue<T, Container>& x, const queue<T, Container>& y);
  template<class T, class Container>
  bool operator<=(const queue<T, Container>& x, const queue<T, Container>& y);
  template<class T, class Container>
  bool operator>=(const queue<T, Container>& x, const queue<T, Container>& y);
  template<class T, three_way_comparable Container>
  compare_three_way_result_t<Container> operator<=>(const queue<T, Container>& x,
                                                    const queue<T, Container>& y);
 
  template<class T, class Container>
  void swap(queue<T, Container>& x, queue<T, Container>& y) noexcept(noexcept(x.swap(y)));
  template<class T, class Container, class Alloc>
  struct uses_allocator<queue<T, Container>, Alloc>;
 
  // formatter specialization for queue
  template<class CharT, class T, formattable<CharT> Container>
  struct formatter<queue<T, Container>, CharT>;
 
  // class template priority_queue
  template<class T,
           class Container = vector<T>,
           class Compare   = less<typename Container::value_type>>
  class priority_queue;
 
  template<class T, class Container, class Compare>
  void swap(priority_queue<T, Container, Compare>& x,
            priority_queue<T, Container, Compare>& y) noexcept(noexcept(x.swap(y)));
  template<class T, class Container, class Compare, class Alloc>
  struct uses_allocator<priority_queue<T, Container, Compare>, Alloc>;
 
  // formatter specialization for priority_queue
  template<class CharT, class T, formattable<CharT> Container, class Compare>
  struct formatter<priority_queue<T, Container, Compare>, CharT>;
}

[编辑] 类模板 std::queue

namespace std {
  template<class T, class Container = deque<T>>
  class queue
  {
  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:
    queue()
      : queue(Container())
    {
    }
    explicit queue(const Container&);
    explicit queue(Container&&);
    template<class InputIter>
    queue(InputIter first, InputIter last);
    template<container-compatible-range<T> R>
    queue(from_range_t, R&& rg);
    template<class Alloc>
    explicit queue(const Alloc&);
    template<class Alloc>
    queue(const Container&, const Alloc&);
    template<class Alloc>
    queue(Container&&, const Alloc&);
    template<class Alloc>
    queue(const queue&, const Alloc&);
    template<class Alloc>
    queue(queue&&, const Alloc&);
    template<class InputIter, class Alloc>
    queue(InputIter first, InputIter last, const Alloc&);
    template<container-compatible-range<T> R, class Alloc>
    queue(from_range_t, R&& rg, const Alloc&);
 
    bool empty() const { return c.empty(); }
    size_type size() const { return c.size(); }
    reference front() { return c.front(); }
    const_reference front() const { return c.front(); }
    reference back() { return c.back(); }
    const_reference back() 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_front(); }
    void swap(queue& q) noexcept(is_nothrow_swappable_v<Container>)
    {
      using std::swap;
      swap(c, q.c);
    }
  };
 
  template<class Container>
  queue(Container) -> queue<typename Container::value_type, Container>;
 
  template<class InputIter>
  queue(InputIter, InputIter) -> queue</*iter-value-type*/<InputIter>>;
 
  template<ranges::input_range R>
  queue(from_range_t, R&&) -> queue<ranges::range_value_t<R>>;
 
  template<class Container, class Allocator>
  queue(Container, Allocator) -> queue<typename Container::value_type, Container>;
 
  template<class InputIter, class Allocator>
  queue(InputIter, InputIter, Allocator)
    -> queue</*iter-value-type*/<InputIter>,
             deque</*iter-value-type*/<InputIter>, Allocator>>;
 
  template<ranges::input_range R, class Allocator>
  queue(from_range_t, R&&, Allocator)
    -> queue<ranges::range_value_t<R>, deque<ranges::range_value_t<R>, Allocator>>;
 
  template<class T, class Container, class Alloc>
  struct uses_allocator<queue<T, Container>, Alloc>
    : uses_allocator<Container, Alloc>::type
  {};
}

[编辑] 类模板 std::priority_queue

namespace std {
  template<class T,
           class Container = vector<T>,
           class Compare   = less<typename Container::value_type>>
  class priority_queue
  {
  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;
    using value_compare   = Compare;
 
  protected:
    Container c;
    Compare comp;
 
  public:
    priority_queue()
      : priority_queue(Compare())
    {
    }
    explicit priority_queue(const Compare& x)
      : priority_queue(x, Container())
    {
    }
    priority_queue(const Compare& x, const Container&);
    priority_queue(const Compare& x, Container&&);
    template<class InputIter>
    priority_queue(InputIter first, InputIter last, const Compare& x = Compare());
    template<class InputIter>
    priority_queue(InputIter first, InputIter last, const Compare& x, const Container&);
    template<class InputIter>
    priority_queue(InputIter first, InputIter last, const Compare& x, Container&&);
    template<container-compatible-range<T> R>
    priority_queue(from_range_t, R&& rg, const Compare& x = Compare());
    template<class Alloc>
    explicit priority_queue(const Alloc&);
    template<class Alloc>
    priority_queue(const Compare&, const Alloc&);
    template<class Alloc>
    priority_queue(const Compare&, const Container&, const Alloc&);
    template<class Alloc>
    priority_queue(const Compare&, Container&&, const Alloc&);
    template<class Alloc>
    priority_queue(const priority_queue&, const Alloc&);
    template<class Alloc>
    priority_queue(priority_queue&&, const Alloc&);
    template<class InputIter, class Alloc>
    priority_queue(InputIter, InputIter, const Alloc&);
    template<class InputIter, class Alloc>
    priority_queue(InputIter, InputIter, const Compare&, const Alloc&);
    template<class InputIter, class Alloc>
    priority_queue(InputIter, InputIter, const Compare&, const Container&, const Alloc&);
    template<class InputIter, class Alloc>
    priority_queue(InputIter, InputIter, const Compare&, Container&&, const Alloc&);
    template<container-compatible-range<T> R, class Alloc>
    priority_queue(from_range_t, R&& rg, const Compare&, const Alloc&);
    template<container-compatible-range<T> R, class Alloc>
    priority_queue(from_range_t, R&& rg, const Alloc&);
 
    bool empty() const { return c.empty(); }
    size_type size() const { return c.size(); }
    const_reference top() const { return c.front(); }
    void push(const value_type& x);
    void push(value_type&& x);
    template<container-compatible-range<T> R>
    void push_range(R&& rg);
    template<class... Args>
    void emplace(Args&&... args);
    void pop();
    void swap(priority_queue& q) noexcept(
      is_nothrow_swappable_v<Container>&& is_nothrow_swappable_v<Compare>)
    {
      using std::swap;
      swap(c, q.c);
      swap(comp, q.comp);
    }
  };
 
  template<class Compare, class Container>
  priority_queue(Compare, Container)
    -> priority_queue<typename Container::value_type, Container, Compare>;
 
  template<class InputIter,
           class Compare   = less</*iter-value-type*/<InputIter>>,
           class Container = vector</*iter-value-type*/<InputIter>>>
  priority_queue(InputIter, InputIter, Compare = Compare(), Container = Container())
    -> priority_queue</*iter-value-type*/<InputIter>, Container, Compare>;
 
  template<ranges::input_range R, class Compare = less<ranges::range_value_t<R>>>
  priority_queue(from_range_t, R&&, Compare = Compare())
    -> priority_queue<ranges::range_value_t<R>,
                      vector<ranges::range_value_t<R>>,
                      Compare>;
 
  template<class Compare, class Container, class Allocator>
  priority_queue(Compare, Container, Allocator)
    -> priority_queue<typename Container::value_type, Container, Compare>;
 
  template<class InputIter, class Allocator>
  priority_queue(InputIter, InputIter, Allocator)
    -> priority_queue</*iter-value-type*/<InputIter>,
                      vector</*iter-value-type*/<InputIter>, Allocator>,
                      less</*iter-value-type*/<InputIter>>>;
 
  template<class InputIter, class Compare, class Allocator>
  priority_queue(InputIter, InputIter, Compare, Allocator)
    -> priority_queue</*iter-value-type*/<InputIter>,
                      vector</*iter-value-type*/<InputIter>, Allocator>,
                      Compare>;
 
  template<class InputIter, class Compare, class Container, class Allocator>
  priority_queue(InputIter, InputIter, Compare, Container, Allocator)
    -> priority_queue<typename Container::value_type, Container, Compare>;
 
  template<ranges::input_range R, class Compare, class Allocator>
  priority_queue(from_range_t, R&&, Compare, Allocator)
    -> priority_queue<ranges::range_value_t<R>,
                      vector<ranges::range_value_t<R>, Allocator>,
                      Compare>;
 
  template<ranges::input_range R, class Allocator>
  priority_queue(from_range_t, R&&, Allocator)
    -> priority_queue<ranges::range_value_t<R>,
                      vector<ranges::range_value_t<R>, Allocator>>;
 
  // no equality is provided
 
  template<class T, class Container, class Compare, class Alloc>
  struct uses_allocator<priority_queue<T, Container, Compare>, Alloc>
    : uses_allocator<Container, Alloc>::type
  {};
}