ACM模板——bitset小结

对于一个叫做a的bitset:
a.size()      返回大小(位数)
a.count()     返回1的个数
a.any()       返回是否有1
a.none()      返回是否没有1
a.set()       全都变成1
a.set(p)      将第p+1位变成1
a.set(p, x)   将第p+1位变成x
a.reset()     全都变成0
a.reset(p)    将第p+1位变成0
a.flip()      全都取反
a.flip(p)     将第p+1位取反
a.to_ulong()  返回它转换为unsigned long的结果,如果超出范围则报错
a.to_ullong() 返回它转换为unsigned long long的结果,如果超出范围则报错
a.to_string() 返回它转换为string的结果
bitset<4> a ( string("1001"));
bitset<4> b ( string("0011"));

cout << (a^=b) << endl;       // 1010 
cout << (a&=b) << endl;       // 0010
cout << (a|=b) << endl;       // 0011 

cout << endl << a << endl;    // 0011
cout << b << endl << endl;    // 0011

cout << (a<<=2) << endl;      // 1100 
cout << (a>>=1) << endl;      // 0110

cout << endl << a << endl;    // 0110
cout << b << endl << endl;    // 0011

cout << (~b) << endl;         // 1100 
cout << (b<<1) << endl;       // 0110 
cout << (b>>1) << endl;       // 0001

cout << (a==b) << endl;       // false (0110==0011)
cout << (a!=b) << endl;       // true  (0110!=0011)

cout << (a&b) << endl;        // 0010
cout << (a|b) << endl;        // 0111
cout << (a^b) << endl;        // 0101

猜你喜欢

转载自www.cnblogs.com/Asurudo/p/11619279.html