bitset类学习(补充)

来源:互联网 发布:希尔瓦娜斯 知乎 编辑:程序博客网 时间:2024/04/29 13:13
// Bitset.cpp : 定义控制台应用程序的入口点。//VS 2005 测试#include "stdafx.h"#include <iostream>#include <bitset>#include <typeinfo>using namespace std;namespace X_BitSet{template<size_t N>class MyBitset{public://reference 类class reference{friend reference;reference();/*这里把 reference 的构造函数声明为私有,那么在外部就不能定义reference的对象为什么要加入reference类?bitset的取值也是一种结构,reference类就提供对这个结构的操作。*/public:~reference();reference& operator=(bool x);//用于b[i]=xreference& operator=(reference&);//用于b[i] = b[j]bool operator ~()const;//返回~b[i];operator bool()const;//用于x = b[i]?????reference& flip();//b[i].flip();};//构造函数MyBitset();//N个二进制位0MyBitset(unsigned long val);///*借助下面的构造函数思考下模板的参数演绎问题,bt = bitset<4>(strBinary);//ok--------bt[4](0,1,1,1)调用的就是下面的构造函数。str => template<class Ch,class Tr, class A>str在没有模板参数的情况下可以演绎出 template<class Ch,class Tr, class A>,这与:模板参数演绎过程中不允许进行自动类型转换不冲突。所以自动演绎应该是没问题的。(个人理解,仅供参考)*/template<class Ch, class Tr, class A>explicit MyBitset(const basic_string<Ch,Tr,A>& str, typename basic_string<Ch,Tr,A>::size_type pos = 0,typename basic_string<Ch,Tr,A>::size_type n = basic_string<Ch,Tr,A>::npos);//bitset操作reference operator[](size_t pos);//b[i]MyBitset& operator&=(const MyBitset& s);//与MyBitset& operator|=(const MyBitset& s);//或MyBitset& operator^=(const MyBitset& s);//异或MyBitset& operator<<(size_t n);//逻辑左移(填充0)MyBitset& operator>>(size_t n);//逻辑右移(填充0)MyBitset& set();//将所有位置为1MyBitset& set(size_t pos, int val = 1);//b[pos] = val;MyBitset& reset();//所有位设置为0MyBitset& reset(size_t pos);//b[pos] = 0注意:这里跟set不同MyBitset& flip();//改变没个位的值MyBitset& flip(size_t pos);//改变 b[pos] 的值MyBitset operator~()const{return MyBitset<N>::(*this).flip();}//做出补集注意:这里返回的不是bitset的引用MyBitset operator<<(size_t n)const{return (*this)<<n;}MyBitset operator>>(size_t n)const{return (*this)>>n;}//其他操作unsigned long to_long()const;template<class Ch,class Tr,class A>basic_string<Ch,Tr,A> to_string()const;size_t size()const;//位数size_t count()const;//值为1的二进制位数bool operator==(MyBitset<N>& s)const;bool operator!=(MyBitset<N>& s)const;bool test(size_t pos)const;//如果 b[pos] 为1则为truebool any()const;//如果任何位为1则为truebool none()const;//如果没有位为1则为true};}int _tmain(int argc, _TCHAR* argv[]){string strBinary;strBinary.assign("11101011");//[0] = 1 ; [1] = 1 ; [2] = 1 ; [3] = 0.....bitset<4> bt,bt2;try{//字符串构造bt = bitset<4>(strBinary);//ok--------bt[4](0,1,1,1)bt = bitset<4>(strBinary,1,2);//ok--------bt[4](1,1,0,0)bt = bitset<4>(strBinary,1,1);//ok--------bt[4](1,0,0,0)bt = bitset<4>(strBinary,1,0);//ok--------bt[4](0,0,0,0)//bt = bitset<4>(strBinary,10,13);//error throw out_of_range::invalid bitset<N> positionstrBinary.assign("abc123");//bt = bitset<4>(strBinary);//error throw invalid_argument::invalid bitset<N> char//unsiged long 构造bt = 0x0e;//ok--------bt[4](0,1,1,1)bt = 0x01;//ok--------bt[4](1,0,0,0)//bitset操作bt.flip();//ok--------bt[4](0,1,1,1)//bt.flip(4);//error throw out_of_range::invalid bitset<N> positionbt.reset(3);//ok--------bt[4](0,1,1,0)bt.set(2);//ok--------bt[4](0,1,1,0)bt2 = bt;//ok--------bt2[4](0,1,1,0)bt2 = bt << 1;//ok--------bt2[4](0,0,1,1)/*下面的两个操作用到的是reference类的成员函数*/bt2.at(0).flip();//ok--------bt2[4](1,0,1,1)bt2.at(3) = ~bt2[3];//ok--------bt2[4](1,0,1,0)//其他操作bt[4](0,1,1,0)size_t bSize = bt.size();// = 4size_t bCount = bt.count();// = 2 bool bAny = bt.any();// = truebool bNone = bt.none();// = falseunsigned long to_long = bt.to_ulong();// = 6string strToString = bt.to_string();// = "0110"bt.set(3);//ok--------bt[4](0,1,1,1)strToString = bt.to_string();// = "1110"//size_t s = sizeof bt;// = 4bitset<100> bt100;bitset<1> bt1;s = sizeof bt100;//16s = sizeof bt1;//4cout << typeid(bt).name() << endl;//class std::bitset<4>cout << typeid(bt.at(0)).name() << endl;//class std::bitset<4>::reference}catch(out_of_range& o){cout << o.what() << endl;}catch(invalid_argument& a){cout << a.what() << endl;}catch(overflow_error& ov){cout << ov.what() << endl;}system("pause");return 0;}

0 0
原创粉丝点击