std :: maps用户定义的类型作为键 [英] std::maps with user-defined types as key

查看:129
本文介绍了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 this cryptic error message. What does it mean? Also, why is it only happening with user-defined types? (primitive types are okay when it is used for the key)


C:\MinGW\bin..\lib\ gcc \mingw32\3.4.5 ........ \include\c ++ \3.4.5\bits\stl_function.h ||在
成员函数`bool
std :: less< _Tp> :: operator()(const _Tp&
const _Tp&)const [with _Tp =
Class1]':|

C:\MinGW\bin..\lib\gcc\mingw32\3.4.5........\include\c++\3.4.5\bits\stl_function.h||In member function `bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Class1]':|

C:\MinGW\bin..\lib\gcc\mingw32\3.4.5 ........ \include\c ++ \3.4.5\ bits \stl_map.h | 338 |实例化
从`_Tp& std :: map< _Key,_Tp,
_Compare,_Alloc> :: operator [with _Key = Class1,_Tp = int,_Compare = std :: less,_Alloc = std :: allocator>]'|

C:\MinGW\bin..\lib\gcc\mingw32\3.4.5........\include\c++\3.4.5\bits\stl_map.h|338|instantiated from `_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator [with _Key = Class1, _Tp = int, _Compare = std::less, _Alloc = std::allocator >]'|

C:\Users\Admin\Documents\dev\sandbox\sandbox\sandbox.cpp | 24 |实例化
从这里开始|

C:\Users\Admin\Documents\dev\sandbox\sandbox\sandbox.cpp|24|instantiated from here|

C:\MinGW\bin..\lib\gcc\mingw32\3.4.5 ........ \include\ c ++ \3.4.5\bits\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\}
__y'| || === Build finished:1 errors,0 warnings === |

C:\MinGW\bin..\lib\gcc\mingw32\3.4.5........\include\c++\3.4.5\bits\stl_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;
}


推荐答案

> have 为你的类定义运算符< 。你还可以为它创建一个比较器函数对象类,并使用它来专门化 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 ,它将委托给为您的类定义的运算符< (如果没有定义则失败)。但有时你想要的对象可用作地图键,但你实际上没有任何有意义的比较语义,所以你不想混淆人们通过提供运算符< 在你的类只是为了。

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 ,但是你不会将运算符<暴露给客户端代码。

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用户定义的类型作为键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆