bitset的用法

    科技2024-07-12  83

    bitset,是一个仅含0/1的一个数组。但是它的每个位置只占1bit。 主要用于快速的集合位运算。

    #include <iostream> #include <bitset> //使用时需要声明 using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); //bitset<N> s N为数组的大小 bitset<23> s1(4); bitset<23> s2(5); for (int i = 0; i < 23; i++) { cout << s1[i] << ' '; } cout << '\n'; //bitset支持所有的位运算,复杂度为N/32 s1 = s1 ^ s2; s1[20] = 1; //直接对某一位赋值 for (int i = 0; i < 23; i++) { cout << s1[i] << ' '; } s1.size(); //返回大小(位数) s1.count(); //返回1的个数 s1.set(); //全都变成1 s1.reset(); //全都变成0 s1.flip(); //全都取反 return 0; }
    Processed: 0.010, SQL: 9