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

📅  最后修改于: 2021-05-30 09:36:08             🧑  作者: Mango

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

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

  • 按对的第一个元素排序
  • 由对的第二个元素排序

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

在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

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

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

下面是优先级队列的实现:

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等的更多准备工作,请参阅“完整面试准备课程”