bitset简介
bitset是一个特殊的容器用来存储位的状态,元素只可能是0、1或者true、false
bitset这个类非常类似一般的数组,但是被优化为一个元素只用1个位来存储,但是因为C++环境中不存在这么小的类型,所以用最小的bool类型来模拟
bitset可以随机访问,即支持下标访问
Constructor
1.bitset ( );
2.bitset ( unsigned long val );
3.template
explicit bitset ( const basic_string& str,
typename basic_string::size_type pos = 0,
typename basic_string::size_type n =
basic_string::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& operator&= (const bitset& rhs);
bitset& operator|= (const bitset& rhs);
bitset& operator^= (const bitset& rhs);
bitset& operator<<= (size_t pos);
bitset& operator>>= (size_t pos);
bitset operator~() const;
bitset operator<<(size_t pos) const;
bitset operator>>(size_t pos) const;
bool operator== (const bitset& rhs) const;
bool operator!= (const bitset& rhs) const;
*** global functions: ***
template
bitset operator& (const bitset& lhs, const bitset& rhs);
template
bitset operator| (const bitset& lhs, const bitset& rhs);
template
bitset operator^ (const bitset& lhs, const bitset& rhs);
//
*** iostream global functions (extraction/insertion): ***
template
basic_istream&
operator>> (basic_istream& is, bitset& rhs);
template
basic_ostream&
operator<< (basic_ostream& os, bitset& 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:
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& set ( );//全部设置为1
2.bitset& 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& reset ( );//全部设置为0
2.bitset& reset ( size_t pos );//对应位置设置为0
//flip
1.bitset& flip ( );//翻转bitset,所有的元素,0→1,1→0
2.bitset& 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
basic_string to_string() const;
eg:
string mystring;
bitset<4> mybits; // mybits: 0000
mybits.set(); // mybits: 1111
mystring=mybits.to_string,allocator >();
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
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.