如何为具有整数名称的变量设置位集

  • 本文关键字:变量 置位 整数 c++ bitset
  • 更新时间 :
  • 英文 :


我想将字符"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作为变量名。或者最好使用一个描述变量用途的名称。

最新更新