Bitset<> default hash function for unordered container

Source: Internet
Author: User
Tags bitset

Since c++11, Bitset is used for unordered container and will provide the default hash function.

In GCC, the relevant code is as follows:

01495   //DR 1182.01496   ///Std::hash Specialization for Bitset.01497template<size_t _nb>01498     structHash<_glibcxx_std_d::bitset<_nb>>01499: PublicStd::unary_function<_glibcxx_std_d::bitset<_nb>, size_t>01500     {01501size_t01502       operator()(Const_glibcxx_std_d::bitset<_nb>& __b)Const01503       {01504     Constsize_t __clength = (_nb + __char_bit__-1) /__char_bit__;01505     returnStd::_fnv_hash::hash (__b._m_getdata (), __clength);01506       }01507     };01508 01509Template<>01510     structhash<_glibcxx_std_d::bitset<0>>01511: Publicstd::unary_function<_glibcxx_std_d::bitset<0, size_t>01512     {01513size_t01514       operator()(Const_glibcxx_std_d::bitset<0>&)Const01515{return 0; }01516};

In vs2015, the relevant code is as follows:

// TEMPLATE struct Specialization hashtemplate<size_t _bits>    struct hash<bitset<_ bits> >    {    //  hash functor for bitset<_bits>    typedef bitset<_ Bits> Argument_type;    typedef size_t RESULT_TYPE;     operator () (constconst        {    //  hash _keyval to size_t value by Pseudorandomizing transform        return  (_keyval.hash ());        }    ;

I wrote a little program myself to test the use of Bitset in unordered container:

#include <bitset>#include<unordered_set>#include<iostream>#include"print.hpp"using namespacestd;intMain () {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;}

Note: Where PRINT.HPP borrowed the code from the C + + standard Library of Nicolai M. Josuttis.

The output is as follows:

 the 001 010 011  - 101  the 111

Bitset<> default hash function for unordered container

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.