有些程序要处理二进制位的有序集,每个位可能包含的是0(关)或1(开)的值。位是用来保存一组项或条件的yes/no信息(有时也称标志)的简洁方法。标准库提供了bitset类使得处理位集合更容易一些。要使用bitset类就必须要包含相关的头文件。
#include<bitset>
using std::bitset;
bitset操作函数:
bitset使用具体实例:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<bitset>
using namespace std;
const int maxn=16;
int main()
{
bitset<16> b2;
cout<<"b2.any() = "<<b2.any()<<endl;
cout<<"b2.none()= "<<b2.none()<<endl;
cout<<"b2.size()= "<<b2.size()<<endl;
cout<<"b2[4]= "<<b2[4]<<endl;
cout<<"b2.test(4)= "<<b2.test(4)<<endl;
b2.set() ; cout<<endl;
cout<<"b2.any() = "<<b2.any()<<endl;
cout<<"b2.none()= "<<b2.none()<<endl;
cout<<"b2.size()= "<<b2.size()<<endl;
cout<<"b2[4]= "<<b2[4]<<endl;
cout<<"b2.test(4)= "<<b2.test(4)<<endl;
b2.reset() ; cout<<endl;
cout<<"b2.any() = "<<b2.any()<<endl;
cout<<"b2.none()= "<<b2.none()<<endl;
cout<<"b2.size()= "<<b2.size()<<endl;
cout<<"b2[4]= "<<b2[4]<<endl;
cout<<"b2.test(4)= "<<b2.test(4)<<endl;
b2.set(4) ; cout<<endl;
cout<<"b2[4]= "<<b2[4]<<endl;
cout<<"b2.test(4)= "<<b2.test(4)<<endl;
return 0;
}