bitset的相关函数
foo.size() 返回大小(位数)foo.count() 返回1的个数foo.any() 返回是否有1foo.none() 返回是否没有1foo.set() 全都变成1foo.set(p) 将第p + 1位变成1foo.set(p, x) 将第p + 1位变成xfoo.reset() 全都变成0foo.reset(p) 将第p + 1位变成0foo.flip() 全都取反foo.flip(p) 将第p + 1位取反foo.to_ulong() 返回它转换为unsigned long的结果,如果超出范围则报错foo.to_ullong() 返回它转换为unsigned long long的结果,如果超出范围则报错foo.to_string() 返回它转换为string的结果
#include<iostream>
#include<bitset>
using namespace std;
int main(){
string s="0011";
string a="1001";
bitset<4>cont(s);//0011
bitset<4>foo(a);//1001
cout<<(cont^=foo)<<endl;//1010
cout<<(cont&=foo)<<endl;//1000
cout<<(cont|=foo)<<endl;//1001
cout<<(cont<<=2)<<endl;//0100
cout<<(cont>>=1)<<endl;//0010
cout<<(~cont)<<endl;//1101取反但不赋值
cout<<(cont==foo)<<endl;
cout<<cont<<endl;
cout<<cont.count()<<endl;
cout<<cont.size()<<endl;
cout<<cont<<endl;
cout<<cont[0];
cout<<cont[1];
cout<<cont[2];
cout<<cont[3]<<endl;
cout<<cont.test(2)<<endl;
cout<<cont<<endl;
cout<<cont.reset()<<endl;
cout<<cont.set(0)<<endl;//第0位是最右
}
本文详细介绍了C++中的bitset类及其相关函数,如size(),count(),set(),flip()等,并通过示例展示了如何使用这些函数进行位操作和变量转换。
114

被折叠的 条评论
为什么被折叠?



