首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::priority_queue::priority_queue

(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)

从各种数据源构造容器适配器的新底层容器。

1%29复制构造底层容器。c带着...的内容cont.复制-构造比较函子comp带着...的内容compare.电话std::make_heap(c.begin(), c.end(), comp)这也是默认构造函数。%28直到C++11%29

2%29移动-构造底层容器c带着std::move(cont).复制-构造比较函子comp带着...的内容compare.电话std::make_heap(c.begin(), c.end(), comp)这也是默认构造函数。%28自C++11%29

3%29复制构造函数。的内容复制构造适配器。other.c.比较函子是用std::move(other.comp).%28隐式声明%29

4%29移动构造函数。适配器是用std::move(other.c).比较函子是用std::move(other.comp).%28隐式声明%29

5-10%29以下构造函数仅在std::uses_allocator<container_type, Alloc>::value==true,也就是说,如果底层容器是所有标准库容器%29的分配器感知容器%28true。

5%29使用alloc作为分配器。有效呼叫c(alloc)...comp是值初始化的。

6%29使用alloc作为分配器。有效呼叫c(alloc).复制构造compcompare...

7%29构造底层容器,其内容为cont和使用alloc作为分配器,就像c(cont, alloc).复制构造compcompare.然后打电话std::make_heap(c.begin(), c.end(), comp)...

8%29构造底层容器,其内容为cont使用移动语义alloc作为分配器,就像c(std::move(cont), alloc).复制构造compcompare.然后打电话std::make_heap(c.begin(), c.end(), comp)...

9%29构造适配器,其内容为other.c和使用alloc作为分配器。有效呼叫c(other.c, alloc).复制构造compother.comp...

10%29构造适配器,其内容为other在使用时使用移动语义alloc作为分配器。有效呼叫c(std::move(other.c), alloc).移动-构造compother.comp...

11%29拷贝构造ccontcompcompare.然后打电话c.insert(c.end(), first, last);,然后打电话std::make_heap(c.begin(), c.end(), comp);...

12%29移动-构造cstd::move(cont)compstd::move(compare).然后打电话c.insert(c.end(), first, last);,然后打电话std::make_heap(c.begin(), c.end(), comp);...

参数

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

类型要求

-分配器必须符合分配器的要求。

-货柜必须符合货柜的规定。构造函数%285-10%29只在容器满足分配程序-仓库容器的要求时才定义。

-输入必须符合输入器的要求。

复杂性

1,3%29 O%28N%29比较,其中N为cont.size()...

此外,O%28N%29调用value_type,其中N是cont.size()...

2%29O%28N%29比较,其中N为cont.size()...

4-6%29常数。

7%29O%28N%29比较,其中N为cont.size()...

此外,O%28N%29调用value_type,其中N是cont.size()...

8%29O%28N%29比较,其中N为cont.size()...

9%29线性other...

10%29常数。

11%29O%28N%29比较,其中N为cont.size()+std::distance(first, last)...

此外,O%28N%29调用value_type,其中N是cont.size()...

12%29O%28N%29比较,其中N为cont.size()+std::distance(first, last)...

二次

代码语言:javascript
复制
#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';
}

二次

产出:

二次

代码语言:javascript
复制
1
1
5

二次

使用自定义比较器的示例

二次

代码语言:javascript
复制
#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>;
 
int main()
{
    auto my_comp =
        [](const my_pair_t& e1, const my_pair_t& e2) 
        { return e1.first > e2.first; };
    std::priority_queue<my_pair_t,
                        my_container_t,
                        decltype(my_comp)> queue(my_comp);
    queue.push(std::make_pair(5, true));
    queue.push(std::make_pair(3, false));
    queue.push(std::make_pair(7, true));
    std::cout << std::boolalpha;
    while(!queue.empty()) 
    {
        const auto& p = queue.top();
        std::cout << p.first << " " << p.second << "\n";
        queue.pop();
    }
}

二次

产出:

二次

代码语言:javascript
复制
3 false
5 true
7 true

二次

另见

operator=

assigns values to the container adaptor (public member function)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券