我想将字符"0"的位集设置为0101010101,但当我尝试时,我得到了错误"expected an identifier">
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
int main() {
bitset<8> '0'=0101010101;
}
我也试过
bitset <8> 0(string("0101010101"));
但是我得到了相同的错误
您可以使用unordered_map
设置int和位集之间的一对一映射。样本0101010101
的长度是10,因此比特集的大小将是10,并且0101010101
=十进制的341
。
#include <iostream>
#include <unordered_map>
#include <bitset>
std::unordered_map<int, std::bitset<10>> M {
{0, 341},
{1, ...},
...
...
...
};
int main()
{
std::cout << M[0] << std::endl;
}
0
是int文字,'0'
是char文字,两者都不是变量名。您可以使用_0
作为变量名。或者最好使用一个描述变量用途的名称。