0

如何将 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;

}

4

4 回答 4

6

使用std::bitset<N>::to_ulong()

if(_b1.to_ulong() >= 2)
于 2013-03-11T21:11:34.120 回答
1

有一种to_ulongbitset 方法可以返回 a bitsetas的值unsigned long

于 2013-03-11T21:12:03.787 回答
1

您可以使用to_ulong来获取位集的无符号整数值:

 _b1.to_ulong() 

这是参考。在您的情况下,它将:

if(_b1.to_ulong()>=2 )
   cout<<_b1;

你也应该避免system("pause")

于 2013-03-11T21:12:12.020 回答
0

您可以使用 to_ulong(),并与 long 进行比较,或者可能将 ulong 转换为 int。

于 2013-03-11T21:14:30.203 回答