<pre name="code" class="cpp">Bitset 一个bitset店位(只有两个可能的值的元素:0或1,真或假,...)。 类模拟布尔元素的数组,但空间分配的优化:一般,每个元素只占用一个位(其中,在大多数系统上,比最小的元素种类少八倍:字符)。 每个位都可以单独进行访问:例如,对于一个给定的位集名为foo,表达式为foo [3]访问其第四位,就像一个普通的数组访问它的元素。但是,因为没有元素类型是在大多数C ++环境的单一比特,各个元件被作为特殊的引用类型(见的bitset ::参考)进行访问。 位集必须从构造的能够特征,并转换为两整数值和二进制字符串(见它的构造和部件to_ulong和to_string)。它们也可以直接插入和二进制格式的流中提取(见适用运算符)。 一个bitset的大小是固定在编译时(由它的模板参数确定)。对于一类,也优化了空间分配,并允许动态调整大小,看载体的布尔专业化(矢量<布尔>)。 ms-help://MS.MSDNQTR.v90.chs/dv_vcstdlib/html/20aa5011-5d31-4f45-8055-60f514091408.htm http://www.cplusplus.com/reference/bitset/bitset/?kw=bitset
<pre name="code" class="cpp">#include <bitset> #include <iostream> //构造函数 void bitsetConstructor(void); //测试序列中的任何位是否被设置为1 void bitset_any(void); //返回的位序列中设定的位的数目 void bitset_count(void); //切换在一个bitset所有位的值,或在指定位置切换的单个比特 void bitset_flip(void); //测试,如果没有,但已经在一个bitset对象设置为1 void bitset_none(void); //重置在一个bitset所有位为0,或在指定位置复位为0位 void bitset_reset(void); //在设置一个bitset所有位为1或设置一个位在指定位置1 void bitset_set(void); //返回位的一个bitset对象的数目 void bitset_size(void); //试验在一个bitset一个指定位置的位是否被设置为1。 void bitset_test(void); //一个bitset对象转换为字符串表示 void bitset_to_string(void); //一个bitset对象如果用于初始化位集,将产生包含比特序列的整数。 void bitset_to_ulong(void); int main() { //bitsetConstructor(); //bitset_any(); //bitset_count(); //bitset_flip(); //bitset_none(); //bitset_reset(); //bitset_set(); //bitset_size(); //bitset_test(); //bitset_to_string(); bitset_to_ulong(); return 0; } //构造函数 void bitsetConstructor(void) { using namespace std; bitset<2> b0; cout << "The set of bits in bitset<2> b0 is: ( " << b0 << " )." << endl; // Using the second member function bitset<5> b1(6); cout << "The set of bits in bitset<5> b1( 6 ) is: ( " << b1 << " )." << endl; // The template parameter N can be an expresssion bitset< 2 * sizeof(int) > b2; cout << "The set of bits in bitset<2 * sizeof ( int ) > b2 is: ( " << b2 << " )." << endl; // The base two representation will be truncated // if its length exceeds the size of the bitset bitset<3> b3(6); cout << "The set of bits in bitset<3> b3( 6 ) is ( " << b3 << " )." << endl; // Using the third member function with the first parameter string bitval4("10011"); bitset<5> b4(bitval4); cout << "The set of bits in bitset<5> b4( bitval4 ) is ( " << b4 << " )." << endl; // Only part of the string may be used for initialization // Starting at position 3 for a length of 6 (100110) string bitval5("11110011011"); bitset<6> b5(bitval5, 3, 6); cout << "The set of bits in bitset<11> b5( bitval, 3, 6 ) is ( " << b5 << " )." << endl; // The bits not initialized with part of the string // will default to zero bitset<11> b6(bitval5, 3, 5); cout << "The set of bits in bitset<11> b6( bitval5, 3, 5 ) is ( " << b6 << " )." << endl; // Starting at position 2 and continue to the end of the string bitset<9> b7(bitval5, 2); cout << "The set of bits in bitset<9> b7( bitval, 2 ) is ( " << b7 << " )." << endl; return; /* The set of bits in bitset<2> b0 is: ( 00 ). The set of bits in bitset<5> b1( 6 ) is: ( 00110 ). The set of bits in bitset<2 * sizeof ( int ) > b2 is: ( 00000000 ). The set of bits in bitset<3> b3( 6 ) is ( 110 ). The set of bits in bitset<5> b4( bitval4 ) is ( 10011 ). The set of bits in bitset<11> b5( bitval, 3, 6 ) is ( 100110 ). The set of bits in bitset<11> b6( bitval5, 3, 5 ) is ( 00000010011 ). The set of bits in bitset<9> b7( bitval, 2 ) is ( 110011011 ). 请按任意键继续. . . */ } //测试序列中的任何位是否被设置为1 void bitset_any(void) { using namespace std; bitset<5> b1(6); bool b, rb; cout << "The original bitset b1( 6 ) is: ( " << b1 << " )" << endl; b = b1.any(); if (b) cout << "At least one of the bits in bitset is set to 1." << endl; else cout << "None of the bits in bitset are set to 1." << endl; bitset<5> rb1; rb1 = b1.reset(); cout << "The reset bitset is: ( " << b1 << " )" << endl; rb = rb1.any(); if (rb) cout << "At least one of the bits in the reset bitset " << "are set to 1." << endl; else cout << "None of the bits in bitset b1 are set to 1." << endl; return; /* The original bitset b1( 6 ) is: ( 00110 ) At least one of the bits in bitset is set to 1. The reset bitset is: ( 00000 ) None of the bits in bitset b1 are set to 1. 请按任意键继续. . . */ } //返回的位序列中设定的位的数目 void bitset_count(void) { using namespace std; bitset<5> b1(4); cout << "The collection of bits in the original bitset is: ( " << b1 << " )" << endl; size_t i; i = b1.count(); cout << "The number of bits in the bitset set to 1 is: " << i << "." << endl; bitset<5> fb1; fb1 = b1.flip(); cout << "The collection of flipped bits in the modified bitset " << "is: ( " << b1 << " )" << endl; size_t ii; ii = fb1.count(); cout << "The number of bits in the bitset set to 1 is: " << ii << "." << endl; return; /* The collection of bits in the original bitset is: ( 00100 ) The number of bits in the bitset set to 1 is: 1. The collection of flipped bits in the modified bitset is: ( 11011 ) The number of bits in the bitset set to 1 is: 4. 请按任意键继续. . . */ } //切换在一个bitset所有位的值,或在指定位置切换的单个比特 void bitset_flip(void) { using namespace std; bitset<5> b1(6); cout << "The collection of bits in the original bitset is: ( " << b1 << " )" << endl; bitset<5> fb1; fb1 = b1.flip(); cout << "After flipping all the bits, the bitset becomes: ( " << fb1 << " )" << endl; bitset<5> f3b1; f3b1 = b1.flip(3); cout << "After flipping the fourth bit, the bitset becomes: ( " << f3b1 << " )" << endl << endl; bitset<5> b2; int i; for (i = 0; i <= 4; i++) { b2.flip(i); cout << b2 << " The bit flipped is in position " << i << ".\n"; } return; /* The collection of bits in the original bitset is: ( 00110 ) After flipping all the bits, the bitset becomes: ( 11001 ) After flipping the fourth bit, the bitset becomes: ( 10001 ) 00001 The bit flipped is in position 0. 00011 The bit flipped is in position 1. 00111 The bit flipped is in position 2. 01111 The bit flipped is in position 3. 11111 The bit flipped is in position 4. 请按任意键继续. . . */ } //测试,如果没有,但已经在一个bitset对象设置为1 void bitset_none(void) { using namespace std; bitset<5> b1(6); bool b, rb; cout << "Original bitset b1(6) is: ( " << b1 << " )" << endl; b = b1.none(); if (b) cout << "None of the bits in bitset b1 are set to 1." << endl; else cout << "At least one of the bits in bitset b1 is set to 1." << endl; bitset<5> rb1; rb1 = b1.reset(); rb = rb1.none(); if (rb) cout << "None of the bits in bitset b1 are set to 1." << endl; else cout << "At least one of the bits in bitset b1 is set to 1." << endl; return; /* Original bitset b1(6) is: ( 00110 ) At least one of the bits in bitset b1 is set to 1. None of the bits in bitset b1 are set to 1. 请按任意键继续. . . */ } //重置在一个bitset所有位为0,或在指定位置复位为0位 void bitset_reset(void) { using namespace std; bitset<5> b1(13); cout << "The set of bits in bitset<5> b1(13) is: ( " << b1 << " )" << endl; bitset<5> b1r3; b1r3 = b1.reset(2); cout << "The collecion of bits obtained from resetting the\n" << " third bit of bitset b1 is: ( " << b1r3 << " )" << endl; bitset<5> b1r; b1r = b1.reset(); cout << "The collecion of bits obtained from resetting all\n" << " the elements of the bitset b1 is: ( " << b1r << " )" << endl; return; /* The set of bits in bitset<5> b1(13) is: ( 01101 ) The collecion of bits obtained from resetting the third bit of bitset b1 is: ( 01001 ) The collecion of bits obtained from resetting all the elements of the bitset b1 is: ( 00000 ) 请按任意键继续. . . */ } //在设置一个bitset所有位为1或设置一个位在指定位置1 void bitset_set(void) { using namespace std; bitset<5> b1(6); cout << "The set of bits in bitset<5> b1(6) is: ( " << b1 << " )" << endl; bitset<5> b1s0; b1s0 = b1.set(0); cout << "The collecion of bits obtained from setting the\n" << " zeroth bit of bitset b1 is: ( " << b1s0 << " )" << endl; bitset<5> bs1; bs1 = b1.set(); cout << "The collecion of bits obtained from setting all the\n" << " elements of the bitset b1 is: ( " << bs1 << " )" << endl; return; /* The set of bits in bitset<5> b1(6) is: ( 00110 ) The collecion of bits obtained from setting the zeroth bit of bitset b1 is: ( 00111 ) The collecion of bits obtained from setting all the elements of the bitset b1 is: ( 11111 ) 请按任意键继续. . . */ } //返回位的一个bitset对象的数目 void bitset_size(void) { using namespace std; bitset<5> b1(6); size_t i; cout << "The set of bits in bitset<5> b1( 6 ) is: ( " << b1 << " )" << endl; i = b1.size(); cout << "The number of bits in bitset b1 is: " << i << "." << endl; return; /* The set of bits in bitset<5> b1( 6 ) is: ( 00110 ) The number of bits in bitset b1 is: 5. 请按任意键继续. . . */ } //试验在一个bitset一个指定位置的位是否被设置为1。 void bitset_test(void) { using namespace std; bitset<5> b1(6); bool b; bitset<5>::element_type e; cout << "Original bitset b1(6) is: ( " << b1 << " )" << endl; cout << "Note: positions in a bitset are numbered\n" << " from the right starting with 0.\n" << endl; b = b1.test(3); if (b) cout << "The bit at position 3 of bitset b1 " << "has a value of 1." << endl; else cout << "The bit at position 3 of bitset b1 " << "has a value of 0." << endl; b1[3] = 1; cout << "Bitset b1 modified by b1[3] = 1 is: ( " << b1 << " )" << endl; e = b1.test(3); if (e) cout << "The bit at position 3 of bitset b1 " << "has a value of 1." << endl; else cout << "The bit at position 3 of bitset b1 " << "has a value of 0." << endl; return; /* Original bitset b1(6) is: ( 00110 ) Note: positions in a bitset are numbered from the right starting with 0. The bit at position 3 of bitset b1 has a value of 0. Bitset b1 modified by b1[3] = 1 is: ( 01110 ) The bit at position 3 of bitset b1 has a value of 1. 请按任意键继续. . . */ } //一个bitset对象转换为字符串表示 void bitset_to_string(void) { using namespace std; bitset<5> b1(7); cout << "The ordered set of bits in the bitset<5> b1( 7 )" << "\n that was generated by the number 7 is: ( " << b1 << " )" << endl; string s1; s1 = b1.template to_string<char, char_traits<char>, allocator<char> >(); cout << "The string returned from the bitset b1" << "\n by the member function to_string( ) is: " << s1 << "." << endl; return; /* The ordered set of bits in the bitset<5> b1( 7 ) that was generated by the number 7 is: ( 00111 ) The string returned from the bitset b1 by the member function to_string( ) is: 00111. 请按任意键继续. . . */ } //一个bitset对象如果用于初始化位集,将产生包含比特序列的整数。 void bitset_to_ulong(void) { using namespace std; bitset<5> b1(7); cout << "The ordered set of bits in the bitset<5> b1( 7 )" << "\n that was generated by the number 7 is: ( " << b1 << " )" << endl; unsigned long int i; i = b1.to_ulong(); cout << "The integer returned from the bitset b1," << "\n by the member function to_long( ), that" << "\n generated the bits as a base two number is: " << i << "." << endl; return; /* The ordered set of bits in the bitset<5> b1( 7 ) that was generated by the number 7 is: ( 00111 ) The integer returned from the bitset b1, by the member function to_long( ), that generated the bits as a base two number is: 7. 请按任意键继续. . . */ }