Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the bitset class of C++

Shulou Source: shulou.com Published: 2022-06-01 19:11:03 09月30日 Update

This article mainly introduces "how to use the bitset class of C++". In daily operation, I believe many people have doubts about how to use the bitset class of C++. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to use the bitset class of C++". Next, please follow the editor to study!

1.bitset class

Std::bitset is a STL class that handles information represented by bit and bit flags. It is not a STL container class because it cannot adjust the length. To use the bitset class, you must include a header file:

# include

Instantiate std::bitset

When you instantiate this template class, you must specify the number of digits to manage:

Bitset fourBits; / / initialized to 0000

Initialize a sequence expressed as the literal quantity of a string (char*):

Bitset fiveBits ("10101")

two。 Use std::bitset and its members

Operator of bitset

1. > >

"0101" > > fourBits; / / insert a string into the bitset object

2. &

Perform bitwise and operation

3. |

To perform a bitwise or operation.

4.^

Perform bitwise XOR operation

5. > > =

Perform a bitwise move to the right

Member functions of bitset

1.set

FourBits.set (); / / all bits are set to 1

2.set (N, val=1) / / set bit N + 1 to val (default is 1 if there is no second parameter)

FourBits.set (2,0); / / the third bit is set to 0

3.reset

FourBits.reset (); / / reset all bits to 0

4.reset (N) / / reset N + 1 to 0

5.flip

FourBits.flip (); / / so bit inversion

6.fourBit.size (); / / returns the number of digits in the sequence

7.count

FourBits.count (); / / returns the number of digits with a value of 1 in the sequence

* * disadvantage: * * bitset cannot adjust the length dynamically. To overcome this disadvantage, STL provides the vectorl class (bit_vector in some STL implementations)

2.vector

Vector is a partial materialization of std::vector for storing Boolean data. This class can adjust the length dynamically. Before using it, you must include the header file:

# include

Instantiate vector

Contains 10 bool elements, each initialized to true:

Vector vecBool (10, true)

Use one vector to create another vector:

Vector vecBoolCopy (vecBool)

Member functions of vector

1.vector provides the function flip (), which is used to reverse Boolean values in a sequence.

2.vector is very similar to vector

The following demonstrates the use of vector's member functions:

# include # include using namespace std;int main () {vectorvecBool (3); vecBool [0] = true; vecBool [1] = false; vecBool [2] = true; vecBool.push_back (false); cout

Tags: Cantilever function Boolean sequence member Learning digit instance length Adjustment element dynamic character string Array File more disadvantage help practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Technology MariaDB Microsoft Linux