📜  C++位集及其应用

📅  最后修改于: 2021-05-25 05:45:40             🧑  作者: Mango

位集是布尔数组,但每个布尔值均未单独存储,而是通过位空间优化空间,使得每个布尔仅占用1位空间,因此位集bs占用的空间小于布尔bs [N]和向量bs( N) 。但是,位集的限制是,必须在编译时知道N,即常量(此限制在矢量和动态数组中不存在)

由于位集以压缩方式存储相同的信息,因此对位集的操作要比数组和向量的操作快。我们可以借助数组索引运算符[]来单独访问位集的每个位,即bs [3]就像简单数组一样显示位集bs的索引3处的位。请记住,bitset开始向后索引,即10110,0位于第0和第3个索引,而1位于第1第2和第4索引。
我们可以通过构造函数使用整数和二进制字符串构造一个位集,如下面的代码所示。位集的大小在编译时是固定的,即不能在运行时更改。
为位集类定义主要函数是运算符[],计数,大小,设置,重置等,下面的代码将对它们进行更多说明–

// C++ program to demonstrate various functionality of bitset
#include 
using namespace std;
  
#define M 32
  
int main()
{
    // default constructor initializes with all bits 0
    bitset bset1;
  
    // bset2 is initialized with bits of 20
    bitset bset2(20);
  
    // bset3 is initialized with bits of specified binary string
    bitset bset3(string("1100"));
  
    // cout prints exact bits representation of bitset
    cout << bset1 << endl; // 00000000000000000000000000000000
    cout << bset2 << endl; // 00000000000000000000000000010100
    cout << bset3 << endl; // 00000000000000000000000000001100
    cout << endl;
  
    // declaring set8 with capacity of 8 bits
  
    bitset<8> set8; // 00000000
  
    // setting first bit (or 6th index)
    set8[1] = 1; // 00000010
    set8[4] = set8[1]; // 00010010
    cout << set8 << endl;
  
    // count function returns number of set bits in bitset
    int numberof1 = set8.count();
  
    // size function returns total number of bits in bitset
    // so there difference will give us number of unset(0)
    // bits in bitset
    int numberof0 = set8.size() - numberof1;
  
    cout << set8 << " has " << numberof1 << " ones and "
         << numberof0 << " zeros\n";
  
    // test function return 1 if bit is set else returns 0
    cout << "bool representation of " << set8 << " : ";
    for (int i = 0; i < set8.size(); i++)
        cout << set8.test(i) << " ";
  
    cout << endl;
  
    // any function returns true, if atleast 1 bit
    // is set
    if (!set8.any())
        cout << "set8 has no bit set.\n";
  
    if (!bset1.any())
        cout << "bset1 has no bit set.\n";
  
    // none function returns true, if none of the bit
    // is set
    if (!bset1.none())
        cout << "bset1 has some bit set\n";
  
    // bset.set() sets all bits
    cout << set8.set() << endl;
  
    // bset.set(pos, b) makes bset[pos] = b
    cout << set8.set(4, 0) << endl;
  
    // bset.set(pos) makes bset[pos] = 1  i.e. default
    // is 1
    cout << set8.set(4) << endl;
  
    // reset function makes all bits 0
    cout << set8.reset(2) << endl;
    cout << set8.reset() << endl;
  
    // flip function flips all bits i.e.  1 <-> 0
    // and  0 <-> 1
    cout << set8.flip(2) << endl;
    cout << set8.flip() << endl;
  
    // Converting decimal number to binary by using bitset
    int num = 100;
    cout << "\nDecimal number: " << num
         << "  Binary equivalent: " << bitset<8>(num);
  
    return 0;
}

输出 :

00000000000000000000000000000000
00000000000000000000000000010100
00000000000000000000000000001100

00010010
00010010 has 2 ones and 6 zeros
bool representation of 00010010 : 0 1 0 0 1 0 0 0 
bset1 has no bit set.
11111111
11101111
11111111
11111011
00000000
00000100
11111011

Decimal number: 100 Binary equivalent: 01100100

对于位设置,定义了复位和翻转函数。设置函数设置(1)如果未提供任何参数,则设置bitset的所有位,否则设置其位置作为参数给出的位。以相同的方式,如果没有参数调用它们,复位和翻转也将起作用,它们将在整个位集上执行其操作;如果提供某个位置作为参数,则它们仅在该位置上执行操作。
对于位集,所有按位运算运算符都是重载的,即它们可以直接应用于位集,而无需任何转换或转换,主要的重载运算符是&,|,==,!=和移位运算符<>,这使对位集的操作变得容易。
上面的运算符的用法显示在下面的代码中。

// C++ program to show applicable operator on bitset.
#include 
using namespace std;
  
int main()
{
    bitset<4> bset1(9); // bset1 contains 1001
    bitset<4> bset2(3); // bset2 contains 0011
  
    // comparison operator
    cout << (bset1 == bset2) << endl; // false 0
    cout << (bset1 != bset2) << endl; // true  1
  
    // bitwise operation and assignment
    cout << (bset1 ^= bset2) << endl; // 1010
    cout << (bset1 &= bset2) << endl; // 0010
    cout << (bset1 |= bset2) << endl; // 0011
  
    // left and right shifting
    cout << (bset1 <<= 2) << endl; // 1100
    cout << (bset1 >>= 1) << endl; // 0110
  
    // not operator
    cout << (~bset2) << endl; // 1100
  
    // bitwise operator
    cout << (bset1 & bset2) << endl; // 0010
    cout << (bset1 | bset2) << endl; // 0111
    cout << (bset1 ^ bset2) << endl; // 0101
}

输出 :

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