One, bitset container
First of all, we need to know the storage method of the container such as bitset . This data structure is essentially like a string of bool type, but it can be directly used as a number to perform binary operations such as left shift and right shift, fetch or negate, etc. For these numbers Performing a simulation operation can perform a unified operation on all numbers.
Second, the use of containers
1, the creation of the container
At the beginning of the bitset type, the number of bits in the array needs to be initialized, and the array is all 0, so the parameter we use when creating is the size parameter
1. Create an empty bitset container
#include <iostream>
#include <bitset>
using namespace std ;
int main ()
{
bitset <18> name ;
cout << name << endl ;
return 0 ;
}
The following assignments are all truncated if it is too long, and 2 if it is too short , to create a bitset container of decimal numbers
bitset <18> name(34) ;
3. Create a string and character array container
string s = "0101100101010" ;
bitset <16> bi(s) ;
cout << b2 << endl;
char s[10] = {
'1' ,'0' ,'1' ,'1','1','0','0'....};
bitset <16> bi(s) ;
cout << b2 << endl ;
2, Common operations of containers
b2 |= b3; //两个二进制数取或操作;
//常用
b2 &= b3; //两个二进制数取与操作;
//常用
b2 ^= b3; //取异或;
b2 << 2; //左移右移;
3. Commonly used functions
Query function, query the number of 1
cout << q.count() << endl ;
Query array length
cout << s.size() << endl ;
rebuild string
q.reset() ;
4. Array characteristics
You can use subscripts to call values just like arrays.