自定义类型作为地图的键 - C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/906244/
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
Custom types as key for a map - C++
提问by Navaneeth K N
I am trying to assign a custom type as a key for std::map. Here is the type which I am using as key.
我正在尝试将自定义类型分配为std::map的键。这是我用作键的类型。
struct Foo
{
Foo(std::string s) : foo_value(s){}
bool operator<(const Foo& foo1) { return foo_value < foo1.foo_value; }
bool operator>(const Foo& foo1) { return foo_value > foo1.foo_value; }
std::string foo_value;
};
When used with std::map, I am getting the following error.
与std::map一起使用时,出现以下错误。
error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const Foo' (or there is no acceptable conversion) c:\program files\microsoft visual studio 8\vc\include\functional 143
If I change the struct like the below, everything worked.
如果我像下面那样更改结构,一切正常。
struct Foo
{
Foo(std::string s) : foo_value(s) {}
friend bool operator<(const Foo& foo,const Foo& foo1) { return foo.foo_value < foo1.foo_value; }
friend bool operator>(const Foo& foo,const Foo& foo1) { return foo.foo_value > foo1.foo_value; }
std::string foo_value;
};
Nothing changed except making the operator overloads as friend. I am wondering why my first code is not working?
除了将运算符重载为朋友之外,没有任何改变。我想知道为什么我的第一个代码不起作用?
Any thoughts?
有什么想法吗?
回答by unwind
I suspect you need
我怀疑你需要
bool operator<(const Foo& foo1) const;
Note the const
after the arguments, this is to make "your" (the left-hand side in the comparison) object constant.
注意const
参数之后,这是为了使“您的”(比较中的左侧)对象保持不变。
The reason only a single operator is needed is that it is enough to implement the required ordering. To answer the abstract question "does a have to come before b?" it is enough to know whether a is less than b.
只需要一个运算符的原因是它足以实现所需的排序。回答抽象的问题“a 必须在 b 之前出现吗?” 知道 a 是否小于 b 就足够了。
回答by stefanB
It's probably looking for const member operators (whatever the correct name is). This works (note const):
它可能正在寻找 const 成员运算符(无论正确的名称是什么)。这有效(注意常量):
bool operator<(const Foo& foo1) const { return foo_value < foo1.foo_value;}
EDIT: deleted operator>
from my answer as it was not needed (copy/paste from question) but it was attracting comments :)
编辑:operator>
从我的答案中删除,因为它不需要(从问题中复制/粘贴)但它吸引了评论:)
Note: I'm 100% sure that you need that constbecause I compiled the example.
注意:我 100% 确定您需要该const,因为我编译了示例。
回答by Tsvetomir Dimitrov
Note the const after the arguments, this is to make "your" (the left-hand side in the comparison) object constant.
注意参数后面的 const,这是为了使“您的”(比较中的左侧)对象保持不变。
Could you please elaborate on this? Why if you make the member const (which as far as I know means that it can't change object's state - e.g. modify private variables) guarantees that "your" will be the left-hand side?
你能详细说明一下吗?为什么如果你让成员 const (据我所知这意味着它不能改变对象的状态 - 例如修改私有变量)保证“你的”将是左侧?
回答by dpwrussell
Could you please elaborate on this? Why if you make the member const (which as far as I know means that it can't change object's state - e.g. modify private variables) guarantees that "your" will be the left-hand side?
你能详细说明一下吗?为什么如果你让成员 const (据我所知这意味着它不能改变对象的状态 - 例如修改私有变量)保证“你的”将是左侧?
I don't yet have the rep to comment on this.
我还没有代表对此发表评论。
const does not magically ensure that "your" will be the left hand side. The poster was saying that the left hand side (i.e. x in x < y) is the object on which the comparison is being called. Just as you protect y's members from change with the const on the argument to operator<, you also want to protect x's members from change with the const at the end of the method signature.
const 并不能神奇地确保“你的”将是左侧。海报说左侧(即 x in x < y)是调用比较的对象。正如您使用 operator< 的参数上的 const 保护 y 的成员免于更改一样,您还希望使用方法签名末尾的 const 来保护 x 的成员免于更改。