C ++中优先级队列的结构排序条件

时间:2018-06-19 17:17:47

标签: c++ stl std priority-queue

我想知道是否可以使用相同的结构但使用不同的排序标准来排序两个不同的priority_queue(stl)。想象一下:

struct stuff {
  int a, b;
}

priority_queue<stuff> a;
priority_queue<stuff> b;

请注意,这不是C ++的功能代码,而只是伪代码。然后,我想知道a和b的顺序是否可能不同,一个在队列开始处具有最大的填充物。a,而另一个在填充物中具有最大的填充物。b。

非常感谢您!

马克

1 个答案:

答案 0 :(得分:2)

std::priority_queue上的文档中有一个示例如何使用lambda的示例,因此只需创建其中两个即可。

struct stuff {
  int a, b;
};

void foo() 
{
    auto cmpBiggerA = []( const stuff &s1, const stuff &s2 ) { return s1.a > s2.a; };
    auto cmpBiggerB = []( const stuff &s1, const stuff &s2 ) { return s1.b > s2.b; };

    priority_queue<stuff,std::vector<stuff>,decltype(cmpBiggerA)> a( cmpBiggerA );
    priority_queue<stuff,std::vector<stuff>,decltype(cmpBiggerB)> b( cmpBiggerB );
}