为什么不能将对象存储在unordered_set中? [英] Why can't I store my objects in an unordered_set?

查看:83
本文介绍了为什么不能将对象存储在unordered_set中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道集合是有序的,因此在不重载< 运算符的情况下添加对象不允许说哪个对象较小以使容器保持排序.但是,我不明白为什么使用 unordered_set 无法做到这一点.

I understand a set is ordered, thus adding an object without overloading the < operator doesn't allow to say which object is smaller to keep the container sorted. However, I don't understand why this isn't possible with an unordered_set.

如果我尝试这样的事情:

If I try something like this:

#include <iostream>
#include <string
#include <unordered_set>

struct someType{
    string name;
    int code;
};

int main(){
    std::unordered_set <someType> myset;
    myset.insert({"aaa",123});
    myset.insert({"bbb",321});
    myset.insert({"ccc",213});
    return 0;
}

我遇到了一些错误,例如:

I get a couple of errors like:

c:\ qt \ qt5.1.0 \ tools \ mingw48_32 \ lib \ gcc \ i686-w64-mingw32 \ 4.8.0 \ include \ c ++ \ bits \ hashtable_policy.h:1070:错误:无效使用了不完整的类型'struct std :: hash'

c:\qt\qt5.1.0\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\bits\hashtable_policy.h:1070: error: invalid use of incomplete type 'struct std::hash'

c:\ qt \ qt5.1.0 \ tools \ mingw48_32 \ lib \ gcc \ i686-w64-mingw32 \ 4.8.0 \ include \ c ++ \ bits \ functional_hash.h:58:错误:声明了'struct std::哈希'

c:\qt\qt5.1.0\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\bits\functional_hash.h:58: error: declaration of 'struct std::hash'

错误:没有匹配的函数可以调用'std :: unordered_set :: unordered_set()'

error: no matching function for call to 'std::unordered_set::unordered_set()'

c:\ qt \ qt5.1.0 \ tools \ mingw48_32 \ lib \ gcc \ i686-w64-mingw32 \ 4.8.0 \ include \ c ++ \ bits \ hashtable_policy.h:1103:错误:与"(const std :: hash)(const someType&)'

c:\qt\qt5.1.0\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\bits\hashtable_policy.h:1103: error: no match for call to '(const std::hash) (const someType&)'

c:\ qt \ qt5.1.0 \ tools \ mingw48_32 \ lib \ gcc \ i686-w64-mingw32 \ 4.8.0 \ include \ c ++ \ bits \ stl_function.h:208:错误:与'operator =不匹配="(操作数类型为"const someType"和"const someType")

c:\qt\qt5.1.0\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\bits\stl_function.h:208: error: no match for 'operator==' (operand types are 'const someType' and 'const someType')

那是为什么,我该如何解决?

Why is that and how can I fix it?

推荐答案

要在unordered_set或unordered_map中使用类型,您需要为该类型使用哈希函数.对于常见的类型,例如 int std :: string -标准库提供了哈希函数.对于您的类型,您可以重载标准的 std :: hash ,如下所示:

To use type in unordered_set or unordered_map you need hashing function for your type. For common types, like int or std::string - hashing function is provided by standard library. For your type, you can overload standard std::hash, like this:

namespace std {
    template <> struct hash<someType> {
        size_t operator()(const someType & x) const {
            std::hash<std::string> h;
            return h(x.name);
            // or simply return x.code
            // or do something more interesting,
            // like xor'ing hashes from both members of struct
        }
    };
}

另一种方法是为自己的类型提供重载的 operator()并将其作为哈希模板参数放入unordered_set中,如下所示:

Another way is to provide your own type with overloaded operator() and put it as hash template argument in unordered_set, like this:

struct someTypeHasher {
    size_t operator()(const someType& x) const {
        return x.code;
    }
};
std::unordered_set<someType, someTypeHasher> myset;

对基于散列的容器的理论的良好阅读方法是这里

Good reading for theory about hash based containers is here

此外,请不要忘记,您需要为 someType 重载 operator == ,没有它-它也将无法工作.

Also, do not forget, that you need to overload operator== for someType, without it - it will also not work.

这篇关于为什么不能将对象存储在unordered_set中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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