2. API
bitset提供的api:
(constructor) Construct bitset (public member function)
operator[] Access bit (public member function)
set Set bits (public member function)
reset Reset bits (public member function )
flip Flip bits (public member function)
to_ulong Convert to unsigned long integer (public member function)
to_string Convert to string (public member function)
count Count bits set (public member function)
size Return size (public member function)
test Return bit value (public member function )
any Test if any bit is set (public member function)
none Test if no bit is set (public member function)
3. 源码剖析
SGI bitset部分实现源码
template<size_t _Nb> class bitset : private _Base_bitset<__BITSET_WORDS(_Nb)> { private: typedef _Base_bitset<__BITSET_WORDS(_Nb)> _Base; typedef unsigned long _WordT; private: void _M_do_sanitize() { _Sanitize<_Nb%__BITS_PER_WORD>::_M_do_sanitize(this->_M_hiword()); } ..... }
#define __BITS_PER_WORD (CHAR_BIT*sizeof(unsigned long)) #define __BITSET_WORDS(__n) \ ((__n) < 1 ? 1 : ((__n) + __BITS_PER_WORD - 1)/__BITS_PER_WORD)
template<size_t _Nw> struct _Base_bitset { typedef unsigned long _WordT; _WordT _M_w[_Nw]; // 0 is the least significant word. _Base_bitset( void ) { _M_do_reset(); } _Base_bitset(unsigned long __val) { _M_do_reset(); _M_w[0] = __val; } static size_t _S_whichword( size_t __pos ) { return __pos / __BITS_PER_WORD; } static size_t _S_whichbyte( size_t __pos ) { return (__pos % __BITS_PER_WORD) / CHAR_BIT; } static size_t _S_whichbit( size_t __pos ) { return __pos % __BITS_PER_WORD; } static _WordT _S_maskbit( size_t __pos ) { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); } _WordT& _M_getword(size_t __pos) { return _M_w[_S_whichword(__pos)]; } _WordT _M_getword(size_t __pos) const { return _M_w[_S_whichword(__pos)]; } _WordT& _M_hiword() { return _M_w[_Nw - 1]; } _WordT _M_hiword() const { return _M_w[_Nw - 1]; } void _M_do_and(const _Base_bitset<_Nw>& __x) { for ( size_t __i = 0; __i < _Nw; __i++ ) { _M_w[__i] &= __x._M_w[__i]; } } void _M_do_or(const _Base_bitset<_Nw>& __x) { for ( size_t __i = 0; __i < _Nw; __i++ ) { _M_w[__i] |= __x._M_w[__i]; } } void _M_do_xor(const _Base_bitset<_Nw>& __x) { for ( size_t __i = 0; __i < _Nw; __i++ ) { _M_w[__i] ^= __x._M_w[__i]; } }
void fun() { const int n = 800000000; bitset<n> a; cout << a.size() << endl; } int main(int argc, char** argv) { fun(); return 0; }大内存分配可以分配在堆中,如下:
const int n = 800000000; bitset<n> *a = new(std::nothrow) bitset<n>; if(a) { cout << a->size() << endl; delete a; a = NULL; }4. vector<bool>及deque<bool>
deque<bool> a; a[0] = 0; bool* b = &a[0]; cout << *b << endl;和
vector<bool> a; a[0] = 0; bool* b = &a[0]; cout << *b << endl;将会发现:
int main(int argc, char** argv) { deque<bool> a(10000000000); sleep(100); return 0; }内存使用:
int main(int argc, char** argv) { vector<bool> a(10000000000); sleep(100); return 0; }内存使用:
int main(int argc, char** argv) { const unsigned long int n = 10000000000; bitset<n> *a = new(std::nothrow) bitset<n>; sleep(100); return 0; }
参考:
http://www.sgi.com/tech/stl/download.html
http://www.cplusplus.com/reference/stl/vector/
http://www.cplusplus.com/reference/stl/bitset/
扩展阅读:
class vector<bool>::reference { friend class vector; reference(); // no public constructor public: ~reference(); operator bool () const; // convert to bool reference& operator= ( const bool x ); // assign from bool reference& operator= ( const reference& x ); // assign from bit void flip(); // flip bit value. }