📜  C++ 中集合的优先级队列及示例

📅  最后修改于: 2022-05-13 01:55:50.937000             🧑  作者: Mango

C++ 中集合的优先级队列及示例

优先队列

优先级队列是一种容器适配器,专门设计为队列的第一个元素是队列中所有元素中最大的,并且元素是非递增顺序的(因此我们可以看到队列的每个元素都有一个优先级{固定命令})。

与优先队列一起使用的函数:

  • empty():返回队列是否为空。
  • size():返回队列的大小。
  • top():返回对队列最顶层元素的引用。
  • pop():删除队列的第一个元素。

集合是一种关联容器,其中每个元素都必须是唯一的,因为元素的值标识了它。元素的值一旦添加到集合中就不能修改,尽管可以删除和添加该元素的修改值。

与集合一起使用的函数:

  • size():返回集合中元素的数量。
  • insert(const x):向集合中添加一个新元素“x”。

集合的优先级队列对于设计复杂的数据结构非常有用。

句法 :

priority_queue> priorityQueue 
This stores set as an element in the max-heap priority queue

下面是set的最大堆优先级队列的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to print priority
// queue contents
void print(priority_queue > priorityQueue)
{
    while (!priorityQueue.empty()) {
        // Each element of the priority
        // queue is a set itself
        set st = priorityQueue.top();
 
        cout << "[ ";
 
        // Print the set elements
        for (auto element : st)
            cout << element << ' ';
        cout << ']';
        cout << '\n';
 
        // Pop out the topmost set
        priorityQueue.pop();
    }
}
 
// Driver code
int main()
{
    // Declaring a max-heap priority
    // queue
    priority_queue > priorityQueue;
 
    // Declaring a set of integers
    set set1;
 
    // Inserting into the set
    set1.insert(10);
    set1.insert(1);
    set1.insert(2);
    set1.insert(5);
 
    // Push the set into priority
    // queue
    priorityQueue.push(set1);
 
    // Declaring another set
    set set2;
 
    // Inserting into the set
    set2.insert(2);
    set2.insert(7);
    set2.insert(12);
    set2.insert(1);
 
    // Push the set into priority queue
    priorityQueue.push(set2);
 
    // Declaring another set
    set set3;
 
    // Inserting into the set
    set3.insert(4);
    set3.insert(7);
    set3.insert(12);
    set3.insert(13);
 
    // Push the set into priority queue
    priorityQueue.push(set3);
 
    // Print the priority queue
    print(priorityQueue);
 
    return 0;
}


C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to print priority
// queue contents
void print(priority_queue,
                          vector >,
                          greater > >
               priorityQueue)
{
    while (!priorityQueue.empty()) {
        // Each element of the priority
        // queue is a set itself
        set st = priorityQueue.top();
 
        cout << "[ ";
 
        // Print the set elements
        for (auto element : st)
            cout << element << ' ';
 
        cout << ']';
        cout << '\n';
 
        // Pop out the topmost set
        priorityQueue.pop();
    }
}
 
// Driver code
int main()
{
    // Declaring a min-heap
    // priority queue
    priority_queue,
                   vector >,
                   greater > >
        priorityQueue;
 
    // Declaring a set of integers
    set set1;
 
    // Inserting into the set
    set1.insert(10);
    set1.insert(1);
    set1.insert(2);
    set1.insert(5);
 
    // Push the set into priority
    // queue
    priorityQueue.push(set1);
 
    // Declaring another set
    set set2;
 
    // Inserting into the set
    set2.insert(2);
    set2.insert(7);
    set2.insert(12);
    set2.insert(1);
 
    // Push the set into priority
    // queue
    priorityQueue.push(set2);
 
    // Declaring another set
    set set3;
 
    // Inserting into the set
    set3.insert(4);
    set3.insert(7);
    set3.insert(12);
    set3.insert(13);
 
    // Push the set into priority
    // queue
    priorityQueue.push(set3);
 
    // Print the priority queue
    print(priorityQueue);
 
    return 0;
}



输出
[ 4 7 12 13 ]
[ 1 2 7 12 ]
[ 1 2 5 10 ]

默认情况下,优先级队列是一个最大堆,因此在内部优先级队列内部的两个集合中,具有较大第一个元素的集合是最顶部的元素。如果第一个元素相等,则比较集合的第二个值,依此类推。

句法:

priority_queue, vector>, greater>> priorityQueue 
This stores set as an element in the min-heap priority queue

下面是set的最小堆优先级队列的实现:

C++

// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to print priority
// queue contents
void print(priority_queue,
                          vector >,
                          greater > >
               priorityQueue)
{
    while (!priorityQueue.empty()) {
        // Each element of the priority
        // queue is a set itself
        set st = priorityQueue.top();
 
        cout << "[ ";
 
        // Print the set elements
        for (auto element : st)
            cout << element << ' ';
 
        cout << ']';
        cout << '\n';
 
        // Pop out the topmost set
        priorityQueue.pop();
    }
}
 
// Driver code
int main()
{
    // Declaring a min-heap
    // priority queue
    priority_queue,
                   vector >,
                   greater > >
        priorityQueue;
 
    // Declaring a set of integers
    set set1;
 
    // Inserting into the set
    set1.insert(10);
    set1.insert(1);
    set1.insert(2);
    set1.insert(5);
 
    // Push the set into priority
    // queue
    priorityQueue.push(set1);
 
    // Declaring another set
    set set2;
 
    // Inserting into the set
    set2.insert(2);
    set2.insert(7);
    set2.insert(12);
    set2.insert(1);
 
    // Push the set into priority
    // queue
    priorityQueue.push(set2);
 
    // Declaring another set
    set set3;
 
    // Inserting into the set
    set3.insert(4);
    set3.insert(7);
    set3.insert(12);
    set3.insert(13);
 
    // Push the set into priority
    // queue
    priorityQueue.push(set3);
 
    // Print the priority queue
    print(priorityQueue);
 
    return 0;
}


输出
[ 1 2 5 10 ]
[ 1 2 7 12 ]
[ 4 7 12 13 ]

对于最小堆优先级队列内部的两个集合,具有较小第一个元素的集合是最顶部的元素。如果第一个元素相等,则比较集合的第二个值,依此类推。