相关疑难解决方法(0)

使用自定义哈希函数插入unordered_set

我有以下代码来制作unordered_set<Interval>.编译好了.

struct Interval {
  unsigned int begin;
  unsigned int end;
  bool updated;   //true if concat.  initially false
  int patternIndex;  //pattern index. valid for single pattern
  int proteinIndex;   //protein index.  for retrieving the pattern
};

struct Hash {
  size_t operator()(const Interval &interval);
};

size_t Hash::operator()(const Interval &interval){
  string temp = to_string(interval.begin) + to_string(interval.end) + to_string(interval.proteinIndex);
  return hash<string>()(temp);
}

unordered_set<Interval, string, Hash> test;
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用此代码插入时,我无法编译:

for(list<Interval>::iterator i = concat.begin(); i != concat.end(); ++i){
  test.insert((*i));
}
Run Code Online (Sandbox Code Playgroud)

而且,我无法确定错误消息的问题,例如:

note: candidate is:
note: size_t …
Run Code Online (Sandbox Code Playgroud)

c++ unordered-set c++11

22
推荐指数
1
解决办法
3万
查看次数

在c ++中声明unordered_set的哈希函数?

我必须将unordered_set用于一个相当大的项目,并确保我正确使用它,我尝试了一个小例子.

#include <iostream>
#include <unordered_set>
using namespace std;

class Foo {
  private:
    int x;
  public:
    Foo(int in) {x = in;}
    bool operator==(const Foo& foo) const {return x == foo.x;}
    size_t hash(const Foo& foo) const {return x;}
};

int main() {
  Foo f1(3);
  unordered_set<Foo> s;
  s.insert(f1);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我编译时,我得到:

/tmp/cc3KFIf4.o: In function `std::__detail::_Hash_code_base<Foo, Foo, std::_Identity<Foo>, std::equal_to<Foo>, std::hash<Foo>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, false>::_M_hash_code(Foo const&) const':
hashset.cc:(.text._ZNKSt8__detail15_Hash_code_baseI3FooS1_St9_IdentityIS1_ESt8equal_toIS1_ESt4hashIS1_ENS_18_Mod_range_hashingENS_20_Default_ranged_hashELb0EE12_M_hash_codeERKS1_[std::__detail::_Hash_code_base<Foo, Foo, std::_Identity<Foo>, std::equal_to<Foo>, std::hash<Foo>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, false>::_M_hash_code(Foo const&) const]+0x19): undefined reference to `std::hash<Foo>::operator()(Foo) const'
/tmp/cc3KFIf4.o: In function …
Run Code Online (Sandbox Code Playgroud)

c++ hash unordered-set

5
推荐指数
1
解决办法
6323
查看次数

为什么我不能将我的对象存储在unordered_set中?

我理解一个集合是有序的,因此添加一个对象而不会重载<操作符不允许说哪个对象更小以保持容器排序.但是,我不明白为什么这是不可能的unordered_set.

如果我尝试这样的事情:

#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;
}
Run Code Online (Sandbox Code Playgroud)

我得到了一些错误:

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: :哈希"

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 :: hash'的声明

错误:没有匹配函数来调用'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\stl_function.h:208:错误:不匹配'operator =='(操作数类型是'const someType'和'const someType')

为什么这样,我该如何解决?

c++ unordered-set c++11

3
推荐指数
1
解决办法
1835
查看次数

标签 统计

c++ ×3

unordered-set ×3

c++11 ×2

hash ×1