Cpp Standard Library 简明教程

C++ Library - <bitset>

Introduction

位集表示 N 个位的固定大小序列,并存储值 0 或 1。零表示值为假或者位未设置,一表示值为真或者位已设置。位集类模拟布尔值的节省空间数组,其中每个元素仅占据一个位。

Bitset represents a fixed-size sequence of N bits and stores values either 0 or 1. Zero means value is false or bit is unset and one means value is true or bit is set. Bitset class emulates space efficient array of boolean values, where each element occupies only one bit.

因为它模拟数组,所以它的索引也从第 0 个位置开始。可以使用下标操作符访问位集中的各个位。例如,要访问位集 foo 的第一个元素,请使用 foo[0]。

As it emulates array, its index also starts from 0th position. Individual bit from bitset can be accessed using subscript operator. For instance to access first element of bitset foo use foo[0].

位集类提供构造函数以从整数和字符串创建位集。位集的大小在编译时固定。STL 提供 vector<bool> 类,它提供动态调整大小的功能。

Bitset class provides constructors to create bitset from integer as well as from strings. The size of the bitset is fixed at compile time. STL provides vector<bool> class that provides dynamic resize functionality.

Definition

以下是 <bitset> 头文件中 std::bitset 的定义

Below is definition of std::bitset from <bitset> header file

template <size_t N> class bitset;

Parameters

N − 位集的大小。

N − Size of the bitset.

Member types

以下成员类型可作为参数或返回类型,由成员函数使用。

Following member types can be used as parameters or return type by member functions.

Sr.No.

Member types

Definition

1

reference

Proxy class that represents a reference to a bit.

Functions from <bitset>

以下是 <bitset> 头文件中所有方法的列表。

Below is list of all methods from <bitset> header.

Constructors

Sr.No.

Method & Description

1

bitset::bitset()Constructs bitset container and initialize it with zero.

2

bitset::bitset()Constructs bitset container and initialize it with the bit value of val.

3

bitset::bitset()Constructs and initializes a bitset container from C++ string object.

4

bitset::bitset()Constructs and initializes a bitset container from c-style string.

Member class

Sr.No.

Method & Description

1

bitset::reference()This is embedded class which provides l-value that can be returned from std::bitset::operator[].

Bitset operators

Sr.No.

Method & Description

1

bitset::operator&=Performs bitwise AND operation on current bitset object.

2

link:../cpp_standard_library/cpp_bitset_operator_bitwise_or_self.html[bitset::operator

=]Performs bitwise OR operation on current bitset object.

3

bitset::operator^=Performs bitwise XOR operation on current bitset object.

4

bitset::operator<⇐Performs bitwise left SHIFT operation on current bitset object.

5

bitset::operator>>=Performs bitwise right SHIFT operation on current bitset object.

6

bitset::operator~Performs bitwise NOT operation on bitset.

7

bitset::operator<<Performs bitwise left SHIFT operation on bitset.

8

bitset::operator>>Performs bitwise right SHIFT operation on bitset.

9

bitset::operator==Test whether two bitsets are equal or not.

10

bitset::operator!=Test whether two bitsets are equal or not.

11

bitset::operator&Performs bitwise AND operation on bitset.

12

link:../cpp_standard_library/cpp_bitset_operator_bitwise_or.html[bitset::operator

]Performs bitwise OR operation on bitset.

13

../cpp_standard_library/cpp_bitset_operator_bitwise_xor.html[bitset::operator^]Performs bitwise XOR operation on bitset.

14

bitset::operator>>Extracts upto N bits from is and stores into another bitset x.

15

bitset::operator>>Inserts bitset x to the character stream os.

Member functions

Sr.No.

Method & Description

1

bitset::all()Tests whether all bits from bitset are set or not.

2

bitset::any()Tests whether at least one bit from bitset is set or not.

3

bitset::count()Count number of set bits from bitset.

4

bitset::flip() all bitsToggles all bits from bitset.

5

bitset::flip() single bitToggles single bit from bitset.

6

bitset::none()Tests whether all bits are unset or not.

7

bitset::operator[] bool versionReturns the value of bit at position pos.

8

bitset::operator[] reference versionReturns the reference of bit at position pos.

9

bitset::reset() all bitsReset all bits of bitset to zero.

10

bitset::reset() single bitReset single bit of bitset to zero.

11

bitset::set() all bitsSet all bits from bitset to one.

12

bitset::set() single bitSet single bit from bitset to either one or zero.

13

bitset::size()Reports the size of the bitset.

14

bitset::test()Tests whether Nth bit is set or not.

15

bitset::to_string()Converts bitset object to string object.

16

bitset::to_ullong()Convert bitset to unsigned long long.

17

bitset::to_ulong()Convert bitset to unsigned long.

Non-member functions

Sr.No.

Method & Description

1

bitset::hash()Returns hash value based on the provided bitset.