[笔记] 整理bitset的常用方法
参考链接
类模板 bitset 表示一个 N 位的固定大小序列。
初始化一个bitset
#include <iostream>
#include <bitset>
#include <string>
using std::string;
using std::bitset;
using std::cout;
using std::endl;
int main()
{
bitset<10> myBit; //定义一个默认的bitset对象,大小为10,值为默认值
bitset<10> myBit2 = 0b0110110111; //通过二进制数初始化一个bitset对象
bitset<10> myBit3 = 439; //通过整数值初始化bitset对象,和myBit2相同
bitset<10> myBit4(string("1101110010")); //通过字符串表示二进制数初始化bitset对象
string bitStr = "ABAABABABBBA";
bitset<10> myBit5(bitStr, 0, std::string::npos, 'A', 'B'); //使用字符初始化,并指定代替0和1的字符,A是0,B是1
return 0;
}
使用[]访问bitset的某一位元素
#include <iostream>
#include <bitset>
#include <string>
#include <cstdint>
using std::string;
using std::bitset;
using std::cout;
using std::endl;
int main()
{
bitset<10> myBit; //定义一个默认的bitset对象,大小为10,值为默认值
bitset<10> myBit2 = 0b0110110111; //通过二进制数初始化一个bitset对象
bitset<10> myBit3 = 439; //通过整数值初始化bitset对象,和myBit2相同
bitset<10> myBit4(string("1101110010")); //通过字符串表示二进制数初始化bitset对象
string bitStr = "ABAABABABB";
bitset<10> myBit5(bitStr, 0, std::string::npos, 'A', 'B'); //使用字符初始化,并指定代替0和1的字符,A是0,B是1
cout << "value of myBit:";
for(uint8_t idx = 0; idx < 10; idx++)
{
cout << myBit[idx]; //0
}
cout << endl;
cout << "value of myBit2:";
for(uint8_t idx = 0; idx < 10; idx++)
{
cout << myBit2[idx]; //0
}
cout << endl;
cout << "value of myBit3:";
for(uint8_t idx = 0; idx < 10; idx++)
{
cout << myBit3[idx]; //0
}
cout << endl;
cout << "value of myBit4:";
for(uint8_t idx = 0; idx < 10; idx++)
{
cout << myBit4[idx]; //0
}
cout << endl;
cout << "value of myBit5:";
for(uint8_t idx = 0; idx < 10; idx++)
{
cout << myBit5[idx]; //0
}
cout << endl;
return 0;
}
运行结果:
value of myBit:0000000000
value of myBit2:1110110110
value of myBit3:1110110110
value of myBit4:0100111011
value of myBit5:1101010010
修改元素的方式
- set(idx):将idx位设置为1
- set(idx, val):将idx位设置为val的值,1/0
- reset(idx):将idx位设置为0
- flip():将所有的位的值翻转
其他访问元素的方式
- test(idx):返回idx位对应的值1/0
- all():测试是否是所有位都被设置位1
- any():测试是否有任意一位被设置为1
- none():测试所有位都没有被设置为1,即所有位都是0
#include <iostream>
#include <bitset>
#include <string>
#include <cstdint>
using std::string;
using std::bitset;
using std::cout;
using std::endl;
int main()
{
bitset<10> myBit; //定义一个默认的bitset对象,大小为10,值为默认值
cout << "test value of myBit:";
for(uint8_t idx = 0; idx < 10; idx++)
{
cout << myBit.test(idx); //0
}
cout << endl;
myBit.set(3); //只有index 3的位置是1
cout << "set 3:" << myBit.test(3) << endl;
cout << "all:" << myBit.all() << " any:" << myBit.any() << " none:" << myBit.none() << endl;
myBit.reset(3); //index 3的值被设定位0
cout << "reset 3:" << myBit.test(3) << endl;
cout << "all:" << myBit.all() << " any:" << myBit.any() << " none:" << myBit.none() << endl;
myBit.flip(); //所有的值都是1
cout << "After flip test value of myBit:";
for(uint8_t idx = 0; idx < 10; idx++)
{
cout << myBit.test(idx);
}
cout << endl;
cout << "all:" << myBit.all() << " any:" << myBit.any() << " none:" << myBit.none() << endl;
return 0;
}
输出结果
test value of myBit:0000000000
set 3:1
all:0 any:1 none:0
reset 3:0
all:0 any:0 none:1
After flip test value of myBit:1111111111
all:1 any:1 none:0

浙公网安备 33010602011771号