std::priority_queue::priority_queue

From cppreference.com
(1)
explicit priority_queue( const Compare& compare = Compare(),
                         const Container& cont = Container() );
(until C++11)
priority_queue( const Compare& compare, const Container& cont );
(since C++11)
explicit priority_queue( const Compare& compare = Compare(),
                         Container&& cont = Container() );
(2) (since C++11)
priority_queue( const priority_queue& other );
(3)
priority_queue( priority_queue&& other );
(4) (since C++11)
template< class Alloc >
explicit priority_queue( const Alloc& alloc );
(5) (since C++11)
template< class Alloc >
priority_queue( const Compare& compare, const Alloc& alloc );
(6) (since C++11)
template< class Alloc >

priority_queue( const Compare& compare, const Container& cont,

                const Alloc& alloc );
(7) (since C++11)
template< class Alloc >

priority_queue( const Compare& compare, Container&& cont,

                const Alloc& alloc );
(8) (since C++11)
template< class Alloc >
priority_queue( const priority_queue& other, const Alloc& alloc );
(9) (since C++11)
template< class Alloc >
priority_queue( priority_queue&& other, const Alloc& alloc );
(10) (since C++11)
template< class InputIt >

priority_queue( InputIt first, InputIt last,

                const Compare& compare, const Container& cont );
(11) (since C++11)
template< class InputIt >

priority_queue( InputIt first, InputIt last,
                const Compare& compare = Compare(),

                Container&& cont = Container() );
(12) (since C++11)

Constructs new underlying container of the container adaptor from a variety of data sources.

1) Copy-constructs the underlying container c with the contents of cont. Copy-constructs the comparison functor comp with the contents of compare. Calls std::make_heap(c.begin(), c.end(), comp). This is also the default constructor. (until C++11)
2) Move-constructs the underlying container c with std::move(cont). Move-constructs the comparison functor comp with std::move(compare). Calls std::make_heap(c.begin(), c.end(), comp). This is also the default constructor. (since C++11)
3) Copy constructor. The adaptor is copy-constructed with the contents of other.c. The comparison functor is constructed with std::move(other.comp). (implicitly declared)
4) Move constructor. The adaptor is constructed with std::move(other.c).The comparison functor is constructed with std::move(other.comp). (implicitly declared)
5-10) The following constructors are only defined if std::uses_allocator<container_type, Alloc>::value == true, that is, if the underlying container is an allocator-aware container (true for all standard library containers).
5) Constructs the underlying container using alloc as allocator. Effectively calls c(alloc). comp is value-initialized.
6) Constructs the underlying container using alloc as allocator. Effectively calls c(alloc). Copy-constructs comp from compare.
7) Constructs the underlying container with the contents of cont and using alloc as allocator. Effectively calls c(cont, alloc). Copy-constructs comp from compare.
8) Constructs the underlying container with the contents of cont using move semantics while utilising alloc as allocator. Effectively calls c(std::move(cont), alloc). Copy-constructs comp from compare.
9) Constructs the adaptor with the contents of other.c and using alloc as allocator. Effectively calls c(athor.c, alloc). Copy-constructs comp from other.comp.
10) Constructs the adaptor with the contents of other using move semantics while utilising alloc as allocator. Effectively calls c(std::move(other.c), alloc). Move-constructs comp from other.comp.
11) Copy-constructs c from cont and comp from compare. Then calls c.insert(c.end(), first, last);, and then calls std::make_heap(c.begin(), c.end(), comp);.
12) Move-constructs c from std::move(cont) and comp from std::move(compare). Then calls c.insert(c.end(), first, last);, and then calls std::make_heap(c.begin(), c.end(), comp);.

Contents

[edit] Parameters

alloc - allocator to use for all memory allocations of the underlying container
other - another container adaptor to be used as source to initialize the underlying container
cont - container to be used as source to initialize the underlying container
compare - the comparison function object to initialize the underlying comparison functor
first, last - range of elements to initialize with
Type requirements
-
Alloc must meet the requirements of Allocator.
-
Container must meet the requirements of Container. The constructors (5-10) are only defined if Container meets the requirements of AllocatorAwareContainer
-
InputIt must meet the requirements of InputIterator.

[edit] Complexity

1,3) O(N) comparisons, where N is cont.size().
Additionally, O(N) calls to the constructor of value_type, where N is cont.size().
2) O(N) comparisons, where N is cont.size().
4-6) Constant.
7) O(N) comparisons, where N is cont.size().
Additionally, O(N) calls to the constructor of value_type, where N is cont.size().
8) O(N) comparisons, where N is cont.size().
9) Linear in size of other.
10) Constant.
11) O(N) comparisons, where N is cont.size() + std::distance(first, last).
Additionally, O(N) calls to the constructor of value_type, where N is cont.size().
12) O(N) comparisons, where N is cont.size() + std::distance(first, last).

[edit] Example

#include <queue>
#include <vector>
#include <iostream>
#include <functional>
 
int main()
{
    std::priority_queue<int> c1;
    c1.push(5);
    std::cout << c1.size() << '\n';
 
    std::priority_queue<int> c2(c1);
    std::cout << c2.size() << '\n';
 
    std::vector<int> vec={3, 1, 4, 1, 5};
    std::priority_queue<int> c3(std::less<int>(), vec);
    std::cout << c3.size() << '\n';
}

Output:

1
1
5

[edit] Example

Example with custom comparator.

#include <iostream>
#include <queue>
#include <vector>
#include <utility>
 
using my_pair_t= std::pair<size_t,bool>;
 
using my_container_t= std::vector<my_pair_t>;
 
auto my_comp=
     []( const my_pair_t &e1, const my_pair_t &e2 ) 
     { return e1.first > e2.first; };
 
using my_queue_t=
      std::priority_queue<my_pair_t,
                          my_container_t,
                          decltype(my_comp)>;
 
int main()
{
    my_queue_t   que(my_comp);
    que.push( std::make_pair( 5, true ) );
    que.push( std::make_pair( 3, false ) );
    que.push( std::make_pair( 7, true ) );
    std::cout << std::boolalpha;
    while(!que.empty()) 
    {
        auto const &p= que.top();
        std::cout << p.first << " " << p.second << "  \t";
        que.pop();
    }
    std::cout << "\n";
} // main

Output:

3 false         5 true          7 true

[edit] See also

assigns values to the container adaptor
(public member function)