C++优先级队列中的运算符重载或比较函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13790276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Operator overload or comparison function in C++ priority queue
提问by Bart?omiej Sza?ach
I am writing a program in C++ and I would like to define priority queue of one of my class. I need it to compare objects by one of the class member variable. I have used an operator< overload, but I know there is a second way to achieve this goal - special function defined with queue definition. Which way is better, more esthetic and more efficient? And how to write such a function?
我正在用 C++ 编写一个程序,我想定义我的一个班级的优先级队列。我需要它通过类成员变量之一来比较对象。我使用了 operator< 重载,但我知道还有第二种方法可以实现此目标 - 使用队列定义定义的特殊函数。哪种方式更好、更美观、更高效?以及如何编写这样的函数?
I have done it as follows:
我做了如下:
#include <iostream>
#include <queue>
using namespace std;
class Human {
public:
string name;
int age;
Human(string name, int age);
};
Human::Human(string name, int age) : name(name), age(age) {}
bool operator<(Human a, Human b) {return a.age < b.age ? true : false;}
int main() {
Human p1("Child",5);
Human p2("Grandfather",70);
Human p3("Older son",20);
Human p4("Father",40);
Human p5("Younger son",10);
priority_queue<Human> Q;
Q.push(p1);
Q.push(p2);
Q.push(p3);
Q.push(p4);
Q.push(p5);
while(!Q.empty()) {
cout << "Name: " << Q.top().name << ", age: " << Q.top().age << endl;
Q.pop();
}
return 0;
}
采纳答案by Useless
Style
风格
If your type has an intrinsic, "natural" ordering, it is reasonable to express that with a built-in operator.
如果您的类型具有内在的“自然”排序,则使用内置运算符来表达它是合理的。
If the ordering varies by how you're using the type (eg. you have one collection of Human sorted by age, one by height, one by IQ), then it seems sensible to say the ordering is a property of the container instead of the type.
如果排序因您使用类型的方式而异(例如,您有一个按年龄、一个按身高、一个按智商排序的 Human 集合),那么说排序是容器的属性而不是一个属性似乎是明智的方式。
Implementation
执行
You can write a free function, or use a functor if you need state.
您可以编写一个免费函数,或者如果需要状态,则使用函子。
Note that these are both exactlyas amenable to inlining as a built-in operator, so there is no inherent difference in speed (it's probably harder for the compiler to prove a function pointer is inline-able though, so functors are generally preferred).
请注意,这些都正是为顺应内联作为一个内置的操作,所以在速度(它可能更难编译器来证明一个函数指针是直列能的,所以仿函数通常是优选的)没有固有的差异。
struct OrderByAge
{
bool operator() (Human const &a, Human const &b) { return a.age < b.age; }
};
typedef std::priority_queue<Human, std::vector<Human>, OrderByAge> age_queue;
回答by Jonas_Hess
#include <iostream>
#include <queue>
#include <iomanip>
#include <cstdlib>
using namespace std;
struct DatenWert {
int ordnungsnummer;
};
class DaternWert_Compare {
public:
bool operator()(DatenWert& t1, DatenWert& t2)
{
if (t1.ordnungsnummer < t2.ordnungsnummer) return true;
return false;
}
};
int main(int argc, char** argv) {
priority_queue<DatenWert, vector<DatenWert>, DaternWert_Compare> pq;
DatenWert wert2 = {2};
DatenWert wert1 = {1};
DatenWert wert3 = {3};
pq.push(wert1);
pq.push(wert2);
pq.push(wert3);
while (! pq.empty()) {
DatenWert t2 = pq.top();
cout << setw(3) << t2.ordnungsnummer << " " << setw(3) << endl;
pq.pop();
}
return 0;
}
Result:
结果:
3 2 1
3 2 1
回答by HAL9000
The overloading of the operator is the best way because only performs a comparison instruction, nothing can be more performant, so your solution is optimal.
运算符的重载是最好的方法,因为只执行比较指令,没有什么比这更好的了,所以你的解决方案是最优的。