📌  相关文章
📜  C ++中对优先级队列,按第一个和第二个元素排序

📅  最后修改于: 2021-09-02 06:40:58             🧑  作者: Mango

优先级队列:优先级队列是队列的扩展,其中与优先级相关联的元素和具有更高优先级的元素首先被弹出。

优先级队列可以包含具有各种数据类型的元素,例如整数、整数对、自定义数据类型。但有一点很常见,即有一个元素定义了元素的优先级。
因此,对的优先级队列可以有两种排序方式——

  • 按pair的第一个元素排序
  • 按 pair 的第二个元素排序

按第一个元素排序的优先队列

在 C++ 中,如果元素是成对形式,则默认情况下元素的优先级取决于第一个元素。因此,我们只需要使用对的优先级队列即可。

C++
// C++ implementation of the
// priority queue of pairs
// ordered by the first element
  
#include 
#include 
using namespace std;
  
// Function to print the data of
// the priority queue ordered by first
void showpq(
    priority_queue > g)
{
    // Loop to print the elements
    // until the priority queue
    // is not empty
    while (!g.empty()) {
        cout << g.top().first
             << " " << g.top().second
             << endl;
        g.pop();
    }
    cout << endl;
}
  
// Driver Code
int main()
{
    priority_queue > p1;
  
    // Insertion of elements
    p1.push(make_pair(4, 5));
    p1.push(make_pair(5, 4));
    p1.push(make_pair(1, 6));
    p1.push(make_pair(7, 3));
    p1.push(make_pair(9, 4));
    showpq(p1);
    return 0;
}


C++
// C++ implementation of the
// priority queue in which elements
// are sorted by the second element
  
#include 
#include 
#include 
  
using namespace std;
  
typedef pair pd;
  
// Structure of the condition
// for sorting the pair by its
// second elements
struct myComp {
    constexpr bool operator()(
        pair const& a,
        pair const& b)
        const noexcept
    {
        return a.second < b.second;
    }
};
  
// Function to show the elements
// of the priority queue
void showpq(
    priority_queue, myComp>
        g)
{
    // Loop to print the elements
    // until the priority queue
    // is not empty
    while (!g.empty()) {
        cout << g.top().first
             << " " << g.top().second
             << endl;
        g.pop();
    }
    cout << endl;
}
  
// Driver Code
int main()
{
    priority_queue, myComp> p1;
    p1.push(make_pair(4, 5));
    p1.push(make_pair(5, 4));
    p1.push(make_pair(1, 6));
    p1.push(make_pair(7, 3));
    p1.push(make_pair(9, 4));
  
    // Function Call
    showpq(p1);
    return 0;
}


C++
// C++ implementation of the priority
// queue sorted by the second element
// in the decreasing order
  
#include 
#include 
#include 
  
using namespace std;
  
typedef pair pd;
  
// Structure of the operator
// overloading for comparison
struct myComp {
    constexpr bool operator()(
        pair const& a,
        pair const& b)
        const noexcept
    {
        return a.second > b.second;
    }
};
  
// Function to print the elements
// of the priority queue
void showpq(
    priority_queue, myComp> g)
{
    // Loop to print the elements
    // of the priority queue
    while (!g.empty()) {
        cout << g.top().first
             << " " << g.top().second
             << endl;
        g.pop();
    }
    cout << endl;
}
  
// Driver Code
int main()
{
    priority_queue, myComp> p1;
  
    // Insertion of the elements
    p1.push(make_pair(4, 5));
    p1.push(make_pair(5, 4));
    p1.push(make_pair(1, 6));
    p1.push(make_pair(7, 3));
    p1.push(make_pair(9, 4));
    showpq(p1);
    return 0;
}


输出:
9 4
7 3
5 4
4 5
1 6

注意:如果某些对的第一个元素相同,则将在第二个元素的基础上进行比较。

按第二个元素排序的优先级队列(最大)

这个想法是使用具有运算符重载概念的结构在优先级队列中按其第二个元素对对进行排序。

下面是按第二个元素排序的优先级队列的实现——

C++

// C++ implementation of the
// priority queue in which elements
// are sorted by the second element
  
#include 
#include 
#include 
  
using namespace std;
  
typedef pair pd;
  
// Structure of the condition
// for sorting the pair by its
// second elements
struct myComp {
    constexpr bool operator()(
        pair const& a,
        pair const& b)
        const noexcept
    {
        return a.second < b.second;
    }
};
  
// Function to show the elements
// of the priority queue
void showpq(
    priority_queue, myComp>
        g)
{
    // Loop to print the elements
    // until the priority queue
    // is not empty
    while (!g.empty()) {
        cout << g.top().first
             << " " << g.top().second
             << endl;
        g.pop();
    }
    cout << endl;
}
  
// Driver Code
int main()
{
    priority_queue, myComp> p1;
    p1.push(make_pair(4, 5));
    p1.push(make_pair(5, 4));
    p1.push(make_pair(1, 6));
    p1.push(make_pair(7, 3));
    p1.push(make_pair(9, 4));
  
    // Function Call
    showpq(p1);
    return 0;
}
输出:
1 6
4 5
9 4
5 4
7 3

按第二个元素排序的优先级队列 (Min)

这个想法是使用运算符重载来实现按其第二个元素排序的优先级队列,最小元素在顶部。

下面是优先队列的实现:

C++

// C++ implementation of the priority
// queue sorted by the second element
// in the decreasing order
  
#include 
#include 
#include 
  
using namespace std;
  
typedef pair pd;
  
// Structure of the operator
// overloading for comparison
struct myComp {
    constexpr bool operator()(
        pair const& a,
        pair const& b)
        const noexcept
    {
        return a.second > b.second;
    }
};
  
// Function to print the elements
// of the priority queue
void showpq(
    priority_queue, myComp> g)
{
    // Loop to print the elements
    // of the priority queue
    while (!g.empty()) {
        cout << g.top().first
             << " " << g.top().second
             << endl;
        g.pop();
    }
    cout << endl;
}
  
// Driver Code
int main()
{
    priority_queue, myComp> p1;
  
    // Insertion of the elements
    p1.push(make_pair(4, 5));
    p1.push(make_pair(5, 4));
    p1.push(make_pair(1, 6));
    p1.push(make_pair(7, 3));
    p1.push(make_pair(9, 4));
    showpq(p1);
    return 0;
}
输出:
7 3
5 4
9 4
4 5
1 6
想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解基础加 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程