Bitset
Unlike vectors, the Bitset type object differs only in its length and not its type. When defining a bitset, it is necessary to clarify how many bits the Bitset contains, and to give it a length within the angle brackets
Degree value:
Bitset<n> b; B has n bits, each of them is 0.
Bitset<n> b (U); B is a copy of the unsigned long type U
Bitset<n> b (s); B is a copy of the bit string contained in the string object s
Bitset<n> B (S, POS, n); B is a copy of n bits from position pos in S.
#include <bitset>#include<string>#include<iostream>using namespace:: STD;intMain () {stringStr"1111111000000011001101"); Bitset< +> Bit5 (str,5,4);//4 bits starting at str[5], 1100bitset< +> bit6 (str, str.size ()-4);//Use last 4charactersbitset< ->A; cout<<a<<" "<<bit5<<" "<<Bit6; //<< "<<b<<" <<c<<endl;Cin>>A; return 0; intIval =1024x768; int*pi =0;//pi initialized to address no object int*pi2 = &ival;//pi2 initialized to address of Ival int*pi3;//OK, but dangerous, PI3 is uninitializedPI = pi2;//Pi and PI2 address the same object, E.g.ivalPi2 =0;//PI2 now addresses no object//assigning an int variable to a pointer is illegal, although the value of this int variable may be 0. However, a const value of 0 or 0 can be obtained at compile time to assign the pointer: intival; intZero =0; Const intC_ival =0;//int *pi = ival;//error:pi initialized from int value of Ival//pi = zero;//Error:pi assigned int value of zeroPI = c_ival;//Ok:c_ival is a const with Compile-time valueof 0PI =0;//ok:directly Initialize to literal constant0}
Pointer
Pointers can only be initialized or assigned to variable addresses of the same type or to another pointer.
assigning an int variable to a pointer is illegal, although the value of this int variable may be 0.
However, a const value of 0 or 0 can be obtained at compile time to assign the pointer:
int ival; int zero = 0 const int c_ival = 0 int *pi = ival; // ERROR:PI initialized from int value of ival pi = zero; // Error:pi assigned int value of zero pi = c_ival; // Ok:c_ival is a const with compile-time valueof 0 pi = 0 ; // ok:directly initialize to literal constant 0
The C + + language cannot detect whether the pointer has not been initialized, and it cannot distinguish between valid addresses and the address formed by the bits stored in the storage space allocated by the pointer. It is recommended that programmers initialize all variables, especially pointers, before using them.
C + + provides a special pointer-type void*, which can hold the address of any type of object:
Double 3.14 ; double *pd = &obj; // ok:void* can hold the address value of any data pointer type void // obj can be a object of any type // PD can is a pointer to any type
void* indicates that the pointer is related to an address value, but it is not clear what type of object is stored on this address.
The void* pointer supports only a few limited operations: compare to another pointer, pass a void* pointer to a function, or return a void* pointer from a function, and assign a value to another void* pointer.
Using the void* pointer to manipulate the object it points to is not allowed.
2015.09.06 C + + notes