如何使用自定义类型作为 C++ 中地图的键?
我正在尝试将自定义类型分配为 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;
};
当与 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
如果我将 struct
更改为以下内容,一切正常:
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;
};
除了操作符被重载为friend之外,没有任何改变。 为什么我的第一个代码不起作用?
I am trying to assign a custom type as a key for std::map
. Here is the type which I am using as key:
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:
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
to the one below, everything works:
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 that the operator is overloaded as friend. Why does my first code not work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑您需要
注意参数后面的
const
,这是为了使“您的”(比较中的左侧)对象保持不变。只需要一个运算符的原因是它足以实现所需的排序。 回答“a 必须在 b 之前吗?”这个抽象问题。 知道a是否小于b就足够了。
I suspect you need
Note the
const
after the arguments, this is to make "your" (the left-hand side in the comparison) object constant.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.
它可能正在寻找 const 成员运算符(无论正确的名称是什么)。
这有效(注意常量):
编辑:从我的答案中删除了
operator>
因为不需要它(从问题中复制/粘贴),但它吸引了评论:)注意:我 100% 确定你需要那个const,因为我编译了这个例子。
It's probably looking for const member operators (whatever the correct name is).
This works (note const):
EDIT: deleted
operator>
from my answer as it was not needed (copy/paste from question) but it was attracting comments :)Note: I'm 100% sure that you need that const because I compiled the example.
其他答案已经解决了您的问题,但我想提供替代解决方案。 从 C++11 开始,您可以使用 lambda 表达式 而不是为您的
struct
operator< >。 (您的映射不需要operator>
即可工作。)向映射的构造函数提供 lambda 表达式具有某些优点:struct
,则此方法特别有用。struct
作为键的不同映射提供不同的比较函数。operator<
并将其用于不同的目的。因此,您可以将
struct
保持如下所示的长度:然后可以按以下方式定义您的映射:
输出:
Ideone 上的代码
The other answers already solve your problem, but I'd like to offer an alternative solution. Since C++11 you can use a lambda expression instead of defining
operator<
for yourstruct
. (operator>
is not needed for your map to work.) Providing a lambda expression to the constructor of a map has certain advantages:struct
that you want to store in your map.struct
as key.operator<
differently and use it for a different purpose.As a result, you can keep your
struct
as short as follows:And your map can then be defined in the following way:
Output:
Code on Ideone