c++11 标准模板(STL)(std::priority_queue)(五)

适配一个容器以提供优先级队列
std::priority_queue
定义于头文件 <queue>
template<

    class T,
    class Container = std::vector<T>,
    class Compare = std::less<typename Container::value_type>

> class priority_queue;

priority_queue 是容器适配器,它提供常数时间的(默认)最大元素查找,对数代价的插入与释出。

可用用户提供的 Compare 更改顺序,例如,用 std::greater<T> 将导致最小元素作为 top() 出现。

priority_queue 工作类似管理某些随机访问容器中的堆,优势是不可能突然把堆非法化。

模板形参

T - 存储的元素类型。若 TContainer::value_type 不是同一类型则行为未定义。 (C++17 起)
Container - 用于存储元素的底层容器类型。容器必须满足序列容器 (SequenceContainer) 的要求,而其迭代器必须满足遗留随机访问迭代器 (LegacyRandomAccessIterator) 的要求。另外,它必须提供拥有通常语义的下列函数:
  • front()
  • push_back()
  • pop_back()

标准容器 std::vector 和 std::deque 满足这些要求。

Compare - 提供严格弱序的比较 (Compare) 类型。

注意 比较 (Compare) 形参的定义,使得若其第一参数在弱序中先于其第二参数则返回 true 。但因为 priority_queue 首先输出最大元素,故“先来”的元素实际上最后输出。即队列头含有按照 比较 (Compare) 所施加弱序的“最后”元素。


成员对象

Container c

底层容器
(受保护成员对象)

Compare comp

比较函数对象
(受保护成员对象)

非成员函数

特化 std::swap 算法

std::swap(std::priority_queue)
template< class T, class Container, class Compare >

void swap( priority_queue<T,Container,Compare>& lhs,

           priority_queue<T,Container,Compare>& rhs );
(C++17 前)
template< class T, class Container, class Compare >

void swap( priority_queue<T,Container,Compare>& lhs,

           priority_queue<T,Container,Compare>& rhs ) noexcept(/* see below */);
(C++17 起)

为 std::priority_queue 特化 std::swap 算法。交换 lhsrhs 的内容。调用 lhs.swap(rhs) 。

此重载仅若 std::is_swappable<Container>::value 与 std::is_swappable<Compare>::value 均为 true 才参与重载决议。

(C++17 起)

参数

lhs, rhs - 要交换内容的容器

返回值

(无)

复杂度

与交换底层容器相同。

辅助类

特化 std::uses_allocator 类型特性

std::uses_allocator<std::priority_queue>
template< class T, class Container, class Compare,class Alloc >

struct uses_allocator<priority_queue<T,Compare,Container>,Alloc> :

    std::uses_allocator<Container, Alloc>::type { };
(C++11 起)

为 std::priority_queue 提供 std::uses_allocator 类型特性的通透特化:容器适配器使用分配器,若且唯若底层容器使用。

继承自 std::integral_constant

成员常量

value

[静态]

true
(公开静态成员常量)

成员函数

operator bool

转换对象为 bool ,返回 value
(公开成员函数)

operator()

(C++14)

返回 value
(公开成员函数)

成员类型

类型 定义
value_type bool
type std::integral_constant<bool, value>

调用示例

#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <queue>
#include <deque>
#include <time.h>

using namespace std;

struct Cell
{
    int x;
    int y;

    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}

    Cell &operator +=(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator +(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator *(const Cell &cell)
    {
        x *= cell.x;
        y *= cell.y;
        return *this;
    }

    Cell &operator ++()
    {
        x += 1;
        y += 1;
        return *this;
    }


    bool operator <(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y < cell.y;
        }
        else
        {
            return x < cell.x;
        }
    }

    bool operator >(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y > cell.y;
        }
        else
        {
            return x > cell.x;
        }
    }

    bool operator ==(const Cell &cell) const
    {
        return x == cell.x && y == cell.y;
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

template<typename _Tp, typename _Sequence = vector<_Tp>,
         typename _Compare  = less<typename _Sequence::value_type> >
void queuePrint(const std::string &name,
                const std::priority_queue<_Tp, vector<_Tp>, _Compare> &queue)
{
    std::cout << name ;
    std::priority_queue<_Tp, vector<_Tp>, _Compare> queuep = queue;
    while (queuep.size() > 0)
    {
        std::cout << queuep.top() << " ";
        queuep.pop();
    }
    std::cout << std::endl;
}

struct Compare
{
    Compare() {}
    bool operator()(const Cell &a, const Cell &b)const
    {
        if (a.x == b.x)
        {
            return a.y < b.y;
        }
        return a.x < b.x;
    }
};

int main()
{
    std::cout << std::boolalpha;

    std::mt19937 g{std::random_device{}()};
    srand((unsigned)time(NULL));

    auto generate = []()
    {
        int n = std::rand() % 10 + 110;
        Cell cell{n, n};
        return cell;
    };

    std::vector<Cell> vector1(6);
    std::generate(vector1.begin(), vector1.end(), generate);
    std::cout << "vector1:  ";
    std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::vector<Cell> vector2(6);
    std::generate(vector2.begin(), vector2.end(), generate);
    std::cout << "vector2:  ";
    std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    //2) 以 cont 的内容复制构造底层容器 c 。
    std::priority_queue<Cell> queue1(std::less<Cell>(), vector1);
    std::priority_queue<Cell> queue2(std::less<Cell>(), vector2);
    queuePrint("queue1:   ", queue1);
    queuePrint("queue2:   ", queue2);

    //为 std::queue 特化 std::swap 算法。交换 lhs 与 rhs 的内容。
    std::cout << "swap before:  " << std::endl;
    queuePrint("queue1:   ", queue1);
    queuePrint("queue2:   ", queue2);
    std::swap(queue1, queue2);
    std::cout << "swap after:  " << std::endl;
    queuePrint("queue1:   ", queue1);
    queuePrint("queue2:   ", queue2);

    return 0;
}

 输出

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/130396675
今日推荐