65,210
社区成员
发帖
与我相关
我的任务
分享
问题:
1.我用的是greater,是不是只需要重载operator > ?
是的
2.如果我需要表达"大于等于",那么priority_queue <Test, vector <Test>, greater>里的"greater"应该换成什么?
greater_equal<Test>
3.如果我需要表达"大于等于",是重载 >= 呢?还是同时重载"> "和 "==" ?
只要重载 >+
4.对于这些比较符,是应该写成成员函数, 还是全局函数?
都可以,不过注意如果写成成员记得要定义为 const 函数
5.如果定义:
priority_queue <Test*, vector <Test*>, greater> myqueue2;
那么对于运算符的重载,参数应该采用下列哪一种?或者是其它的?
a) bool operator XX (const Test* l, const Test* r)
b) bool operator XX (const Test* & l, const Test* & r)
a, b 都不行
自己写比较
#include "iostream"
#include "queue"
#include "functional"
using namespace std;
class Test
{
public :
Test()
{
}
Test(int k) : key(k)
{
}
int key;
};
struct greater2
: public binary_function<Test*, Test*, bool>
{
bool operator()(Test* const& _Left, const Test* const& _Right) const
{
return (_Left->key > _Right->key);
}
};
int main()
{
priority_queue <Test*, vector <Test*>, greater2> myqueue;
myqueue.push(new Test(1));
myqueue.push(new Test(2));
return 0;
}
// declaration in class
bool operator < (Test const & rhs) const;