比较c ++中的int和bitset

时间:2013-03-11 21:09:54

标签: c++ bitset

如何将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 个答案:

答案 0 :(得分:6)

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

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。