免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3407 | 回复: 2
打印 上一主题 下一主题

[C++] unordered_set的hash可以是普通函数或者lambda吗? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2018-07-28 16:32 |只看该作者 |倒序浏览
  1. #include<unordered_set>
  2. using namespace std;
  3. struct node{
  4.        size_t value;
  5.        node* next;
  6.        node():value(-1),next(NULL){}
  7. };
  8. struct myhash{
  9.        size_t operator()(const node& n)const{
  10.            return (size_t)n.value;
  11.      }
  12. };
  13. size_t h(const node& n){
  14.        return (size_t)n.value;
  15. }
  16. int main(){
  17.        unordered_set<node, myhash> s1;
  18.        unordered_set<node, h> s2;
  19.        unordered_set<node, [](const node& e){return n.value;}> s;
  20.        return 0;
  21. }
复制代码


编译main函数,第二行和第三行有编译错误。
  1. ||=== Build: Release in my (compiler: GNU GCC Compiler) ===|
  2. C:\Users\a\Documents\my\main.cpp||In function 'int main()':|
  3. C:\Users\a\Documents\my\main.cpp|18|error: type/value mismatch at argument 2 in template parameter list for 'template<class _Value, class _Hash, class _Pred, class _Alloc> class std::unordered_set'|
  4. C:\Users\a\Documents\my\main.cpp|18|note:   expected a type, got 'h'|
  5. C:\Users\a\Documents\my\main.cpp|19|error: lambda-expression in template-argument|
  6. C:\Users\a\Documents\my\main.cpp|19|error: template argument 2 is invalid|
  7. C:\Users\a\Documents\my\main.cpp|18|warning: unused variable 's2' [-Wunused-variable]|
  8. C:\Users\a\Documents\my\main.cpp|19|warning: unused variable 's' [-Wunused-variable]|
  9. ||=== Build failed: 3 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
复制代码


  
这个是unordered_set的限制,还是我的用法有问题呢?
谢谢
  

论坛徽章:
0
2 [报告]
发表于 2018-07-28 17:05 |只看该作者
unordered_set<node, decltype(h)> s2;

这样也不行。我该如何改呢?

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
3 [报告]
发表于 2018-07-30 08:58 |只看该作者
#include <functional>

unordered_set<node, function<size_t(const node& n)>> s2( 1, h );
unordered_set<node, function<size_t(const node& n)>> s( 1, [](const node& n){return n.value;} );
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP