Advertisement
Guest User

Untitled

a guest
Mar 11th, 2013
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <bitset>
  3. // bitset standard header
  4. #pragma once
  5. #ifndef _BITSET_
  6. #define _BITSET_
  7. #ifndef RC_INVOKED
  8. #include <string>
  9. #include <xfunctional>
  10.  
  11.  #pragma pack(push,_CRT_PACKING)
  12.  #pragma warning(push,3)
  13.  
  14.  #pragma warning(disable: 4127)
  15.  
  16.  #pragma warning(disable: 6294)
  17.  
  18. _STD_BEGIN
  19.         // TEMPLATE CLASS _Bitset_base
  20. template<int>
  21.     struct _Bitset_base
  22.     {   // default element size
  23.     typedef char _Ty;
  24.     };
  25.  
  26. template<>
  27.     struct _Bitset_base<8>
  28.     {   // eight-byte bitset
  29.     typedef char _Ty;
  30.     };
  31.  
  32.         // TEMPLATE CLASS bitset
  33. template<size_t _Bits>
  34.     class bitset
  35.         : public _Bitset_base<_Bits <= 8 ? 1
  36.             : _Bits <= 16 ? 2
  37.             : _Bits <= 32 ? 4
  38.             : 8>
  39.     {   // store fixed-length sequence of Boolean elements
  40. public:
  41.     enum {_EEN_BITS = _Bits};   // helper for expression evaluator
  42.     typedef _Bitset_base<_Bits <= 8 ? 1
  43.         : _Bits <= 16 ? 2
  44.         : _Bits <= 32 ? 4
  45.         : 8> _Mybase;
  46.     typedef typename    // sic
  47.         _Mybase::_Ty _Ty;
  48.  
  49.     typedef bool element_type;  // retained
  50.  
  51.         // CLASS reference
  52.     class reference
  53.         {   // proxy for an element
  54.         friend class bitset<_Bits>;
  55.  
  56.     public:
  57.         reference& operator=(bool _Val)
  58.             {   // assign Boolean to element
  59.             _Pbitset->set(_Mypos, _Val);
  60.             return (*this);
  61.             }
  62.  
  63.         reference& operator=(const reference& _Bitref)
  64.             {   // assign reference to element
  65.             _Pbitset->set(_Mypos, bool(_Bitref));
  66.             return (*this);
  67.             }
  68.  
  69.         reference& flip()
  70.             {   // complement stored element
  71.             _Pbitset->flip(_Mypos);
  72.             return (*this);
  73.             }
  74.  
  75.         bool operator~() const
  76.             {   // return complemented element
  77.             return (!_Pbitset->test(_Mypos));
  78.             }
  79.  
  80.         operator bool() const
  81.             {   // return element
  82.             return (_Pbitset->test(_Mypos));
  83.             }
  84.  
  85.     private:
  86.         reference(bitset<_Bits>& _Bitset, size_t _Pos)
  87.             : _Pbitset(&_Bitset), _Mypos(_Pos)
  88.             {   // construct from bitset reference and position
  89.             }
  90.  
  91.         bitset<_Bits> *_Pbitset;    // pointer to the bitset
  92.         size_t _Mypos;  // position of element in bitset
  93.         };
  94.  
  95.     bool at(size_t _Pos) const  // retained
  96.         {   // subscript nonmutable sequence with checking
  97.         return (test(_Pos));
  98.         }
  99.  
  100.     reference at(size_t _Pos)   // retained
  101.         {   // subscript mutable sequence with checking
  102.         return (reference(*this, _Pos));
  103.         }
  104.  
  105.     bool operator[](size_t _Pos) const
  106.         {   // subscript nonmutable sequence
  107.         return (test(_Pos));
  108.         }
  109.  
  110.     reference operator[](size_t _Pos)
  111.         {   // subscript mutable sequence
  112.  #if _ITERATOR_DEBUG_LEVEL == 2
  113.         if (_Bits <= _Pos)
  114.             _DEBUG_ERROR("bitset index outside range");
  115.  
  116.  #elif _ITERATOR_DEBUG_LEVEL == 1
  117.         _SCL_SECURE_VALIDATE_RANGE(_Pos < _Bits);
  118.  #endif /* _ITERATOR_DEBUG_LEVEL == 2 */
  119.  
  120.         return (reference(*this, _Pos));
  121.         }
  122.  
  123.     bitset()
  124.         {   // construct with all false values
  125.         _Tidy();
  126.         }
  127.  
  128.  #if _HAS_CPP0X
  129.     bitset(int _Ival)
  130.         {   // construct from bits in int
  131.         unsigned int _Val = (unsigned int)_Ival;
  132.         _Tidy();
  133.         for (size_t _Pos = 0; _Val != 0 && _Pos < _Bits; _Val >>= 1, ++_Pos)
  134.             if (_Val & 1)
  135.                 set(_Pos);
  136.         }
  137.  
  138.     bitset(_ULonglong _Val)
  139.  
  140.  #else /* _HAS_CPP0X */
  141.     bitset(unsigned long _Val)
  142.  #endif /* _HAS_CPP0X */
  143.  
  144.         {   // construct from bits in unsigned long
  145.         _Tidy();
  146.         for (size_t _Pos = 0; _Val != 0 && _Pos < _Bits; _Val >>= 1, ++_Pos)
  147.             if (_Val & 1)
  148.                 set(_Pos);
  149.         }
  150.  
  151.  #define _BITSET_SIZE_TYPE  \
  152.     typename basic_string<_Elem, _Tr, _Alloc>::size_type
  153.  
  154.     template<class _Elem,
  155.         class _Tr,
  156.         class _Alloc>
  157.         explicit bitset(const basic_string<_Elem, _Tr, _Alloc>& _Str,
  158.             _BITSET_SIZE_TYPE _Pos = 0)
  159.         {   // construct from [_Pos, ...) elements in string
  160.         _Construct(_Str, _Pos,
  161.             basic_string<_Elem, _Tr, _Alloc>::npos, (_Elem)'0', (_Elem)'1');
  162.         }
  163.  
  164.     template<class _Elem,
  165.         class _Tr,
  166.         class _Alloc>
  167.         explicit bitset(const basic_string<_Elem, _Tr, _Alloc>& _Str,
  168.             _BITSET_SIZE_TYPE _Pos,
  169.             _BITSET_SIZE_TYPE _Count,
  170.             _Elem _E0 = (_Elem)'0',
  171.             _Elem _E1 = (_Elem)'1')
  172.         {   // construct from [_Pos, _Pos + _Count) elements in string
  173.         _Construct(_Str, _Pos, _Count, _E0, _E1);
  174.         }
  175.  
  176.     explicit bitset(const char *_Ptr)
  177.         {   // initialize from NTBS
  178.         string _Str(_Ptr);
  179.         _Construct(_Str, 0, _Str.size(), '0', '1');
  180.         }
  181.  
  182.     template<class _Elem,
  183.         class _Tr,
  184.         class _Alloc>
  185.         void _Construct(
  186.             const basic_string<_Elem, _Tr, _Alloc>& _Str,
  187.             _BITSET_SIZE_TYPE _Pos,
  188.             _BITSET_SIZE_TYPE _Count,
  189.             _Elem _E0,
  190.             _Elem _E1)
  191.         {   // initialize from [_Pos, _Pos + _Count) elements in string
  192.         typename basic_string<_Elem, _Tr, _Alloc>::size_type _Num;
  193.         if (_Str.size() < _Pos)
  194.             _Xran();    // _Pos off end
  195.         if (_Str.size() - _Pos < _Count)
  196.             _Count = _Str.size() - _Pos;    // trim _Count to size
  197.         if (_Bits < _Count)
  198.             _Count = _Bits; // trim _Count to length of bitset
  199.         _Tidy();
  200.  
  201.         for (_Pos += _Count, _Num = 0; _Num < _Count; ++_Num)
  202.             if (_Str[--_Pos] == _E1)
  203.                 set(_Num);
  204.             else if (_Str[_Pos] != _E0)
  205.                 _Xinv();
  206.         }
  207.  
  208.     bitset<_Bits>& operator&=(const bitset<_Bits>& _Right)
  209.         {   // AND in _Right
  210.         for (int _Wpos = _Words; 0 <= _Wpos; --_Wpos)
  211.             _Array[_Wpos] &= _Right._Getword(_Wpos);
  212.         return (*this);
  213.         }
  214.  
  215.     bitset<_Bits>& operator|=(const bitset<_Bits>& _Right)
  216.         {   // OR in _Right
  217.         for (int _Wpos = _Words; 0 <= _Wpos; --_Wpos)
  218.             _Array[_Wpos] |= _Right._Getword(_Wpos);
  219.         return (*this);
  220.         }
  221.  
  222.     bitset<_Bits>& operator^=(const bitset<_Bits>& _Right)
  223.         {   // XOR in _Right
  224.         for (int _Wpos = _Words; 0 <= _Wpos; --_Wpos)
  225.             _Array[_Wpos] ^= _Right._Getword(_Wpos);
  226.         return (*this);
  227.         }
  228.  
  229.     bitset<_Bits>& operator<<=(size_t _Pos)
  230.         {   // shift left by _Pos
  231.         const int _Wordshift = (int)(_Pos / _Bitsperword);
  232.         if (_Wordshift != 0)
  233.             for (int _Wpos = _Words; 0 <= _Wpos; --_Wpos)   // shift by words
  234.                 _Array[_Wpos] = _Wordshift <= _Wpos
  235.                     ? _Array[_Wpos - _Wordshift] : (_Ty)0;
  236.  
  237.         if ((_Pos %= _Bitsperword) != 0)
  238.             {   // 0 < _Pos < _Bitsperword, shift by bits
  239.             for (int _Wpos = _Words; 0 < _Wpos; --_Wpos)
  240.                 _Array[_Wpos] = (_Ty)((_Array[_Wpos] << _Pos)
  241.                     | (_Array[_Wpos - 1] >> (_Bitsperword - _Pos)));
  242.             _Array[0] <<= _Pos;
  243.             }
  244.         _Trim();
  245.         return (*this);
  246.         }
  247.  
  248.     bitset<_Bits>& operator>>=(size_t _Pos)
  249.         {   // shift right by _Pos
  250.         const int _Wordshift = (int)(_Pos / _Bitsperword);
  251.         if (_Wordshift != 0)
  252.             for (int _Wpos = 0; _Wpos <= _Words; ++_Wpos)   // shift by words
  253.                 _Array[_Wpos] = _Wordshift <= _Words - _Wpos
  254.                         ? _Array[_Wpos + _Wordshift] : (_Ty)0;
  255.  
  256.         if ((_Pos %= _Bitsperword) != 0)
  257.             {   // 0 < _Pos < _Bitsperword, shift by bits
  258.             for (int _Wpos = 0; _Wpos < _Words; ++_Wpos)
  259.                 _Array[_Wpos] = (_Ty)((_Array[_Wpos] >> _Pos)
  260.                     | (_Array[_Wpos + 1] << (_Bitsperword - _Pos)));
  261.             _Array[_Words] >>= _Pos;
  262.             }
  263.         return (*this);
  264.         }
  265.  
  266.     bitset<_Bits>& set()
  267.         {   // set all bits true
  268.         _Tidy((_Ty)~0);
  269.         return (*this);
  270.         }
  271.  
  272.     bitset<_Bits>& set(size_t _Pos,
  273.         bool _Val = true)
  274.         {   // set bit at _Pos to _Val
  275.         if (_Bits <= _Pos)
  276.             _Xran();    // _Pos off end
  277.         if (_Val)
  278.             _Array[_Pos / _Bitsperword] |= (_Ty)1 << _Pos % _Bitsperword;
  279.         else
  280.             _Array[_Pos / _Bitsperword] &= ~((_Ty)1 << _Pos % _Bitsperword);
  281.         return (*this);
  282.         }
  283.  
  284.     bitset<_Bits>& reset()
  285.         {   // set all bits false
  286.         _Tidy();
  287.         return (*this);
  288.         }
  289.  
  290.     bitset<_Bits>& reset(size_t _Pos)
  291.         {   // set bit at _Pos to false
  292.         return (set(_Pos, false));
  293.         }
  294.  
  295.     bitset<_Bits> operator~() const
  296.         {   // flip all bits
  297.         return (bitset<_Bits>(*this).flip());
  298.         }
  299.  
  300.     bitset<_Bits>& flip()
  301.         {   // flip all bits
  302.         for (int _Wpos = _Words; 0 <= _Wpos; --_Wpos)
  303.             _Array[_Wpos] = (_Ty)~_Array[_Wpos];
  304.  
  305.         _Trim();
  306.         return (*this);
  307.         }
  308.  
  309.     bitset<_Bits>& flip(size_t _Pos)
  310.         {   // flip bit at _Pos
  311.         if (_Bits <= _Pos)
  312.             _Xran();    // _Pos off end
  313.         _Array[_Pos / _Bitsperword] ^= (_Ty)1 << _Pos % _Bitsperword;
  314.         return (*this);
  315.         }
  316.  
  317.     unsigned long to_ulong() const
  318.         {   // convert bitset to unsigned long
  319.         _ULonglong _Val = to_ullong();
  320.         unsigned long _Ans = (unsigned long)_Val;
  321.         if (_Ans  != _Val)
  322.             _Xoflo();
  323.         return (_Ans);
  324.         }
  325.  
  326.     _ULonglong to_ullong() const
  327.         {   // convert bitset to unsigned long long
  328.         enum
  329.             {   // cause zero divide if unsigned long long not multiple of _Ty
  330.             _Assertion = 1
  331.                 / (int)(sizeof (_ULonglong) % sizeof (_Ty) == 0)};
  332.  
  333.         int _Wpos = _Words;
  334.         for (; (int)(sizeof (_ULonglong) / sizeof (_Ty)) <= _Wpos; --_Wpos)
  335.             if (_Array[_Wpos] != 0)
  336.                 _Xoflo();   // fail if any high-order words are nonzero
  337.  
  338.         _ULonglong _Val = _Array[_Wpos];
  339.         for (; 0 <= --_Wpos; )
  340.             _Val = ((_Val << (_Bitsperword - 1)) << 1) | _Array[_Wpos];
  341.         return (_Val);
  342.         }
  343.  
  344.     template<class _Elem,
  345.         class _Tr,
  346.         class _Alloc>
  347.         basic_string<_Elem, _Tr, _Alloc>
  348.             to_string(_Elem _E0 = (_Elem)'0',
  349.                 _Elem _E1 = (_Elem)'1') const
  350.         {   // convert bitset to string
  351.         basic_string<_Elem, _Tr, _Alloc> _Str;
  352.         typename basic_string<_Elem, _Tr, _Alloc>::size_type _Pos;
  353.         _Str.reserve(_Bits);
  354.  
  355.         for (_Pos = _Bits; 0 < _Pos; )
  356.             if (test(--_Pos))
  357.                 _Str += _E1;
  358.             else
  359.                 _Str += _E0;
  360.         return (_Str);
  361.         }
  362.  
  363.     template<class _Elem,
  364.         class _Tr>
  365.         basic_string<_Elem, _Tr, allocator<_Elem> >
  366.             to_string(_Elem _E0 = (_Elem)'0',
  367.                 _Elem _E1 = (_Elem)'1') const
  368.         {   // convert bitset to string
  369.         return (to_string<_Elem, _Tr, allocator<_Elem> >(_E0, _E1));
  370.         }
  371.  
  372.     template<class _Elem>
  373.         basic_string<_Elem, char_traits<_Elem>, allocator<_Elem> >
  374.             to_string(_Elem _E0 = (_Elem)'0',
  375.                 _Elem _E1 = (_Elem)'1') const
  376.         {   // convert bitset to string
  377.         return (to_string<_Elem, char_traits<_Elem>,
  378.             allocator<_Elem> >(_E0, _E1));
  379.         }
  380.  
  381.         string to_string(char _E0 = '0', char _E1 = '1') const
  382.         {   // convert bitset to string
  383.         return (to_string<char, char_traits<char>, allocator<char> >(
  384.             _E0, _E1));
  385.         }
  386.  
  387.     size_t count() const
  388.         {   // count number of set bits
  389.         static char _Bitsperhex[] = "\0\1\1\2\1\2\2\3\1\2\2\3\2\3\3\4";
  390.         size_t _Val = 0;
  391.         for (int _Wpos = _Words; 0 <= _Wpos; --_Wpos)
  392.             for (_Ty _Wordval = _Array[_Wpos]; _Wordval != 0; _Wordval >>= 4)
  393.                 _Val += _Bitsperhex[_Wordval & 0xF];
  394.         return (_Val);
  395.         }
  396.  
  397.     size_t size() const
  398.         {   // return size of bitset
  399.         return (_Bits);
  400.         }
  401.  
  402.  #if _HAS_CPP0X
  403.     size_t hash() const
  404.         {   // hash bits to size_t value by pseudorandomizing transform
  405.         size_t _Val = 2166136261U;
  406.         for (int _Wpos = _Words; 0 <= _Wpos; --_Wpos)
  407.             _Val = 16777619U * _Val ^ _Array[_Wpos];
  408.         return (_Val);
  409.         }
  410.  #endif /* _HAS_CPP0X */
  411.  
  412.     bool operator==(const bitset<_Bits>& _Right) const
  413.         {   // test for bitset equality
  414.         for (int _Wpos = _Words; 0 <= _Wpos; --_Wpos)
  415.             if (_Array[_Wpos] != _Right._Getword(_Wpos))
  416.                 return (false);
  417.         return (true);
  418.         }
  419.  
  420.     bool operator!=(const bitset<_Bits>& _Right) const
  421.         {   // test for bitset inequality
  422.         return (!(*this == _Right));
  423.         }
  424.  
  425.     bool test(size_t _Pos) const
  426.         {   // test if bit at _Pos is set
  427.         if (_Bits <= _Pos)
  428.             _Xran();    // _Pos off end
  429.         return ((_Array[_Pos / _Bitsperword]
  430.             & ((_Ty)1 << _Pos % _Bitsperword)) != 0);
  431.         }
  432.  
  433.     bool any() const
  434.         {   // test if any bits are set
  435.         for (int _Wpos = _Words; 0 <= _Wpos; --_Wpos)
  436.             if (_Array[_Wpos] != 0)
  437.                 return (true);
  438.         return (false);
  439.         }
  440.  
  441.     bool none() const
  442.         {   // test if no bits are set
  443.         return (!any());
  444.         }
  445.  
  446.     bool all() const
  447.         {   // test if all bits set
  448.         return (count() == size());
  449.         }
  450.  
  451.     bitset<_Bits> operator<<(size_t _Pos) const
  452.         {   // return bitset shifted left by _Pos
  453.         return (bitset<_Bits>(*this) <<= _Pos);
  454.         }
  455.  
  456.     bitset<_Bits> operator>>(size_t _Pos) const
  457.         {   // return bitset shifted right by _Pos
  458.         return (bitset<_Bits>(*this) >>= _Pos);
  459.         }
  460.  
  461.     _Ty _Getword(size_t _Wpos) const
  462.         {   // get word at _Wpos
  463.         return (_Array[_Wpos]);
  464.         }
  465.  
  466. private:
  467.     enum
  468.         {   // parameters for packing bits into words
  469.         _Bitsperword = (int)(CHAR_BIT * sizeof (_Ty))// bits in each word
  470.         _Words = (int)(_Bits == 0
  471.             ? 0 : (_Bits - 1) / _Bitsperword)}; // NB: number of words - 1
  472.  
  473.     void _Tidy(_Ty _Wordval = 0)
  474.         {   // set all words to _Wordval
  475.         for (int _Wpos = _Words; 0 <= _Wpos; --_Wpos)
  476.             _Array[_Wpos] = _Wordval;
  477.         if (_Wordval != 0)
  478.             _Trim();
  479.         }
  480.  
  481.     void _Trim()
  482.         {   // clear any trailing bits in last word
  483.         _Trim_if<_Bits % _Bitsperword != 0>();
  484.         }
  485.  
  486.     template<bool _Has_bits>
  487.         void _Trim_if()
  488.         {   // bits to trim, remove them
  489.         _Array[_Words] &= ((_Ty)1 << _Bits % _Bitsperword) - 1;
  490.         }
  491.  
  492.     template<>
  493.         void _Trim_if<false>()
  494.         {   // no bits to trim, do nothing
  495.         }
  496.  
  497.     __declspec(noreturn) void _Xinv() const
  498.         {   // report invalid string element in bitset conversion
  499.         _Xinvalid_argument("invalid bitset<N> char");
  500.         }
  501.  
  502.     __declspec(noreturn) void _Xoflo() const
  503.         {   // report converted value too big to represent
  504.         _Xoverflow_error("bitset<N> overflow");
  505.         }
  506.  
  507.     __declspec(noreturn) void _Xran() const
  508.         {   // report bit index out of range
  509.         _Xout_of_range("invalid bitset<N> position");
  510.         }
  511.  
  512.     _Ty _Array[_Words + 1]; // the set of bits
  513.     };
  514.  
  515.         // bitset TEMPLATE FUNCTIONS
  516. template<size_t _Bits> inline
  517.     bitset<_Bits> operator&(const bitset<_Bits>& _Left,
  518.         const bitset<_Bits>& _Right)
  519.         {   // return bitset _Left AND _Right
  520.         bitset<_Bits> _Ans = _Left;
  521.         return (_Ans &= _Right);
  522.         }
  523.  
  524. template<size_t _Bits> inline
  525.     bitset<_Bits> operator|(const bitset<_Bits>& _Left,
  526.         const bitset<_Bits>& _Right)
  527.         {   // return bitset _Left OR _Right
  528.         bitset<_Bits> _Ans = _Left;
  529.         return (_Ans |= _Right);
  530.         }
  531.  
  532. template<size_t _Bits> inline
  533.     bitset<_Bits> operator^(const bitset<_Bits>& _Left,
  534.         const bitset<_Bits>& _Right)
  535.         {   // return bitset _Left XOR _Right
  536.         bitset<_Bits> _Ans = _Left;
  537.         return (_Ans ^= _Right);
  538.         }
  539.  
  540. template<class _Elem,
  541.     class _Tr,
  542.     size_t _Bits> inline
  543.     basic_ostream<_Elem, _Tr>& operator<<(
  544.         basic_ostream<_Elem, _Tr>& _Ostr, const bitset<_Bits>& _Right)
  545.     {   // insert bitset as a string
  546.     const ctype<_Elem>& _Ctype_fac = _USE(_Ostr.getloc(), ctype<_Elem>);
  547.     const _Elem _E0 = _Ctype_fac.widen('0');
  548.     const _Elem _E1 = _Ctype_fac.widen('1');
  549.  
  550.     return (_Ostr
  551.         << _Right.template to_string<_Elem, _Tr, allocator<_Elem> >(
  552.             _E0, _E1));
  553.     }
  554.  
  555.         // TEMPLATE operator>>
  556. template<class _Elem,
  557.     class _Tr,
  558.     size_t _Bits> inline
  559.     basic_istream<_Elem, _Tr>& operator>>(
  560.         basic_istream<_Elem, _Tr>& _Istr, bitset<_Bits>& _Right)
  561.     {   // extract bitset as a string
  562.     const ctype<_Elem>& _Ctype_fac = _USE(_Istr.getloc(), ctype<_Elem>);
  563.     const _Elem _E0 = _Ctype_fac.widen('0');
  564.     const _Elem _E1 = _Ctype_fac.widen('1');
  565.     ios_base::iostate _State = ios_base::goodbit;
  566.     bool _Changed = false;
  567.     string _Str;
  568.     const typename basic_istream<_Elem, _Tr>::sentry _Ok(_Istr);
  569.  
  570.     if (_Ok)
  571.         {   // valid stream, extract elements
  572.         _TRY_IO_BEGIN
  573.         typename _Tr::int_type _Meta = _Istr.rdbuf()->sgetc();
  574.         for (size_t _Count = _Right.size(); 0 < _Count;
  575.             _Meta = _Istr.rdbuf()->snextc(), --_Count)
  576.             {   // test _Meta
  577.             _Elem _Char;
  578.             if (_Tr::eq_int_type(_Tr::eof(), _Meta))
  579.                 {   // end of file, quit
  580.                 _State |= ios_base::eofbit;
  581.                 break;
  582.                 }
  583.             else if ((_Char = _Tr::to_char_type(_Meta)) != _E0
  584.                 && _Char != _E1)
  585.                 break;  // invalid element
  586.             else if (_Str.max_size() <= _Str.size())
  587.                 {   // no room in string, give up (unlikely)
  588.                 _State |= ios_base::failbit;
  589.                 break;
  590.                 }
  591.             else
  592.                 {   // valid, append '0' or '1'
  593.                 if (_Char == _E1)
  594.                     _Str.append(1, '1');
  595.                 else
  596.                     _Str.append(1, '0');
  597.                 _Changed = true;
  598.                 }
  599.             }
  600.         _CATCH_IO_(_Istr)
  601.         }
  602.  
  603.     if (!_Changed)
  604.         _State |= ios_base::failbit;
  605.     _Istr.setstate(_State);
  606.     _Right = bitset<_Bits>(_Str);   // convert string and store
  607.     return (_Istr);
  608.     }
  609.  
  610.  #if _HAS_CPP0X
  611. template<class _Kty>
  612.     class hash;
  613.  
  614. template<size_t _Bits>
  615.     class hash<bitset<_Bits> >
  616.         : public unary_function<bitset<_Bits>, size_t>
  617.     {   // hash functor
  618. public:
  619.     typedef bitset<_Bits> _Kty;
  620.  
  621.     size_t operator()(const _Kty& _Keyval) const
  622.         {   // hash _Keyval to size_t value by pseudorandomizing transform
  623.         return (_Keyval.hash());
  624.         }
  625.     };
  626.  #endif /* _HAS_CPP0X */
  627. _STD_END
  628.  
  629.  #pragma warning(pop)
  630.  #pragma pack(pop)
  631.  
  632. #endif /* RC_INVOKED */
  633. #endif /* _BITSET */
  634.  
  635. /*
  636.  * Copyright (c) 1992-2009 by P.J. Plauger.  ALL RIGHTS RESERVED.
  637.  * Consult your license regarding permissions and restrictions.
  638. V5.20:0009 */
  639.  
  640. std::bitset<16> s;
  641.  
  642. int main(){
  643.     s[0] = 1;
  644.     s[7] = 1;
  645.     std::cout << s;
  646.     system("pause");
  647.  
  648. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement