Standard library header <queue>

From cppreference.com
< cpp‎ | header
 
 
Standard Library headers
Note: a slash '/' in a revision mark means that the header was deprecated and/or removed.
Language Support
Concepts
<concepts> (C++20)
Diagnostics
<system_error> (C++11)
General utilities
<bitset>
<tuple> (C++11)
<optional> (C++17)
<any> (C++17)
<variant> (C++17)
<type_traits> (C++11)
<ratio> (C++11)
<chrono> (C++11)
Strings
<charconv> (C++17)
<format> (C++20)

<cwchar>
<cuchar> (C++11)

Localization
<codecvt> (C++11/17)

Containers
<span> (C++20)
<array> (C++11)
<vector>
<map>
<set>
<queue>
Iterators
<iterator>
Ranges
<ranges> (C++20)
Algorithms
<execution> (C++17)
Numerics
<bit> (C++20)
<numbers> (C++20)
<cfenv> (C++11)
<cmath>
Input/Output
<cstdio>
<cinttypes> (C++11)
<strstream> (C++98/)

Regular expressions
<regex> (C++11)
Filesystem support
<filesystem> (C++17)
Thread support
<thread> (C++11)
<atomic> (C++11)
<mutex> (C++11)
<shared_mutex> (C++14)
<condition_variable> (C++11)  
<future> (C++11)
<stop_token> (C++20)
<semaphore> (C++20)

<latch> (C++20)
<barrier> (C++20)

C compatibility
<cstdbool> (C++11/17/20)
<ccomplex> (C++11/17/20)
<ctgmath> (C++11/17/20)
<cstdalign> (C++11/17/20)
<ciso646> (until C++20)
<stdatomic.h>
 

This header is part of the containers library.

Includes

(C++20)
Three-way comparison operator support
std::initializer_list class template

Classes

adapts a container to provide queue (FIFO data structure)
(class template)
adapts a container to provide priority queue
(class template)
specializes the std::uses_allocator type trait
(class template specialization)
specializes the std::uses_allocator type trait
(class template specialization)

Functions

lexicographically compares the values in the queue
(function template)
specializes the std::swap algorithm
(function template)
specializes the std::swap algorithm
(function template)

Synopsis

#include <compare>
#include <initializer_list>
 
namespace std {
  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>;
 
  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>;
}

Class template 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 InputIt> queue(InputIt first, InputIt last);
    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 InputIt, class Alloc>
      queue(InputIt first, InputIt last, const Alloc&);
 
    [[nodiscard]] 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<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 InputIt>
    queue(InputIt, InputIt) -> queue</*iter-value-type*/<InputIt>>;
 
  template<class Container, class Allocator>
    queue(Container, Allocator) -> queue<typename Container::value_type, Container>;
 
  template<class InputIt, class Allocator>
    queue(InputIt, InputIt, Allocator)
      -> queue</*iter-value-type*/<InputIt>, deque</*iter-value-type*/<InputIt>,
               Allocator>>;
 
  template<class T, class Container, class Alloc>
    struct uses_allocator<queue<T, Container>, Alloc>
      : uses_allocator<Container, Alloc>::type { };
}

Class template 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 InputIt>
      priority_queue(InputIt first, InputIt last, const Compare& x = Compare());
    template<class InputIt>
      priority_queue(InputIt first, InputIt last, const Compare& x,
                     const Container&);
    template<class InputIt>
      priority_queue(InputIt first, InputIt last, const Compare& x,
                     Container&&);
    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 InputIt, class Alloc>
      priority_queue(InputIt, InputIt, const Alloc&);
    template<class InputIt, class Alloc>
      priority_queue(InputIt, InputIt, const Compare&, const Alloc&);
    template<class InputIt, class Alloc>
      priority_queue(InputIt, InputIt, const Compare&, const Container&,
                     const Alloc&);
    template<class InputIt, class Alloc>
      priority_queue(InputIt, InputIt, const Compare&, Container&&, const Alloc&);
 
    [[nodiscard]] 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<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 InputIt,
           class Compare = less</*iter-value-type*/<InputIt>>,
           class Container = vector</*iter-value-type*/<InputIt>>>
    priority_queue(InputIt, InputIt, Compare = Compare(), Container = Container())
      -> priority_queue</*iter-value-type*/<InputIt>, Container, Compare>;
 
  template<class Compare, class Container, class Allocator>
    priority_queue(Compare, Container, Allocator)
      -> priority_queue<typename Container::value_type, Container, Compare>;
 
  template<class InputIt, class Allocator>
    priority_queue(InputIt, InputIt, Allocator)
      -> priority_queue</*iter-value-type*/<InputIt>,
                        vector</*iter-value-type*/<InputIt>, Allocator>,
                        less</*iter-value-type*/<InputIt>>>;
 
  template<class InputIt, class Compare, class Allocator>
    priority_queue(InputIt, InputIt, Compare, Allocator)
      -> priority_queue</*iter-value-type*/<InputIt>,
                        vector</*iter-value-type*/<InputIt>, Allocator>, Compare>;
 
  template<class InputIt, class Compare, class Container, class Allocator>
    priority_queue(InputIt, InputIt, Compare, Container, Allocator)
      -> priority_queue<typename Container::value_type, Container, Compare>;
 
  // 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 { };
}