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

发布于 2024-11-02 08:18:01 字数 330 浏览 4 评论 0 原文

我想在priority_queue中存储3个整数。我知道如何存储 2 个整数。 我用 pair

我的代码

priority_queue<pair<int,int> , vector<pair<int,int> > , greater<pair<int,int> > > pq;
pq.push(make_pair(5,6));

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

对不起我的英语。

I want to store 3 integer in priority_queue. I know how to store 2 integer.
I store 2 integer with pair<int,int>

my code

priority_queue<pair<int,int> , vector<pair<int,int> > , greater<pair<int,int> > > pq;
pq.push(make_pair(5,6));

but I don't know how can I store 3 integer. I need help.

sorry for my English.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眉目亦如画i 2024-11-09 08:18:01

最简单的是创建一个 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;
}

Simplest would be create a struct which logically binds all the integers and create a priority queue of that struct objects.

EDIT
Sample code:

#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;
}
傾旎 2024-11-09 08:18:01

或者简单的方法:std::pair>

or the easy way: std::pair<int,std::pair<int,int>>

还不是爱你 2024-11-09 08:18:01

您可以使用 Boost::Tuple

#include "boost/tuple/tuple.hpp"
#include <queue>
#include <vector>
#include <iostream>

typedef boost::tuple<int, int, int> triple_t;

class my_greater  {
public:
  bool operator() (const triple_t& arg1, const triple_t& arg2) const
  {
    return arg1.get<0>() > arg2.get<0>();
    return false;
  }
};

typedef std::priority_queue<triple_t, std::vector<triple_t>, my_greater> 
   my_priority_queue;

int main()
{
  my_priority_queue triples;

  triples.push(boost::make_tuple(1,2,3));
  triples.push(boost::make_tuple(10,20,30));
  triples.push(boost::make_tuple(5,10,15));
  triples.push(boost::make_tuple(15,30,45));
  triples.push(boost::make_tuple(2,4,6));

  std::cout << "Result: \n";
  while (!triples.empty()) 
  {
    const triple_t& t = triples.top();
    std::cout << t.get<0>() << ", " << t.get<1>() << ", " << t.get<2>() << std::endl;
    triples.pop();
  }

  return 0;
}

You may use Boost::Tuple

#include "boost/tuple/tuple.hpp"
#include <queue>
#include <vector>
#include <iostream>

typedef boost::tuple<int, int, int> triple_t;

class my_greater  {
public:
  bool operator() (const triple_t& arg1, const triple_t& arg2) const
  {
    return arg1.get<0>() > arg2.get<0>();
    return false;
  }
};

typedef std::priority_queue<triple_t, std::vector<triple_t>, my_greater> 
   my_priority_queue;

int main()
{
  my_priority_queue triples;

  triples.push(boost::make_tuple(1,2,3));
  triples.push(boost::make_tuple(10,20,30));
  triples.push(boost::make_tuple(5,10,15));
  triples.push(boost::make_tuple(15,30,45));
  triples.push(boost::make_tuple(2,4,6));

  std::cout << "Result: \n";
  while (!triples.empty()) 
  {
    const triple_t& t = triples.top();
    std::cout << t.get<0>() << ", " << t.get<1>() << ", " << t.get<2>() << std::endl;
    triples.pop();
  }

  return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文