c++11 standard template (STL) (std::priority_queue) (5)

Adapt a container to provide a priority queue
std::priority_queue
Defined in the header file <queue>
template<

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

> class priority_queue;

priority_queue is a container adapter that provides constant-time (default) maximum element lookup, logarithmic cost insertion and extraction.

The order can be Comparechanged , eg with std::greater<T> will cause the smallest element to appear as top().

priority_queueWorks similarly to managing the heap in some random-access container, with the advantage that it is impossible to suddenly invalidate the heap.

template parameter

T - The stored element type. The behavior is undefined if Tand Container::value_typeis not the same type. (since C++17)
Container - The underlying container type used to store elements. A container must meet the requirements of a sequence container (SequenceContainer), and its iterator must meet the requirements of a legacy random access iterator (LegacyRandomAccessIterator). Additionally, it must provide the following functions with the usual semantics:
  • front()
  • push_back()
  • pop_back()

The standard containers std::vector and std::deque meet these requirements.

Compare - Provides a strict and weakly ordered comparison (Compare) type.

Note that the Compare parameter is defined such that it returns true if its first argument precedes its second argument in weak ordering. But because priority_queue outputs the largest element first, the "first come" element is actually output last. That is, the head of the queue contains the "last" element in the weak ordering imposed by Compare.


member object

Container c

underlying container
(protected member object)

Compare comp

comparison function object
(protected member object)

non-member function

specialization of the std::swap algorithm

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 );
(before 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 */);
(since C++17)

Specializes the std::swap algorithm for std::priority_queue. lhsSwaps rhsthe contents of and . Call lhs. swap(rhs).

This overload only participates in overload resolution if both std::is_swappable<Container>::value and std::is_swappable<Compare>::value are true.

(since C++17)

parameter

lhs, rhs - container to swap content

return value

(none)

the complexity

Same as swapping the underlying container.

Auxiliary class

specialize the std::uses_allocator type trait

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 { };
(since C++11)

Provides a transparent specialization of the std::uses_allocator type trait for std::priority_queue: the container adapter uses the allocator if and only if the underlying container does.

Inherited from std::integral_constant

member constant

value

[static]

true
(public static member constants)

member function

operator bool

Convert object to bool, return value
(public member function)

operator()

(C++14)

return value
(public member function)

member type

type definition
value_type bool
type std::integral_constant<bool, value>

call example

#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;
}

 output

 

Guess you like

Origin blog.csdn.net/qq_40788199/article/details/130396675