📜  C++ boost :: dynamic_bitset类与示例

📅  最后修改于: 2021-05-30 15:19:55             🧑  作者: Mango

boost中有150多个库,其中一些最常用的库已经包含在C++标准库中。 dynamic_bitset是用于位操作的功能强大的库。 dynamic_bitset类用于表示0(重置)或1(集合)形式的一组位。 dynamic_bitset是对bitset( std :: bitsetboost :: bitset )的改进,后者在运行时分配任何所需的位长度,而bitset的位长度必须在编译时确定。

如果升压头升压/ dynamic_bitset.hpp中下找到来,dynamic_bitset。

句法:

boost::dynamic_bitset  B (N, num);

构造函数的参数是

  • N表示集合中所需的位数。
  • num表示将要存储其位的任何整数值。
  • uint8_t表示块大小(此处为8,如果我们不需要指定块大小,也可以为空)。

可以类似于位集索引运算符[]来访问dynamic_bitset的每个位。

请注意,长度为n的dynamic_bitset中数字B的位表示形式为:

B[n-1] B[n-2] ... B[1] B[0]

换句话说,dynamic_bitset中的索引以相反的顺序工作(类似于bitset)。 dynamic_bitset中的每个位由于其优化而恰好占用1位空间,因此使操作比布尔向量更快。

成员职能:
下面列出了可以在dynamic_bitset上执行的基本成员函数:

  • set() :为每个位分配1
  • 复位():它指定n,其中第零位,如果n是参数传递,否则它清除整个位集的对象。
  • flip() :反转任何给定的位,即将0切换为1,反之亦然
  • size() :返回dynamic_bitset对象的大小
  • resize() :用于增加或减小对象的大小
  • push_back() :将dynamic_bitset对象的大小增加一,并将该值压入MSB
  • pop_back() :从MSB中删除一位
  • num_blocks() :返回位集中的块数
  • append :将这些位追加到最高有效位。这通过bits_per_block增加了位集的大小。对于范围在[0,bits_per_block)中的i,位置(size + i)上的位设置为((value >> i)&1)
  • empty() :如果dynamic_bitset的长度为0,则返回true,否则返回false。
  • count() :返回dynamic_bitset中设置的位数。
  • all() :测试dynamic_bitset中的所有位是否都已设置,如果设置为true,则返回false。
  • any() :它测试是否设置了dynamic_bitset中的至少一位,如果它们设置为true,则返回false。
  • none() :测试是否未设置dynamic_bitset中的任何位,如果未设置任何位,则返回true,否则返回false。
  • 试验():它测试,如果i位被设置或没有。如果设置,则返回true,否则返回false。

范例1:

#include 
#include 
  
using namespace std;
  
int main(int argc, char* argv[])
{
    int bit_size = 8;
  
    // B1 is initialized with size 0
    // with all bits 0
    boost::dynamic_bitset<> B1;
  
    // B2 is initialized with size
    // bit_size with all bits 0
    boost::dynamic_bitset<> B2(bit_size);
  
    // B3 is initialized with size
    // bit_size and value 14
    boost::dynamic_bitset<> B3(bit_size, 14);
  
    // B4 is initialized with size
    // bit_size, value 14 and
    // block_size of 8 bits
    boost::dynamic_bitset B4(16, 84);
  
    // Empty
    cout << "Content of B1 is: "
         << B1 << endl;
  
    // 00000000
    cout << "Content of B2 is: "
         << B2 << endl;
    cout << "Binary representation of 14 in 8 bit: "
         << B3 << endl;
    cout << "Content of B4 is: "
         << B4 << endl
         << endl;
  
    // Setting 1 st of B2 to 1
    cout << "Content of B2 before set(): "
         << B2 << endl;
  
    B2.set(0);
    cout << "Content of B2 after set(0): "
         << B2 << endl;
  
    // Setting every bits of B22 to 1
    B2.set();
    cout << "Content of B2 after set(): "
         << B2 << endl;
  
    // Resetting 2nd bit to 0
    B2.reset(1);
    cout << "After resetting 2nd bit of B2: "
         << B2<< endl;
  
    // Resetting every bit to 0
    B2.reset();
    cout << "After resetting every bit of B2: "
         << B2 << endl;
  
    // Flipping first bit of B3
    cout << "Content of B3 before flip(): "
         << B3 << endl;
  
    B3.flip(0);
    cout << "Content of B3 after flip(0): "
         << B3 << endl;
  
    // Flipping every bits of B3
    B3.flip();
    cout << "Content of B3 after flip():  "
         << B3 << endl
         << endl;
  
    // Size of B1, B2, B3, B4
    cout << "Size of B1 is: "
         << B1.size() << endl;
    cout << "Size of B2 is: "
         << B2.size() << endl;
    cout << "Size of B3 is: "
         << B3.size() << endl;
    cout << "Size of B4 is: "
         << B4.size() << endl
         << endl;
  
    // Resizing B1 to size 4,
    // default bit-value 0
    B1.resize(4);
    cout << "B1 after increasing size to 4 bits: "
         << B1 << endl;
  
    // Resizing B1 to size 8, bit-value 1.
    // If num_bits > size() then the bits
    // in the range [0, size()) remain the same,
    // and the bits in [size(), num_bits)
    // are all set to value (here 1).
    B1.resize(8, 1);
    cout << "B1 after increasing size to 8 bits: "
         << B1 << endl;
  
    // Resizing B1 to size 1 i.e.
    // slicing [1, B1.size()-1)
    B1.resize(1);
    cout << "B1 after decreasing size to 1 bit:  "
         << B1 << endl
         << endl;
  
    // Pushing a set bit at MSB in B1
    B1.push_back(1);
    cout << "B1 after push(1) operation:   "
         << B1 << endl;
  
    // Pushing a reset bit at MSB in B1
    B1.push_back(0);
    cout << "B1 after push(0) operation : "
         << B1 << endl
         << endl;
  
    // Poping 1 bit from MSB in B1
    cout << "B1 before pop operation: "
         << B1 << endl;
  
    B1.pop_back();
    cout << "B1 after pop operation:   "
         << B1 << endl
         << endl;
  
    // Number of blocks = number of bits / block size
    cout << "Number of blocks in B4: "
         << B4.num_blocks() << endl
         << endl;
  
    // Checking if any bitset is empty
    cout << "B1 is "
         << (B1.empty() ? "empty" : "not empty")
         << endl;
    cout << "B2 is "
         << (B2.empty() ? "empty" : "not empty")
         << endl;
  
    // Resizing B3 to 0
    B3.resize(0);
    cout << "B3 is "
         << (B3.empty() ? "empty" : "not empty")
         << endl
         << endl;
  
    // Counting number of set bits in B4
    cout << "Content of B4 is: " << B4 << endl;
    cout << "Number of set bits in it are: "
         << B4.count() << endl
         << endl;
  
    // Checking if all of the bits of B2 is set
    B2.set(); // B2 => 11111111
  
    cout << "All bits in B2 are "
         << (B2.all() ? "set" : "not set") << endl;
  
    B2.reset(2); // B2 => 11111011
    cout << "All bits in B2 are "
         << (B2.all() ? "set" : "not set")
         << endl
         << endl;
  
    // Checking if any of the bits of B2 is set
    cout << (B2.any() ? "Atleast one" : "No")
         << " bit in B2 is set " << endl;
    B2.reset(); // B2 => 00000000
  
    cout << (B2.any() ? "Atleast one" : "No")
         << " bit in B2 is set " << endl
         << endl;
  
    // Checking if none of the bits of B2 are set
    // B2 => 00000000
    if (B2.none())
        cout << "None of the bits in B2 is set";
    else
        cout << "Atleast one bit in B2 is set";
    cout << endl
         << endl;
  
    // Testing if 1st bit of B1 is set or not
    cout << "Content of B1 is: " << B1 << endl;
    if (B1.test(1))
        cout << "B1[1] is set";
    else
        cout << "B1[1] is reset";
    return 0;
}
输出:
Content of B1 is: 
Content of B2 is: 00000000
Binary representation of 14 in 8 bit: 00001110
Content of B4 is: 0000000001010100

Content of B2 before set(): 00000000
Content of B2 after set(0): 00000001
Content of B2 after set(): 11111111
After resetting 2nd bit of B2: 11111101
After resetting every bit of B2: 00000000
Content of B3 before flip(): 00001110
Content of B3 after flip(0): 00001111
Content of B3 after flip():  11110000

Size of B1 is: 0
Size of B2 is: 8
Size of B3 is: 8
Size of B4 is: 16

B1 after increasing size to 4 bits: 0000
B1 after increasing size to 8 bits: 11110000
B1 after decreasing size to 1 bit:  0

B1 after push(1) operation:   10
B1 after push(0) operation : 010

B1 before pop operation: 010
B1 after pop operation:   10

Number of blocks in B4: 2

B1 is not empty
B2 is not empty
B3 is empty

Content of B4 is: 0000000001010100
Number of set bits in it are: 3

All bits in B2 are set
All bits in B2 are not set

Atleast one bit in B2 is set 
No bit in B2 is set 

None of the bits in B2 is set

Content of B1 is: 10
B1[1] is set

运营商:
一些有助于位操作的运算符:

  • 运算符[] :返回n位的引用。
  • operator&=() :它与当前的bitset对象和传入的bitset对象进行按位与运算。例如。 B1.operator&=(B2); B1和B2(即B1和B2)的按位与(B1和B2必须具有相同的位数)。
  • 运算符| =() :它与当前的bitset对象和传入的bitset对象进行按位或运算。例如。 B1。运算符| =(B2); B1和B2的按位或,即B1 | B2(B1和B2必须具有相同的位数)。
  • 运算符^ =() :使用当前的位集对象和传入参数的位集对象执行按位异或。例如。 B1。运算符^ =(B2); B1和B2的按位异或,即B1 ^ B2(B1和B2必须具有相同的位数)。
  • 运算符-=() :它与当前位集对象和传入参数的位集对象执行设置差异。例如。 B1。运算符-=(B2);设置B1和B2的差,即B1 – B2(B1和B2必须具有相同的位数)。
  • 运算符=() :将当前位集对象分配给传入参数的对象。
  • 运算符==() :它验证当前位集对象是否完全等于作为参数传递的对象。
  • 运算符!=() :验证当前位集对象是否不等于作为参数传递的对象。
  • 运算符<() :如果当前位集在字典上小于作为参数传递的bitet对象,则返回true,否则返回false。
  • 运算符>() :如果当前位集在字典上大于作为参数传递的bitet对象,则返回true,否则返回false。
  • 运算符<=() :如果当前位集在字典上小于或等于作为参数传递的bitet对象,则返回true,否则返回false。
  • 运算符> =() :如果当前位集在字典上大于或等于作为参数传递的bitet对象,则返回true,否则返回false。
  • 运算符〜() :它创建一个当前位集的副本,其所有位都被翻转。
  • 运算符<<() :创建当前位集对象的副本,该对象向左移动n位。
  • 运算符>>() :它创建当前位集对象的副本,该对象向右移动n位。
  • 运算符<< =() :将当前位集对象向左移动n位。
  • 运算符>> =() :将当前位集对象右移n位。

范例2:

#include 
#include 
using namespace std;
  
int main(int argc, char* argv[])
{
    int n_bits = 8;
  
    boost::dynamic_bitset<> B1(n_bits, 123);
    boost::dynamic_bitset<> B2(n_bits, 206);
    boost::dynamic_bitset<> temp(4, 3);
  
    cout << "Binary representation of 123: "
         << B1 << endl;
    cout << "Binary representation of 206: "
         << B2 << endl
         << endl;
  
    // Operator[] is used to access an individual index
    // It is a reference of the nth bit. It can be used for
    // assignment of a boolean value at nth bit
    cout << "4th bit of B1 consist: "
         << B1[3] << endl;
    B1[3] = 0;
    cout << "Assigning 0 to 4th bit of B1: "
         << B1 << endl
         << endl;
  
    // Operator= assigns on current bitset object
    cout << "temp before assignment: "
         << temp << endl;
  
    temp.operator=(B1);
    cout << "temp after assignment with B1: "
         << temp << endl
         << endl;
  
    // Operator&= performs bitwise-AND
    cout << "B1 consist of: "
         << B1 << endl;
    cout << "B2 consist of: "
         << B2 << endl;
  
    temp.operator=(B1);
    temp.operator&=(B2);
    cout << "B1 AND B2 is : & "
         << temp << endl
         << endl;
  
    // Operator|= performs bitwise-OR
    cout << "B1 consist of: "
         << B1 << endl;
    cout << "B2 consist of: "
         << B2 << endl;
  
    temp.operator=(B1);
    temp.operator|=(B2);
    cout << "B1 OR B2 is  : | "
         << temp << endl
         << endl;
  
    // Operator^= preforms bitwise-XOR
    cout << "B1 consist of: "
         << B1 << endl;
    cout << "B2 consist of: "
         << B2 << endl;
  
    temp.operator=(B1);
    temp.operator^=(B2);
    cout << "B1 XOR B2 is : ^ "
         << temp << endl
         << endl;
  
    // Operator-= performs set difference
    cout << "B1 consist of: "
         << B1 << endl;
    cout << "B2 consist of: "
         << B2 << endl;
  
    temp.operator=(B1);
    temp.operator-=(B2);
    cout << "Set differ is:   "
         << temp << endl
         << endl;
  
    // Operator== checks if bitset object is equal to
    // another one, bit length has to be same for true
    cout << "dynamic_bitset B1 and B2 are "
         << (operator==(B1, B2) ? "equal" : "not equal")
         << endl;
  
    boost::dynamic_bitset<> B3(2, 0), B4(3, 0);
    cout << "Content of B3: " << B3 << endl
         << "Content of B4: " << B4 << endl
         << "dynamic_bitset B3 and B4 are "
         << (operator==(B3, B4) ? "equal" : "not equal")
         << endl;
  
    B3.operator=(B4);
    cout << "dynamic_bitset B3 and B4 are: "
         << (operator==(B3, B4) ? "equal" : "not equal")
         << endl
         << endl;
  
    // Operator!= checks if bitset object is unequal
    cout << "dynamic_bitset B1 and B2 are ";
    cout << (operator!=(B1, B2) ? "not equal" : "equal")
         << endl
         << endl;
  
    // Operator< checks if first bitset object is
    // lexicographically less than second one
    cout << "B1 consist of: " << B1 << endl;
    cout << "B2 consist of: " << B2 << endl;
  
    if (operator<(B1, B2))
        cout << "B1 is lexicographically less than B2";
    else
        cout << "B2 is lexicographically greater than B2";
    cout << endl
         << endl;
  
    // Operator> checks if first bitset object is
    // lexicographically greater than second one
    cout << "B1 consist of: " << B1 << endl;
    boost::dynamic_bitset<> B5(8, 0);
    cout << "B5 consist of: " << B5 << endl;
  
    if (operator>(B1, B5))
        cout << "B1 is lexicographically greater than B5";
    else
        cout << "B1 is lexicographically less than B5";
    cout << endl
         << endl;
  
    // Operator<= checks if first bitset object is
    // lexicographically less than or equal to second one
    cout << "B3 is lexicographically ";
    if (operator<=(B3, B3))
        cout << "less than or equal to B3"
             << endl
             << endl;
    else
        cout << "greater than B3"
             << endl
             << endl;
  
    // Operator>=
    cout << "B5 consist of: " << B5 << endl;
    cout << "B2 consist of: " << B2 << endl;
    cout << "B5 is lexicographically ";
    if (operator>=(B5, B2))
        cout << "greater than or equal to B2";
    else
        cout << "less than B2";
    cout << endl
         << endl;
  
    // Operator~ creates a copy of flipped bitset object
    cout << "Value of dynamic_bitset B4 : " << B4 << endl;
    cout << "Creating flipped copy of B4: ";
    cout << B4.operator~() << endl
         << endl;
  
    // Operator<< creates a copy of current bitset object
    // which is shifted to left by n times
    cout << "Value of dynamic_bitset B2: " << B2 << endl;
    cout << "Copy of B2 left shift 3 times is  : ";
    cout << B2.operator<<(3) << endl;
  
    // Operator>> creates a copy of current bitset object
    // which is shifted to right by n times value of B2
    // is not changed, the copy is displayed
    cout << "Copy of B2 right shift 1 time is  : ";
    cout << B2.operator>>(1) << endl
         << endl;
  
    // Operator<<= shifts the current bitset object
    // n times to left
    cout << "Value of dynamic_bitset B2: "
         << B2 << endl;
    cout << "B2 left shift 3 times is  : ";
    cout << B2.operator<<=(2) << endl;
  
    // Operator>>= shifts current bitset object
    // n times value to right
    cout << "B2 right shift 1 time is  : ";
    cout << B2.operator>>=(3);
  
    return 0;
}
输出:
Binary representation of 123: 01111011
Binary representation of 206: 11001110

4th bit of B1 consist: 1
Assigning 0 to 4th bit of B1: 01110011

temp before assignment: 0011
temp after assignment with B1: 01110011

B1 consist of: 01110011
B2 consist of: 11001110
B1 AND B2 is : & 01000010

B1 consist of: 01110011
B2 consist of: 11001110
B1 OR B2 is  : | 11111111

B1 consist of: 01110011
B2 consist of: 11001110
B1 XOR B2 is : ^ 10111101

B1 consist of: 01110011
B2 consist of: 11001110
Set differ is:   00110001

dynamic_bitset B1 and B2 are not equal
Content of B3: 00
Content of B4: 000
dynamic_bitset B3 and B4 are not equal
dynamic_bitset B3 and B4 are: equal

dynamic_bitset B1 and B2 are not equal

B1 consist of: 01110011
B2 consist of: 11001110
B1 is lexicographically less than B2

B1 consist of: 01110011
B5 consist of: 00000000
B1 is lexicographically greater than B5

B3 is lexicographically less than or equal to B3

B5 consist of: 00000000
B2 consist of: 11001110
B5 is lexicographically less than B2

Value of dynamic_bitset B4 : 000
Creating flipped copy of B4: 111

Value of dynamic_bitset B2: 11001110
Copy of B2 left shift 3 times is  : 01110000
Copy of B2 right shift 1 time is  : 01100111

Value of dynamic_bitset B2: 11001110
B2 left shift 3 times is  : 00111000
B2 right shift 1 time is  : 00000111

应用领域

  • dynamic_bitset可以有效地用于表示有限集的子集。每个位代表有限集的元素是否在子集中。
  • Eratosthenes的筛子,用于查找整数N以下的所有素数。

参考: https : //www.boost.org/doc/libs/1_36_0/libs/dynamic_bitset/dynamic_bitset.html

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”