Bitset & lt; & gt; default hash function for unordered container, bitsetunordered

Source: Internet
Author: User

Bitset <> default hash function for unordered container, bitsetunordered

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

In gcc, the related code is as follows:

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_t01502       operator()(const _GLIBCXX_STD_D::bitset<_Nb>& __b) const01503       {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_t01514       operator()(const _GLIBCXX_STD_D::bitset<0>&) const01515       { return 0; }01516     };

In vs2015, the related 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;    size_t operator()(const argument_type& _Keyval) const        {    // hash _Keyval to size_t value by pseudorandomizing transform        return (_Keyval.hash());        }    };

I also wrote a small program to test the usage of bitset in 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;}

Note: print. hpp borrowed The code in The C ++ Standard Library of niclai M. josutis.

The output is as follows:

000 001 010 011 100 101 110 111

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.