如何将 bitset 与整数进行比较?或者更一般地使用整数运算符:像这样:
#include <iostream>
#include <iomanip>
#include <bitset>
using namespace std;
int main()
{
bitset<4> _b1 = 3 ;
if(_b1>=2 )
cout<<_b1;
system("pause");
return 0;
}
if(_b1.to_ulong() >= 2)
有一种to_ulong
bitset 方法可以返回 a bitset
as的值unsigned long
。
您可以使用to_ulong
来获取位集的无符号整数值:
_b1.to_ulong()
这是参考。在您的情况下,它将:
if(_b1.to_ulong()>=2 )
cout<<_b1;
你也应该避免system("pause")。
您可以使用 to_ulong(),并与 long 进行比较,或者可能将 ulong 转换为 int。