C ++ Simplify05

Source: Internet
Author: User

1. Standard Library bitset
# Include <bitset>
Using std: bitset; // or using namespace std;
1-1: The bitset class is a type template. Unlike the vector, the bitset type object only differs in its length rather than its type. Well-known length value: bitset <32> bitvec;
The position of the bit set starts from 0, and the bitvec sequence is from 0 to 31. The Bit String starting with 0 is the lower level, and the bit string ending with 31 is the higher level.
1-2: Several initialization methods:
Bitset <n> B; // B has n digits, each of which is 0.
Bitset <n> B (u); // B is a copy of the unsigned long u.
Bitset <n> B (s); // B is a copy of the bits contained in string object s.
Bitset <32> bitvec ("1100"); // 2nd, 3 bits are 1, and the remaining 0
Note: The string object and bitset object are converted in reverse order: the rightmost character of the string object (that is, the character with the largest subscript) it is used to initialize the low-level bits of a bitset object (that is, the bits of subscript 0 ).
Bitset <n> B (s, pos, n); // B is the copy of the n-digit starting from the position pos in s. If the third parameter n is omitted, always get to the end of the string
Bitset <16> bitvec (0 xffff); // hexadecimal representation of 16 1
Bitset <32> bitvec (0 xffff); // hexadecimal representation of 16 low 1 and 16 high 0
1-3: Common bitset operations
B. any (); // whether there is a binary bit set to 1 in B
B. none (); // does B contain binary digits set to 1?
B. count (); // The number of binary values set to 1 in B
B. size (); // The number of binary digits in B. The return value type is size_t.
B [pos]; // access the binary position at the pos in B
B. test (pos); // is the binary position at the pos position 1 in B?
B. set (); // set all binary bits in B to 1
B. set (pos); // set the binary position of B in pos to 1.
B. reset (); // set all binary values in B to 0.
B. reset (pos); // set the binary position in B to 0.
B. flip (); // reverse all binary values in B by bit
B. flip (pos); // reverse the binary position at the pos in B
B. to_ulong (); // return an unsigned long value with the same binary bits in B.
OS <B // outputs the bit set in B to the OS stream
2. Array
Similar to the vector type, Arrays can also store a group of objects of a certain type, but the array length is fixed. New elements cannot be added if they have been created.
Comparison: Modern C ++ programs use vector to replace arrays. arrays are strictly restricted to internal applications. Only when performance tests show that vector cannot meet the necessary speed requirements, to use the array.
The type name in the array definition can be a built-in array type or a class type. In addition to the reference, the type of the array element is also an arbitrary composite type. Not all elements are referenced arrays.
2-1: array initialization: The dimension must be specified in a pair of square brackets [].
Display the initialization array elements:
Const unsigned size = 3;
Int ia [size] = {0, 1, 2}; // The array contains three elements: 0, 1, 2
Int ia [] = {0, 1, 2}; // The array length is determined to be 3 based on the number of listed elements
If the element is of the class type, the default constructor is automatically called for initialization. If the class does not have a default constructor, the display initialization must be provided for the elements in the array.
Special character array:
Char ca1 [] = {'C', '+', '+'}; // a 3-character array
Char Ca 2 [] = {'C', '+', '+', '\ 0'}; // a 4-character array with an empty character at the end
Char A3 [] = "C ++"; 4-character array with an empty character at the end
Directly copying and assigning values to arrays is not allowed:
You cannot use an array value assignment to initialize a new array.
Int ia [] = {0, 1, 2 };
Int ia2 [] (ia); // Error !!!
2-2: Array Operations
Use the subscript operator. The subscript starts from 0 to n-1 (the array has n elements ).
Author: TskyFree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.