首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何正确使用struct/std::pair?

如何正确使用struct/std::pair?
EN

Stack Overflow用户
提问于 2017-04-17 06:51:16
回答 1查看 349关注 0票数 2
代码语言:javascript
复制
usr/include/c++/4.8/bits/hashtable_policy.h:1082:53: error: invalid use of incomplete type âstrruct std::hash<NAMESPACE::Span<int> >â
using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>;

operator==(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
 ^
/usr/include/c++/4.8/bits/unordered_map.h:1388:5: note:   template  argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/string:48:0,

-----------------------------------------------------------
/usr/include/c++/4.8/bits/hashtable_policy.h: In instantiation of âstruct  std::__detail::_Hash_ccode_base<std::pair<int, int>, std::pair<const std::pair<int, int>, NAMESPACE::ConfigSet>, std::__detail::_Select1st, std::hash<std::pair<int, int> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>â:
/usr/include/c++/4.8/bits/unordered_map.h:100:18:   required from âclass std::unordered_map<std::pair<int, int>, RTBKIT::ConfigSet>â

怎么了?下面是我的结构:

代码语言:javascript
复制
namespace MYNAMESPACE {

template<typename T>
//can pass in a list than a range for IntervalFilter
struct Span
{
    //no arg/two args not working for the template typename T
    //span(T lb, T ub);
    //span();

    T lowerBound;
    T upperBound;

    static Span
    createFromJson(const Json::Value & val); 


    void fromJson(const Json::Value & val);

    Json::Value toJson() const;

    bool empty() const;
    //didn't declare this template type, isIncluded is not used
    template<typename U> bool isIncluded(const U & value) const;

 };
 } //namespace

 #endif 

代码使用struct:...

代码语言:javascript
复制
 private:
    static constexpr unsigned PRIORITY = 0x1401; }
    //std::unordered_map<Span<int>, ConfigSet> data;
    std::unordered_map<std::pair<int,int>, ConfigSet> data;
    void setConfig(unsigned configIndex, const AgentConfig& config) {
         auto range = [] (int first, int last) {
         return std::make_pair(first, last);
         };
         //int l = config.rangeFilter.lowerBound;
         //int r  = configrangeFilter.upperBound;
         //data[config.rangeFilter].set(configIndex);
         data[range(config.rangeFilter.lowerBound, config.rangeFilter.upperBound)].set(configIndex);
    }

};
EN

回答 1

Stack Overflow用户

发布于 2017-04-17 07:57:52

问题是该标准没有为std::pair定义hash专门化。编译器(间接地)告诉你它不知道如何为你的键生成散列值。

您应该定义自己的散列类,并在data声明中使用它。如下所示:

代码语言:javascript
复制
struct MyHash
{
  size_t operator()(std::pair<int, int> value) const
  {
    // I make no promise that this is a good hash function for your dataset.
    return value.first + value.second;
  }
};

std::unordered_map<std::pair<int, int>, ConfigSet, MyHash> data;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43442982

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档