如何将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;
}
答案 0 :(得分:6)
if(_b1.to_ulong() >= 2)
答案 1 :(得分:1)
bitset的方法to_ulong
会将bitset
的值返回为unsigned long
。
答案 2 :(得分:1)
您可以使用to_ulong
来获取bitset的无符号整数值:
_b1.to_ulong()
这是ref。在你的情况下,它会:
if(_b1.to_ulong()>=2 )
cout<<_b1;
此外,您应该避免使用system("pause")。
答案 3 :(得分:0)
您可以使用to_ulong(),并与long进行比较,或者可能将ulong转换为int。