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

  • 相关阅读:
    java毕业设计大众采编本微资讯发布平台(附源码、数据库)
    计算机组成原理——总结
    QT添加右键菜单(二):QWidget的右键菜单策略
    高效管理企业固定资产的办法
    基于Apache Hudi构建分析型数据湖
    Oracle 数据库中的全文搜索
    协程和 C++ Boost库的Coroutine2
    android的USB开发时 mUsbManager.getDeviceList()获取都为空
    Go学习笔记 -- 并发原理
    R语言结课及Matlab开始
  • 原文地址:https://blog.csdn.net/m0_63222058/article/details/132910548