How can I use std::maps with user-defined types as key?(如何使用带有用户定义类型的 std::maps 作为键?)
问题描述
我想知道为什么我不能将 STL 映射与用户定义的类一起使用.当我编译下面的代码时,我收到以下神秘的错误消息.这是什么意思?另外,为什么它只发生在用户定义的类型上?(原始类型用作键时是可以的.)
I'm wondering why I can't use STL maps with user-defined classes. When I compile the code below, I get the following cryptic error message. What does it mean? Also, why is it only happening with user-defined types? (Primitive types are okay when they are used as key.)
C:MinGWin..libgccmingw323.4.5........includec++3.4.5itsstl_function.h||In成员函数`boolstd::less<_Tp>::operator()(const _Tp&,const _Tp&) const [与 _Tp =Class1]':|
C:MinGWin..libgccmingw323.4.5........includec++3.4.5itsstl_function.h||In member function `bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Class1]':|
C:MinGWin..libgccmingw323.4.5........includec++3.4.5itsstl_map.h|338|已实例化来自`_Tp&std::map<_Key, _Tp,_Compare, _Alloc>::operator[](const _Key&) [with _Key = Class1, _Tp = int, _Compare = std::less, _Alloc = std::allocator >]'|
C:MinGWin..libgccmingw323.4.5........includec++3.4.5itsstl_map.h|338|instantiated from `_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = Class1, _Tp = int, _Compare = std::less, _Alloc = std::allocator >]'|
C:UsersAdminDocumentsdevsandboxsandboxsandbox.cpp|24|已实例化从这里|
C:UsersAdminDocumentsdevsandboxsandboxsandbox.cpp|24|instantiated from here|
C:MinGWin..libgccmingw323.4.5........includec++3.4.5itsstl_function.h|227|错误:不匹配对于'运算符<'在 '__x <__y'|||==== 构建完成:1 个错误,0 个警告 ===|
C:MinGWin..libgccmingw323.4.5........includec++3.4.5itsstl_function.h|227|error: no match for 'operator<' in '__x < __y'| ||=== Build finished: 1 errors, 0 warnings ===|
#include <iostream>
#include <map>
using namespace std;
class Class1
{
public:
Class1(int id);
private:
int id;
};
Class1::Class1(int id): id(id)
{}
int main()
{
Class1 c1(1);
map< Class1 , int> c2int;
c2int[c1] = 12;
return 0;
}
推荐答案
实际上,您不必为您的类定义operator<
.您还可以为它创建一个比较器函数对象类,并使用它来专门化 std::map
.扩展您的示例:
You don't have to define operator<
for your class, actually. You can also make a comparator function object class for it, and use that to specialize std::map
. To extend your example:
struct Class1Compare
{
bool operator() (const Class1& lhs, const Class1& rhs) const
{
return lhs.id < rhs.id;
}
};
std::map<Class1, int, Class1Compare> c2int;
恰巧 std::map
的第三个模板参数的默认值是 std::less
,它将委托给为您的类定义的operator<
(如果没有则失败).但有时您希望对象可用作映射键,但您实际上没有任何有意义的 比较语义,因此您不想通过提供 operator<
在你的课堂上就是为了这个.如果是这种情况,您可以使用上述技巧.
It just so happens that the default for the third template parameter of std::map
is std::less
, which will delegate to operator<
defined for your class (and fail if there is none). But sometimes you want objects to be usable as map keys, but you do not actually have any meaningful comparison semantics, and so you don't want to confuse people by providing operator<
on your class just for that. If that's the case, you can use the above trick.
实现相同目标的另一种方法是专门化 std::less
:
Yet another way to achieve the same is to specialize std::less
:
namespace std
{
template<> struct less<Class1>
{
bool operator() (const Class1& lhs, const Class1& rhs) const
{
return lhs.id < rhs.id;
}
};
}
这样做的好处是它会被 std::map
默认"选择,但你不会将 operator<
暴露给客户端代码.
The advantage of this is that it will be picked by std::map
"by default", and yet you do not expose operator<
to client code otherwise.
这篇关于如何使用带有用户定义类型的 std::maps 作为键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用带有用户定义类型的 std::maps 作为键?
基础教程推荐
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01