BitSet可以看作是二进制位的容器,并提供了位的相关操作函数。
常用函数
(1)构造、赋值函数
bitset()
bitset(constbitset&) ;拷贝构造函数
bitset(unsignedlongval) ;由无符号长整形数构建位容器
bitset(conststring&str,size_tpos= 0,size_tn = -1);由字符串创建位容器
bitset&operator=(constbitset&);赋值操作
(2)逻辑运算操作(与、或、非)
bitset&operator&=(constbitset&)
bitset&operator|=(constbitset&)
bitset&operator^=(constbitset&)
bitset&operator<<=(size_t)
bitset&operator>>=(size_t)
bitsetoperator<<(size_tn)const
bitsetoperator>>(size_tn)const
bitsetoperator&(constbitset&,constbitset&)
bitsetoperator|(constbitset&,constbitset&)
bitsetoperator^(constbitset&,constbitset&)
(3)其他操作函数
string toString() ;位容器内容转化成字符串,方便显示
size_tsize()const ;返回位容器大小
size_tcount()const ;返回设置1位个数
boolany()const ;是否有位设置1
boolnone()const ;是否没有位设置1
booltest(size_tn)const ;测试某位是否为1
booloperator[](size_tn)const ;随机访问位元素
unsigned longto_ulong()const ;若没有溢出异常,返回无符号长整形数
bitset&set() ;位容器所有位置1
bitset&flip() ;位容器所有位翻转
bitset&reset() ;位容器所有位置0
bitset&set(size_tn,intval= 1) ;设置某位为1或0,缺省1
bitset&reset(size_tn); ;复位某位为0
bitsetflip(size_tn); ;翻转某位
<span style="font-size:14px;">#include<iostream>
#include<string>
#include<bitset>
using namespace std;
int main()
{
bitset<5>s1;
cout<<"初始内存空间内容:"<<s1.to_string()<<endl;
cout<<"位容器空间(size):"<<s1.size()<<"\t置1个数(cout):"<<s1.count()<<endl;
s1.set(2,true);
cout<<s1.to_string()<<endl;
s1[3]=1;
cout<<s1.to_string()<<endl;
s1.set(); //所有位置1
cout<<s1.to_string()<<endl;
bitset<16>s2(65535);
cout<<s2.to_string()<<endl;
// bitset<5>s3("1111101",2,5);
return 0;
}</span>
(1)结合结果体会size()和count()函数区别,set无参、有参函数区别,以及可以用[]随机访问位容器的某一位。同时思考一下reset无
参、有参函数区别,flip无参、有参函数区别。
(2)当用长整形数建立位容器时,范围应在[0,)范围内,若位容器设置位数N不足以容纳长整形数,则仅截取假想已是二进制数的低N
位。例如:bit<2>s4(13),由于13二进制表示是1101,而容器仅两位,从低位开始填充,位容器内容为01。
(3)当用字符串建立位容器bitset(conststring&str,size_tpos= 0,size_tn = -1)含义是:从字符串第pos位置字符开始,截取n个字符填充
位容器,缺省设置是填充全字符串。但要注意字符串只能包含“1”或“0”字符,不能有其它字符。如“10101”正确,而“12345”错误。
位操作函数简单示例
<span style="font-size:14px;">#include <iostream>
#include <string>
#include <bitset>
using namespace std;
void main()
{
bitset<5> s1("01011");
bitset<5> s2("10010");
cout << "s1:" << s1.to_string() << "\t";
cout << "s2:" << s2.to_string() << endl;
s1 &= s2; //结果修改了s1
cout << " s1&=s2: " << s1.to_string() << endl;
bitset<5> s3("01011");
bitset<5> s4("10010");
bitset<5> s5 = s3 & s4;//与后结果赋值给s5,s3及s4不变
cout << "s3:" << s3.to_string() << "\t";
cout << "s4:" << s4.to_string() << endl;
cout << " s5=s3&s4: " <<s5.to_string() << endl;
cout << " 原s3: " << s3.to_string() << endl;
}</span><span style="font-size: 18px;">
</span>
(1)本示例主要区分&=、&操作符的差别。&=操作符完成与操作同时,修改了第1个操作数。&操作符完成与操作,并不修改原操作数。
其实从原型定义上也可以看出来:bitset&operator&=(constbitset&)返回当前位容器对象引用,因此当前位容器对象内容可能已发生了
变化;bitsetoperator&(constbitset&,constbitset&)返回位容器对象的拷贝,且传入的两个与操作位容器对象类型是const,因此原容器
内容一定不发生变化。依此类推,可知|,|=, ^, ^=, <<, <<=, >>, >>=的功能差别。
(2)若两个位容器对象进行各种操作,必须保证位容器大小相同,示例中容器大小均是5,所以可以进行操作。但如果bitset<5>s1,
bitset<6>s2,则s1,s2不能进行各种操作。
#include<iostream>
#include <string>
#include <bitset>
#include <vector>
using namespace std;
template <size_t N>
class MyAttend
{
int month;
bitset<N>b;
public:
MyAttend(int m,string str):month(m),b(str){}
int GetMonth()
{
return month;
}
long GetAttendDays()
{
return b.count();
}
};
class Student
{
string name;
vector<MyAttend<31>>v;
public:
Student(string na):name(na){}
void Add(MyAttend<31>&m)
{
v.push_back(m);
}
void show()
{
cout<<"姓名:"<<name<<endl;
cout<<"月份\t出勤天数"<<endl;
for(int i=0;i<v.size();i++)
{
MyAttend<31>&m=v.at(i);
int month=m.GetMonth();
long days=m.GetAttendDays();
cout<<month<<"\t"<<days<<endl;
}
}
};
int main()
{
Student stu1("张三");
string s1="1110101010101011001000110101010";
string s2="111100001110111011011101111";
MyAttend<31>m1(1,s1);
MyAttend<31>m2(2,s2);
stu1.Add(m1);
stu1.Add(m2);
stu1.show();
return 0;
}