【C++】bitset介绍与用法讲解

今日写csp,看大佬的题解中出现了bitset,以前有印象但没学,所以赶快去OI-wiki上补一下,并记录于此

std::bitset 是标准库中的一个存储 0/1 的大小不可变容器。严格来讲,它并不属于 STL。

The C++ standard library provides some special container classes, the so-called container adapters (stack, queue, priority queue). In addition, a few classes provide a container-like interface (for example, strings, bitsets, and valarrays). All these classes are covered separately.1 Container adapters and bitsets are covered in Chapter 12.

The C++ standard library provides not only the containers for the STL framework but also some containers that fit some special needs and provide simple, almost self-explanatory, interfaces. You can group these containers into either the so-called container adapters, which adapt standard STL containers to fit special needs, or a bitset, which is a containers for bits or Boolean values. There are three standard container adapters: stacks, queues, and priority queues. In priority queues, the elements are sorted automatically according to a sorting criterion. Thus, the "next" element of a priority queue is the element with the "highest" value. A bitset is a bitfield with an arbitrary but fixed number of bits. Note that the C++ standard library also provides a special container with a variable size for Boolean values: vector.

——摘自《The C++ Standard Library 2nd Edition》

由此看来,bitset 并不属于 STL,而是一种标准库中的 "Special Container"。事实上,它作为一种容器,也并不满足 STL 容器的要求。说它是适配器,它也并不依赖于其它 STL 容器作为底层实现。

 bitset的头文件:<bitset>
三种创建方式:

bitset<8>bs1;//00000000
bitset<8>bs2(0x5);//00000101
bitset<8>bs3(string("101"))//00000101

运算符[],可以直接访问某位上的元素

bitset<16>bs(string("100101"));
cout<<bs[0]<<endl;//1

运算符== !=   用于比较两个bitset是否完全一样(不演示)

operator &/&=/|/| =/^/^=/~: 进行按位与/或/异或/取反操作。bitset 只能与 bitset 进行位运算,若要和整型进行位运算,要先将整型转换为 bitset

operator <>/<<=/>>=: 进行二进制左移/右移

bitset<4> foo (string("11111"));
bitset<4> bar (string("00111"));
cout << (foo^bar) << endl;       // 11000
cout << (foo&bar) << endl;       // 00111
cout << (foo|bar) << endl;       // 11111
cout << (foo<<2) << endl;        // 11100
cout << (foo>>1) << endl;        // 01111
cout << (~bar) << endl;           // 11000
cout << (bar>>1) << endl;         // 00011
cout << (foo==bar) << endl;       // 0
cout << (foo!=bar) << endl;       // 1
cout << (foo&=bar) << endl;        // 00111
cout << (foo|=bar) << endl;        // 00111
cout << (foo^=bar) << endl;		  // 00000

可以使用<<和>>输入输出bitset

如果创建的大小为4,但是输入了100010,就会截取前4位,输出1000

bitset<4>bs;
cin>>bs;
cout<<bs;

亿些函数和使用说明

count(): 返回 true 的数量
size(): 返回 bitset 的大小。
test(pos): 它和 vector 中的 at() 的作用是一样的,和 [] 运算符的区别就是越界检查。
any(): 若存在某一位是 true 则返回 true,否则返回 false。
none(): 若所有位都是 false 则返回 true,否则返回 false。
all():C++11,若所有位都是 true 则返回 true,否则返回 false。
set(): 将整个 bitset 设置成 true
set(pos, val = true): 将某一位设置成 true/false。
reset(): 将整个 bitset 设置成 false
reset(pos): 将某一位设置成 false。相当于 set(pos, false)。
flip(): 翻转每一位。(0\leftrightarrow1,相当于异或一个全是 1 的 bitset)
flip(pos): 翻转某一位
to_string(): 返回转换成的字符串表达。

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值