Chapter 12.特殊容器bitset

bitset簡介

bitset是一個特殊的容器用來存儲位的狀態,元素只可能是0、1或者true、false
bitset這個類非常類似一般的數組,但是被優化爲一個元素只用1個位來存儲,但是因爲C++環境中不存在這麼小的類型,所以用最小的bool類型來模擬
bitset可以隨機訪問,即支持下標訪問
Constructor
1.bitset ( );
2.bitset ( unsigned long val );
3.template<class charT, class traits, class Allocator>
explicit bitset ( const basic_string<charT,traits,Allocator>& str,
   typename basic_string<charT,traits,Allocator>::size_type pos = 0,
   typename basic_string<charT,traits,Allocator>::size_type n =
      basic_string<charT,traits,Allocator>::npos);
eg:
  bitset<10> first;                   // empty bitset
  bitset<10> second (120ul);          // initialize from unsigned long to binary is:0001111000
  bitset<10> third (string("01011")); // initialize from string
applicable operators
***
bitset<N>& operator&= (const bitset<N>& rhs);
bitset<N>& operator|= (const bitset<N>& rhs);
bitset<N>& operator^= (const bitset<N>& rhs);
bitset<N>& operator<<= (size_t pos);
bitset<N>& operator>>= (size_t pos);
bitset<N> operator~() const;
bitset<N> operator<<(size_t pos) const;
bitset<N> operator>>(size_t pos) const;
bool operator== (const bitset<N>& rhs) const;
bool operator!= (const bitset<N>& rhs) const;
*** global functions: ***
template<size_t N>
  bitset<N> operator& (const bitset<N>& lhs, const bitset<N>& rhs);
template<size_t N>
  bitset<N> operator| (const bitset<N>& lhs, const bitset<N>& rhs);
template<size_t N>
  bitset<N> operator^ (const bitset<N>& lhs, const bitset<N>& rhs);
//
*** iostream global functions (extraction/insertion): ***
template<class charT, class traits, size_t N>
  basic_istream<charT, traits>&
    operator>> (basic_istream<charT,traits>& is, bitset<N>& rhs);
template<class charT, class traits, size_t N>
  basic_ostream<charT, traits>&
    operator<< (basic_ostream<charT,traits>& os, bitset<N>& rhs);
eg:
  bitset<4> first (string("1001"));
  bitset<4> second (string("0011"));
  cout << (first^=second) << endl;          // 1010 (XOR,assign)
  cout << (first&=second) << endl;          // 0010 (AND,assign)
  cout << (first|=second) << endl;          // 0011 (OR,assign)
  cout << (first<<=2) << endl;              // 1100 (SHL,assign)
  cout << (first>>=1) << endl;              // 0110 (SHR,assign)
  cout << (~second) << endl;                // 1100 (NOT)
  cout << (second<<1) << endl;              // 0110 (SHL)
  cout << (second>>1) << endl;              // 0001 (SHR)
  cout << (first==second) << endl;          // false (0110==0011)
  cout << (first!=second) << endl;          // true  (0110!=0011)
  cout << (first&second) << endl;           // 0010
  cout << (first|second) << endl;           // 0111
  cout << (first^second) << endl;           // 0101
Bit access:
operator[] Access bit
eg:
	bitset<10> bs(120UL);
	for (size_t i = 0; i != 10; ++i)
	{
		cout << bs[i];
	}
	bs[1] = bs[2];
	bs[3] = true;
Bit operations:
set Set bits
reset Reset bits
flip Flip bits
//set
1.bitset<N>& set ( );//全部設置爲1
2.bitset<N>& set ( size_t pos, bool val = true );//對應位置設置爲1
eg:
  bitset<4> mybits;
  cout << mybits.set() << endl;       // 1111
  cout << mybits.set(2,0) << endl;    // 1011
  cout << mybits.set(2) << endl;      // 1111
//reset
1.bitset<N>& reset ( );//全部設置爲0
2.bitset<N>& reset ( size_t pos );//對應位置設置爲0
//flip
1.bitset<N>& flip ( );//翻轉bitset,所有的元素,0→1,1→0
2.bitset<N>& flip ( size_t pos );//對應位置翻轉
Bitset operations:
to_ulong Convert to unsigned long integer//如果bitset轉爲unsigned long太長,則拋出異常
to_string Convert to string
count Count bits set//i.e.,value is 1
size Return size
test Return bit value//0 or 1
any Test if any bit is set//看bitset裏有無元素值爲1,有則返回true,否則返回false
none Test if no bit is set//看bitset裏有無元素值爲0,有則返回true,否則返回false
//to_ulong
eg:
  bitset<4> mybits;     // mybits: 0000
  mybits.set();         // mybits: 1111
  cout << mybits << " as an integer is: " << mybits.to_ulong() << endl;
//to_string
template <class charT, class traits, class Allocator>
  basic_string<charT,traits,Allocator> to_string() const;
eg:
  string mystring;
  bitset<4> mybits;     // mybits: 0000
  mybits.set();         // mybits: 1111
  mystring=mybits.to_string<char,char_traits<char>,allocator<char> >();
  cout << "mystring: " << mystring << endl;
Output:
1111
//test
eg:
 bitset<5> mybits (string("01011"));
  cout << "mybits contains:\n";
  cout << boolalpha;
  for (size_t i=0; i<mybits.size(); ++i)
    cout << mybits.test(i) << endl;
Output:
mybits contains:
true
true
false
true
false
//any
eg:
	bitset<16> mybits;
	cout << "enter a binary number: ";
	cin >> mybits;//1011
	if (mybits.any())
		cout << "mybits has " << (int)mybits.count() << " bits set.\n";
	else cout << "mybits has no bits set.\n";
Output:
mybits has 3 bits set.
//none
eg:
	bitset<16> mybits;
	cout << "enter a binary number: ";
	cin >> mybits;//1011
	if (mybits.none())
		cout << "mybits has no bits unset.\n";
	else
		cout << "mybits has " << (int)mybits.count() << " bits unset.\n";
Output:
mybits has 1 bits unset.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章