C++ 如何对对象使用优先级队列STL?
我想在优先级队列中存储Person类的对象C++ 如何对对象使用优先级队列STL?,c++,stl,C++,Stl,我想在优先级队列中存储Person类的对象 class Person { public: int age; }; 优先级队列 我想我需要为比较定义一个类,但我不确定 还有,我们写的时候, priority_queue< Person, vector<Person>, ??? > 优先级\u队列 更大的工作原理是什么?在本例中,您需要为队列中存储的类型Person提供有效的严格弱排序比较。默认情况下使用std::less,它解析为与操作符等价的内容,您可以编
class Person
{
public:
int age;
};
优先级队列
我想我需要为比较定义一个类,但我不确定
还有,我们写的时候,
priority_queue< Person, vector<Person>, ??? >
优先级\u队列
更大的工作原理是什么?在本例中,您需要为队列中存储的类型
Person
提供有效的严格弱排序比较。默认情况下使用std::less
,它解析为与操作符等价的内容,您可以编写一个比较器类,例如:
std::priority_queue<Person, std::vector<Person>, LessThanByAge> pq;
结构比较{
布尔运算符()(人员常量和p1,人员常量和p2){
//如果“p1”在“p2”之前排序,则返回“true”,例如:
返回p1.age
并将其用作比较参数:
struct CompareAge {
bool operator()(Person const & p1, Person const & p2) {
// return "true" if "p1" is ordered before "p2", for example:
return p1.age < p2.age;
}
};
优先级队列
使用
great
与默认的less
顺序相反,这意味着队列将为您提供最低的值,而不是最高的值。这段代码可能会有所帮助
priority_queue<Person, vector<Person>, CompareAge>
优先级队列是一种抽象数据类型,它捕获了容器的概念,容器的元素具有附加的“优先级”。优先级最高的元素总是出现在队列的前面。如果删除该元素,则下一个优先级最高的元素将前进到前面
C++标准库定义类模板PrimyIySype队列,操作如下:
推送:将元素插入优先级队列class Person
{
public:
int age;
};
top:从优先级队列返回(不删除)优先级最高的元素
class Person
{
public:
int age;
};
pop:从优先级队列中删除最高优先级的元素
class Person
{
public:
int age;
};
size:返回优先级队列中的元素数
class Person
{
public:
int age;
};
empty:根据优先级队列是否为空返回true或false
以下代码段显示了如何构造两个优先级队列,一个可以包含整数,另一个可以包含字符串:
22 prashantonly
22 prashant
23 prashantandsoon..
因为队列遵循优先级规则,所以字符串从最高优先级打印到最低优先级
有时需要创建一个优先级队列来包含用户定义的对象。在这种情况下,优先级队列需要知道用于确定哪些对象具有最高优先级的比较标准。这是通过属于重载运算符()的类的函数对象来实现的。重载()充当<以确定优先级。例如,假设我们想要创建一个优先级队列来存储时间对象。时间对象有三个字段:小时、分钟、秒:
the quick
the lazy dog
jumped over
fox
如果我们希望最早的时间具有最高优先级,我们将重新定义CompareTime,如下所示:
struct LessThanByAge
{
bool operator()(const Person& lhs, const Person& rhs) const
{
return lhs.age < rhs.age;
}
};
5 16 13
5 14 20
3 2 40
3 2 26
类比较时间{
公众:
bool运算符()(Time&t1,Time&t2)//如果t2早于t1,则t2的优先级高于t1
{
如果(t2.h
我们可以定义用户定义的比较器:。下面的代码可能会对您有所帮助
代码段:
#包括
使用名称空间std;
建筑工人
{
字符串名;
int优先级;
};
类比较器
{
公众:
布尔运算符()(常数人和a、常数人和b)
{
返回a.priorityarr[i]。名称>>arr[i]。优先级;
质量推送(arr[i]);
}
而(!pq.empty())
{
虽然这个答案是正确的,但我不喜欢使用operator@BjörnPollex同意。我在添加一些东西。在一个只有一个数据成员的类中,运算符可能是有意义的。值得注意的是:实现bool YourClass::operator谢谢你的回答。我可以重载'@user2441151',是的,你可以,但你必须小心ul的逻辑。它必须实现严格的弱排序。数据成员越多,就越容易出错。除非您使用,在这种情况下,它非常简单。可以传递“comparator对象”而不是comparator类?(为了参数化它并获得更大的灵活性)@castarco是的,您可以将特定的comparator对象作为构造函数参数传递。这是一种比最上面的答案更可取的方法。这不仅是因为它实现了更高级别的比较(可以轻松地用其他语言、更低级别和更高级别进行传输),但也因为它会产生更多可重用的代码。类似的帖子,如果可以的话,我有一个问题。优先级。\u queue pq;。为什么第二个参数vector是必需的?我在许多代码片段中看到过它,但我不理解它。哦,不……狐狸不是棕色的,而非棕色的狐狸跳过了懒狗。:-(不应该pq.front()在第一个代码段中是pq.top()吗?
the quick
the lazy dog
jumped over
fox
struct Time {
int h;
int m;
int s;
};
class CompareTime {
public:
bool operator()(Time& t1, Time& t2) // Returns true if t1 is earlier than t2
{
if (t1.h < t2.h) return true;
if (t1.h == t2.h && t1.m < t2.m) return true;
if (t1.h == t2.h && t1.m == t2.m && t1.s < t2.s) return true;
return false;
}
}
priority_queue<Time, vector<Time>, CompareTime> pq;
Here is a complete program:
#include <iostream>
#include <queue>
#include <iomanip>
using namespace std;
struct Time {
int h; // >= 0
int m; // 0-59
int s; // 0-59
};
class CompareTime {
public:
bool operator()(Time& t1, Time& t2)
{
if (t1.h < t2.h) return true;
if (t1.h == t2.h && t1.m < t2.m) return true;
if (t1.h == t2.h && t1.m == t2.m && t1.s < t2.s) return true;
return false;
}
};
int main()
{
priority_queue<Time, vector<Time>, CompareTime> pq;
// Array of 4 time objects:
Time t[4] = { {3, 2, 40}, {3, 2, 26}, {5, 16, 13}, {5, 14, 20}};
for (int i = 0; i < 4; ++i)
pq.push(t[i]);
while (! pq.empty()) {
Time t2 = pq.top();
cout << setw(3) << t2.h << " " << setw(3) << t2.m << " " <<
setw(3) << t2.s << endl;
pq.pop();
}
return 0;
}
5 16 13
5 14 20
3 2 40
3 2 26
class CompareTime {
public:
bool operator()(Time& t1, Time& t2) // t2 has highest prio than t1 if t2 is earlier than t1
{
if (t2.h < t1.h) return true;
if (t2.h == t1.h && t2.m < t1.m) return true;
if (t2.h == t1.h && t2.m == t1.m && t2.s < t1.s) return true;
return false;
}
};
#include<bits/stdc++.h>
using namespace std;
struct man
{
string name;
int priority;
};
class comparator
{
public:
bool operator()(const man& a, const man& b)
{
return a.priority<b.priority;
}
};
int main()
{
man arr[5];
priority_queue<man, vector<man>, comparator> pq;
for(int i=0; i<3; i++)
{
cin>>arr[i].name>>arr[i].priority;
pq.push(arr[i]);
}
while (!pq.empty())
{
cout<<pq.top().name<<" "<<pq.top().priority;
pq.pop();
cout<<endl;
}
return 0;
}