C++ bool std::operator < 带有键 std::unordered_set<int,std::hash<int>> 的映射错误

标签 c++ unordered-set

当我尝试插入此 map 时:

std::map<std::unordered_set<int >, std::pair<float, std::pair<float, float >> >

我收到此错误

error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::unordered_set,std::equal_to<_Kty>,std::allocator<_Kty>>'

我的数据定义如下:

    struct Trans {
    int Item;
    float Prob;
    float W;
};
bool operator<(const Trans &a, const Trans &b)
    {
        return a.Item < b.Item;
    }
    bool operator==( Trans c,  Trans d) { return c.Item == d.Item; }


    struct MyHash {
        size_t operator()(const Trans& x) const { return std::hash<int>()(x.Item);  }
    };


std::vector<std::vector<Trans>> data;
std::map<std::unordered_set<int>, float> S1;
std::map<std::unordered_set<int >, std::pair<float, std::pair<float, float >> > S2;
std::map<std::unordered_set<int >, std::pair<float, std::pair<float, float >> > S3;

有问题的部分:

    do
        {

std::unordered_set<Trans, MyHash> KS(data[i].begin(), data[i].begin() + k);
std::unordered_set<int > elem;

float esupp = 1;
float Weight = 0;
float Wesupp = 1;
    for (auto const &iter : KS)
        {
          elem.insert(iter.Item);
           esupp *= iter.Prob;
          Weight += iter.W;
        }
        Weight = Weight / k;


        /*
        some code, and until here I didn't get any problem
        */

            **// This the area that has the problem** 

    S1[elem] = std::move(S1[elem] + esupp);
    Wesupp = Weight * S1[elem];
    S2[elem].first = std::move(S2[elem].first + esupp);
    S2[elem].second = std::make_pair(elem, Wesupp);
  } while (next_combination(data[i].begin(), data[i].begin() + k, data[i].end()));

最佳答案

一个 std::map 期望其 key 实现运算符 < ,除非提供比较器。

您的 key 类型, std::unordered_set 不实现“小于”。

作为@T.C.提到,你可以使用 std::set而不是std::unordered_set .

关于C++ bool std::operator < 带有键 std::unordered_set<int,std::hash<int>> 的映射错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24253403/

相关文章:

C++ libPNG - 简单的初始化错误

c++ - cmake 在 linux 上找不到 boost header

c++ - 如何将 unordered_set 与自定义结构一起使用?

c++ - 为什么我不能使用 pair 作为 unordered_set/unordered_map 的键?

c++ - 为什么无序集会混合值

c++ - 如何通过 g++-arm-linux-gnueabihf 为 Arm 构建 Qt?

c++ - 将文本添加到 jpeg

c++ - 将焦点转移到另一个程序 Windows API

c++ - 填充 unordered_set 的更有效方法?

c++ - Qt 的 std::unordered_set 模拟/对应