Chinaunix

标题: unordered_set使用自定义hash函数编译错误,帮忙砍下 [打印本页]

作者: zhycjh    时间: 2018-07-28 23:00
标题: unordered_set使用自定义hash函数编译错误,帮忙砍下
如题,我测试了一下:

  1. int main(){
  2.        auto f=[](const node& n){
  3.               return (size_t)n.value;
  4.        };
  5.        unordered_set<node, decltype(f)> s2;
  6.        return 0;
  7. }
复制代码
编译错误:
g++ -std=c++11 usehashset.cpp
usehashset.cpp: In function ‘int main()’:
usehashset.cpp:21:39: error: use of deleted function ‘std::unordered_set<_Value, _Hash, _Pred, _Alloc>::unordered_set() [with _Value = node; _Hash = main()::<lambda(const node&)>; _Pred = std::equal_to<node>; _Alloc = std::allocator<node>]’
      unordered_set<node, decltype(f)> s2;

这个该怎么改呢?谢谢。



作者: bruceteen    时间: 2018-07-30 08:49
  1. #include <unordered_set>
  2. using namespace std;

  3. struct node
  4. {
  5.     size_t value;
  6. };

  7. int main( void )
  8. {
  9.     auto f = [](const node& n) {
  10.         return (size_t)n.value;
  11.     };
  12.     auto g = [](const node& a,const node& b) {
  13.         return a.value==b.value;
  14.     };
  15.     std::unordered_set<node,decltype(f),decltype(g)> s2( 100, f, g );

  16.     node a = { 0 };
  17.     s2.insert(a);
  18. }
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2