Introduction to the simple use of C++ standard library type bitset
- 2020-05-26 09:52:07
- OfStack
std::bitset is part 1 of STL. To be exact, std::bitset is a template class whose template parameter is not a type, but an integer value (this 1 feature is a new feature of ISO C++2003), with which we can use bits like array 1.
#include<bister>
using std::bitset;
1 sentence definition: customizable bits that are used to record data types in base 2.
1. Define and initialize
bitset < n > b; //b has n bits, each of which is 0;
bitset < n > b (u); //b is a copy of unsigned long type u
bitset < n > b (s); //b is a copy of the n bit string in the string object s
bitset < n > b (s pos, n); //b is a copy of the n locations in s starting at the pos location
bitset < n > b (s pos); //b from the pos position of s to the end of s (note that the value starts from the right end of b)
Note: the number of bits defined by n is filled according to the initial value at the initialization time. The range beyond the assigned value is left out, and the empty number is filled with zero.
When reading into the bit set from the string object, bitset reads from right to left.
2, the operating
b. any (); // find out if b exists.
b. none (); // is there no 1 in b?
b. count (); // the number of 1 in b
b. size (); / / b digits
b [pos]; // access the value at pos in b
b. test (pos); // check whether pos in b is 1
b. set (); // set all the bits in b to 1
b. set (pos); // set pos as 1 in b
b. reset (); // set all positions in b to 0
b. reset (pos); // set pos in b to 0
b. flip (); // all the base 2 bits in b are inverted
b. flip (pos); //b inverts the base 2 bit at pos
b. to_ulong; // returns a value equal to unsigned long
os
<
<
b; // output b middle set
The above is the site to introduce the C++ standard library bitset type of simple use method, I hope to help you, if you have any questions welcome to give me a message, this site will timely reply to you!