比较c++中的int和bitset

Compare int and bitset in c++

本文关键字:bitset int 中的 c++ 比较      更新时间:2023-10-16

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

}

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

if(_b1.to_ulong() >= 2)

bitset的to_ulong方法返回bitset的值为unsigned long

您可以使用to_ulong来获取bitset的unsigned int值:

 _b1.to_ulong() 

这里是ref.在你的例子中,它会:

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

还应该避免system("pause")

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