自从c++11起,bitset用于unordered container,将会提供默认的hash函数。

在gcc中,相关代码如下:

01495   // DR 1182.
01496 /// std::hash specialization for bitset.
01497 template<size_t _Nb>
01498 struct hash<_GLIBCXX_STD_D::bitset<_Nb>>
01499 : public std::unary_function<_GLIBCXX_STD_D::bitset<_Nb>, size_t>
01500 {
01501 size_t
01502 operator()(const _GLIBCXX_STD_D::bitset<_Nb>& __b) const
01503 {
01504 const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
01505 return std::_Fnv_hash::hash(__b._M_getdata(), __clength);
01506 }
01507 };
01508
01509 template<>
01510 struct hash<_GLIBCXX_STD_D::bitset<0>>
01511 : public std::unary_function<_GLIBCXX_STD_D::bitset<0>, size_t>
01512 {
01513 size_t
01514 operator()(const _GLIBCXX_STD_D::bitset<0>&) const
01515 { return 0; }
01516 };

在vs2015中,相关代码如下:

// TEMPLATE STRUCT SPECIALIZATION hash
template<size_t _Bits>
struct hash<bitset<_Bits> >
{ // hash functor for bitset<_Bits>
typedef bitset<_Bits> argument_type;
typedef size_t result_type; size_t operator()(const argument_type& _Keyval) const
{ // hash _Keyval to size_t value by pseudorandomizing transform
return (_Keyval.hash());
}
};

我自己也写了一小段程序来试验bitset在unordered container中的用法:

#include <bitset>
#include <unordered_set>
#include <iostream>
#include "print.hpp"
using namespace std; int main()
{
bitset<3> bitset00(0);
bitset<3> bitset01(1);
bitset<3> bitset02(2);
bitset<3> bitset03(3);
bitset<3> bitset04(4);
bitset<3> bitset05(5);
bitset<3> bitset06(6);
bitset<3> bitset07(7); unordered_set<bitset<3>> coll = {bitset00, bitset01, bitset02, bitset03, bitset04, bitset05, bitset06, bitset07 };
PRINT_ELEMENTS(coll);
return 0;
}

注:其中print.hpp借用的是Nicolai M. Josuttis的The C++ Standard Library中的代码。

其输出如下:

000 001 010 011 100 101 110 111

Bitset<>用于unordered container时的默认hash函数的更多相关文章

  1. STL学习笔记— —无序容器(Unordered Container)

    简单介绍 在头文件<unordered_set>和<unordered_map> 中定义 namespace std { template <typename T, ty ...

  2. cmder设置打开时的默认目录

    cmder设置打开时的默认目录 打开cmder自动进入工作目录,怎么配置? http://superuser.com/questions/1005285/run-a-bat-file-with-cmd ...

  3. input 默认值为灰色,输入时清楚默认值

    input 默认值为灰色,输入时清楚默认值 <input value="please input your name" onFocus="if(value==def ...

  4. magento -- 添加新产品时状态默认为激活,库存状态默认为有库存

    添加新产品时状态默认为激活 打开文件/app/code/core/Mage/Catalog/Model/Product/Status.php,注释掉“Please Select” /** * Retr ...

  5. [ActionScript 3.0] AS3 用于拖动对象时跟随鼠标的缓动效果

    package com.fylibs.components.effects { import flash.display.DisplayObject; import flash.events.Even ...

  6. 其他函数:值为NULL时的默认值NVL,DECODE

    NVL(列,默认数字值),此函数返回值为数值型,非NULL时返回原始值,NULL时返回默认数字值. DECODE:

  7. [笔试题目]使用Stringbuffer无 参的构造函数创建 一个对象时,默认的初始容量是多少? 如果长度不够使用了,自动增长多少倍?

    [笔试题目] 使用Stringbuffer无 参的构造函数创建 一个对象时,默认的初始容量是多少? 如果长度不够使用了,自动增长多少倍? StringBuffer 底层是依赖了一个字符数组才能存储字符 ...

  8. Android开发,在Activity启动时,默认隐藏软键盘。和遮挡Edittext时的处理

    在Activity启动时,默认隐藏软键盘: 在AndroidManifest.xml中找到你得Activity ,为它添加属性: android:windowSoftInputMode="s ...

  9. JS检查当图片不存在时显示默认图片和键盘大小写键状态

    当图片不存在时显示默认图片 <script type="text/javascript"> var imgs = document.images; for(var i ...

随机推荐

  1. 3、Redis 基础

    Redis的五大数据类型 String(字符串) string是redis最基本的类型,你可以理解成与Memcached一模一样的类型,一个key对应一个value.string类型是二进制安全的.意 ...

  2. Java多线程系列--“JUC锁”02之 互斥锁ReentrantLock

    本章对ReentrantLock包进行基本介绍,这一章主要对ReentrantLock进行概括性的介绍,内容包括:ReentrantLock介绍ReentrantLock函数列表ReentrantLo ...

  3. 1Z0-053 争议题目解析481

    1Z0-053 争议题目解析481 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 481.Which statement is true about a running sessi ...

  4. composer安装yii2问题总结

    今天周六,在家安装yii2的advanced版本, 过程有些坎坷, 不过最后总算安装好了. 总结一下, 主要遇到下面两个问题: 1, 下载速度慢, 主要原因是网络问题 下载yii2时, 模板(除了ve ...

  5. jQuery-1.9.1源码分析系列(十六)ajax——ajax框架

    ajax的介绍就不多说了,点击可看. 既然是ajax框架,那么闲谈一谈jQuery的ajax处理思路. 现在的浏览器都支持ajax,只不过不同的浏览器使用方法可能有不同(IE使用new window. ...

  6. Linux上的SQL Server的起步

    我们知道,几个星期前,微软发布了在Linux上直接运行的SQL Server第一个公开CTP版本!因此,对我来说,是时候跨界在Linux上安装我的第一个SQL安装,这样的话,我就可以在Linux上折腾 ...

  7. 12款响应式 Lightbox(灯箱)效果插件

    灯箱效果(Lightbox)是网站中最常用的效果之一,用于实现类似模态对话框的效果.网络上各种 Lightbox 插件琳琅满目,随着响应式设计(Respnsive Design)的发展,这一先进理念也 ...

  8. Newtonsoft.Json(Json.Net)学习笔记

    Newtonsoft.Json 在Vs2013中就有自带的: 下面是Json序列化和反序列化的简单封装: /// <summary> /// Json帮助类 /// </summar ...

  9. ASP.NET MVC开发中常见异常及解决方案

    ASP.NET MVC4入门到精通系列目录汇总 NHibernate:no persister for 异常 1.配置文件后缀名写错 mapping file 必须是.hbm.xml结尾 2.Web. ...

  10. Atitit 面向对象编程(OOP)、面向组件编程(COP)、面向方面编程(AOP)和面向服务编程(SOP)的区别和联系

    Atitit 面向对象编程(OOP).面向组件编程(COP).面向方面编程(AOP)和面向服务编程(SOP)的区别和联系 1. 面向组件编程(COP) 所以,组件比起对象来的进步就在于通用的规范的引入 ...