C++ 如何在优先级队列中存储3个整数?

C++ 如何在优先级队列中存储3个整数?,c++,data-structures,priority-queue,C++,Data Structures,Priority Queue,我想在优先级队列中存储3个整数。我知道如何存储2个整数。 我用对存储2个整数 我的代码 priority_queue<pair<int,int> , vector<pair<int,int> > , greater<pair<int,int> > > pq; pq.push(make_pair(5,6)); 优先级队列pq; pq.推送(制造成对(5,6)); 但我不知道如何存储3个整数。我需要帮助 对不起,我的英语不好

我想在优先级队列中存储3个整数。我知道如何存储2个整数。 我用
对存储2个整数

我的代码

priority_queue<pair<int,int> , vector<pair<int,int> > , greater<pair<int,int> > > pq;
pq.push(make_pair(5,6));
优先级队列pq;
pq.推送(制造成对(5,6));
但我不知道如何存储3个整数。我需要帮助

对不起,我的英语不好。

你可以用

#包括“boost/tuple/tuple.hpp”
#包括
#包括
#包括
typedef boost::元组三元组;
我的班级更大{
公众:
布尔运算符()
{
返回arg1.get()>arg2.get();
返回false;
}
};
typedef std::优先级_队列
我的优先权队列;
int main()
{
我的优先级队列是三倍;
三元组。push(boost::make_tuple(1,2,3));
三元组。push(boost::make_tuple(10,20,30));
三元组。push(boost::make_tuple(5,10,15));
三元组。push(boost::make_tuple(15,30,45));
三元组。push(boost::make_tuple(2,4,6));

std::cout最简单的方法是创建一个
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;
}
#包括
使用名称空间std;
结构
{
int m_n1;
int m_n2;
国际货币基金组织3;
S(int n1,int n2,int n3):m_n1(n1),m_n2(n2),m_n3(n3)
{
}

布尔运算符或简单的方法:
std::pair

或者
运算符或者创建结构,但我不能推送整数。我如何推送整数?因为优先级队列对元素进行排序,使最大的总是第一个。在我的比较运算符
运算符@Elmi中:您可能需要进行词典比较。这是comp的类型我想你可以用
pair
来存储3个整数,但那太傻了。:P-1
triplet.second.second
是访问三元组的第三个元素的一种非常丑陋和复杂的方式。这种解决方案根本不能很好地扩展。它不应该扩展,而且无论如何也不会更小可扩展的其他解决方案(不包括boost/tr1 tuple)。而且…它是最快的一个:)(作为编写代码)。关于丑陋的…它只是在做这项工作。我的观点是程序员,这比为某些应用选择的解决方案要好得多:例如竞争性编程。
#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;
}