• 【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的头文件:
    三种创建方式:

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

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

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

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

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

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

    1. bitset<4> foo (string("11111"));
    2. bitset<4> bar (string("00111"));
    3. cout << (foo^bar) << endl; // 11000
    4. cout << (foo&bar) << endl; // 00111
    5. cout << (foo|bar) << endl; // 11111
    6. cout << (foo<<2) << endl; // 11100
    7. cout << (foo>>1) << endl; // 01111
    8. cout << (~bar) << endl; // 11000
    9. cout << (bar>>1) << endl; // 00011
    10. cout << (foo==bar) << endl; // 0
    11. cout << (foo!=bar) << endl; // 1
    12. cout << (foo&=bar) << endl; // 00111
    13. cout << (foo|=bar) << endl; // 00111
    14. cout << (foo^=bar) << endl; // 00000

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

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

    1. bitset<4>bs;
    2. cin>>bs;
    3. cout<

    亿些函数和使用说明

    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(): 返回转换成的字符串表达。

  • 相关阅读:
    用于图像恢复的即插即用 ADMM:定点收敛和应用(Matlab代码实现)
    SkyEye Q&A ——第三期
    【HTML】HTML网页设计--智能养老系统前端
    基于GIS、RS、VORS模型、CCDM模型、geodetecto、GWR模型集成的生态系统健康的耦合协调分析
    《HarmonyOS开发 – OpenHarmony开发笔记(基于小型系统)》第6章 环境监测系统
    1. 爬虫之Beautifulsoup解析库&在线解析图片验证码
    Debian下Hadoop集群安装
    PyTorch学习笔记-Convolution Layers与Pooling Layers
    Docker容器自启动
    如何正确的关闭Redis服务器
  • 原文地址:https://blog.csdn.net/m0_63222058/article/details/132910548