一二三四五 上山打老虎

bitset-c++

参考博客链接:http://c.biancheng.net/view/406.html

初始化

include

bitset<30> ans;
表示定义了一个30位的bitset类型对象,初始值为0
ans[0]~ans[29]

bitset 有许多成员函数,有些成员函数执行的就是类似于位运算的操作。bitset 成员函数列表如下:
bitset & operator &= (const bitset & rhs); //和另一个 bitset 对象进行与操作
bitset & operator |= (const bitset & rhs); //和另一个 bitset 对象进行或操作
bitset & operator ^= (const bitset & rhs); //和另一个 bitset 对象进行异或操作
bitset & operator <<= (size_t num); //左移 num 位
bitset & operator >>= (size_t num); //右移 num 位
bitset & set(); //将所有位全部设成 1
bitset & set(size_t pos, bool val = true); //将第 pos 位设为 val
bitset & reset(); //将所有位全部设成0
bitset & reset (size_t pos); //将第 pos 位设成 0
bitset & flip(); //将所有位翻转(0变成1,1变成0)
bitset & flip(size_t pos); //翻转第 pos 位
reference operator[] (size_t pos); //返回对第 pos 位的引用
bool operator[] (size_t pos) const; //返回第 pos 位的值
reference at(size_t pos); //返回对第 pos 位的引用
bool at (size_t pos) const; //返回第 pos 位的值
unsigned long to_ulong() const; //将对象中的0、1串转换成整数
string to_string () const; //将对象中的0、1串转换成字符串(Visual Studio 支持,Dev C++ 不支持)
size_t count() const; //计算 1 的个数
size_t size () const; //返回总位数
bool operator == (const bitset & rhs) const;
bool operator != (const bitset & rhs) const;
bool test(size_t pos) const; //测试第 pos 位是否为 1
bool any() const; //判断是否有某位为1
bool none() const; //判断是否全部为0
bitset operator << (size_t pos) const; //返回左移 pos 位后的结果
bitset operator >> (size_t pos) const; //返回右移 pos 位后的结果
bitset operator ~ (); //返回取反后的结果
bitset operator & (const bitset & rhs) const; //返回和另一个 bitset 对象 rhs 进行与运算的结果
bitset operator | (const bitset & rhs) const; //返回和另一个 bitset 对象 rhs 进行或运算的结果
bitset operator ^ (const bitset & rhs) const; //返回和另一个 bitset 对象 rhs 进行异或运算的结果

posted @ 2021-01-24 22:18  黒川川  阅读(100)  评论(0)    收藏  举报