【发布时间】:2011-03-01 16:31:54
【问题描述】:
我有一个包含几个数字字段的类,例如:
class Class1 {
int a;
int b;
int c;
public:
// constructor and so on...
bool operator<(const Class1& other) const;
};
我需要使用此类的对象作为std::map 中的键。因此我实现了operator<。在这里使用operator< 的最简单实现是什么?
编辑:
可以假设<的含义,只要其中任何一个字段不相等,就可以保证唯一性。
编辑 2:
一个简单的实现:
bool Class1::operator<(const Class1& other) const {
if(a < other.a) return true;
if(a > other.a) return false;
if(b < other.b) return true;
if(b > other.b) return false;
if(c < other.c) return true;
if(c > other.c) return false;
return false;
}
这篇文章背后的全部原因是我发现上面的实现太冗长了。应该有更简单的东西。
【问题讨论】:
-
您必须首先确定'
标签: c++ operators operator-overloading operator-keyword