如何在priority_queue中存储3个整数?

Elm*_*dov 5 c++ priority-queue data-structures

我想在priority_queue中存储3个整数.我知道如何存储2个整数.我存储了2个整数pair<int,int>

我的代码

priority_queue<pair<int,int> , vector<pair<int,int> > , greater<pair<int,int> > > pq;
pq.push(make_pair(5,6));
Run Code Online (Sandbox Code Playgroud)

但我不知道如何存储3个整数.我需要帮助.

对不起我的英语不好.

Nav*_*een 6

最简单的是创建一个struct逻辑绑定所有整数并创建该struct对象的优先级队列.

编辑 示例代码:

#include <queue>
using namespace std;
struct S
{
    int m_n1;
    int m_n2;
    int m_n3;

    S(int n1, int n2, int n3) : m_n1(n1), m_n2(n2), m_n3(n3)
    {
    }

    bool operator<(const struct S& other) const
    {
        //Your priority logic goes here
        return m_n1 < other.m_n1;
    }
};

int main()
{
    priority_queue<S> pq;

    //Add the elements to the queue
    pq.push(S(1,2,3));
    pq.push(S(4,2,3));
    pq.push(S(2,2,3));

    //This element will be S(4,2,3)
    S s1 = pq.top();
    pq.pop();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)


cpr*_*mer 5

或简单的方法: std::pair<int,std::pair<int,int>>

  • cprogrammer,这比为某些应用程序选择的解决方案要好得多:例如竞争性编程。 (3认同)