Chapter IV. Composite Types
5. Commons
Usage is the same as structure, the main difference is that all elements in the structure allocate memory, and the common body (union) is that all elements share a single memory, that is, only one type can be represented at a time, and the size is the largest type.
union one4all // Size is the length of long { int int_val; long long_val; } pail; pail.int_val = 15;
6. Enumeration
Constants are defined within an enumeration. When an enumeration variable is declared, the value of the variable can only be within the range of the enumeration. Constants defined within an enumeration can be automatically converted to int, but ints cannot be automatically converted to the enumeration type. There is no arithmetic operation defined for the enumeration type. An arithmetic operation occurs in the expression, automatically converting the enumeration constant to int.
enum spetrum {red, orange, yellow, green, blue, violet, indigo, ultraviolet}; // red-ultra 0-7 spetrum band; band = orange; //effective band++; //invalid band = red + green; // Invalid, red, green are automatically converted to int, and int cannot be converted to enumeration, so band cannot be assigned int color = red; //red is automatically converted to int, color = 0 band = 3; // Invalid, int cannot be automatically converted to enumeration band = spectrum(5); //Valid, 5 within enumeration range
The value of an enumeration constant can be initialized. The value must be an integer. The uninitialized value is 1 greater than the previous one. The values of a constant can be the same. The range of enumeration is the smallest power of 2 greater than the maximum value minus one. If the minimum value of enumeration is greater than 0, the lower limit is 0. Otherwise, the lower limit is calculated as the upper limit. However, if the int type constant is within the enumeration range, it can be cast to an enumeration.
enum bits {one = 1, null = 1, two = 100, three}; // three = 101 // The bits range is 0-127, the minimum power of 2 greater than 101 is 128, and the lower limit is 0 // If one = -6, the lower limit is -8, plus 1, and -7, the smallest power of 2 less than -6 bits num = bits(111); // Valid, 111 in bits range
7. Pointer and free storage
Uninitialized pointers do not point to constants, and where uninitialized pointers point is unknown, the memory stored by the constant is not known, and a bug may occur. To assign a numeric value as an address to the pointer, cast it.
int *pt; pt = 0xB8000000; // Invalid, 0XB8000000 is integer pt = (int*)0xB8000000; //effective *pt = 200; //Don't do this
When the pointer variable is + 1, the number of bytes of its type is increased, such as the value of pt+1 in the code above is 0xB8000004. The array name can be used as a pointer constant, meaning that the pointer represented by the array name can only point to the address of the first element of the array.
int num[4]; int *pt = num; num = num + 1; //invalid pt = pt + 1; //Valid, address value plus 4
The array name is interpreted as the address of the entire array, &num+1 adds 16 bytes to the value, and pt+1 adds 4 bytes to the value.
8. new operator
typeName * pointerName = new typeName; delete pointerName;
By allocating a piece of memory space with new, you can point to a constant with a pointer. Newallocated memory is unused and will not cause a bug. When delete releases new, it does not delete the new pointer, which can continue to point to other variables of the same type.
typeName * pointerName = new typeName [num_elements]; delete [] pointerName;
new tells you the number of elements when creating a dynamic array, and don't forget [] (in brackets, space-time) when deleting. When used, the pointer points to the first element, just as the array does.
9. Substitutes for arrays
#include <vector> using namespace std; // Or use vector via std:: vector<int> VI // Vector<typeName> vt(n_elem) n_ Elem can be a variable or a constant vector <int> vi;
To use the vectors class, include the vectors header file, which is in the std namespace. It can adjust the length of the array in use, so you can leave it unchanged.
#include <array> using namespace std; // Or use array via std:: array < int, 5 > AI array<int, 5> ai = {1, 2, 3}; //Array<typeName, n_ Elem> arr n_ Elem can only be a constant
The array class is a fixed-length array and requires a header file and namespace std to use.
Chapter 5. Cycles and Relational Expressions
1. for loop
The value of the assignment expression is defined as the value of the left member.
y = (x = 14); // y has a value of 14
The prefix increment, subtraction, and de-reference operators have the same precedence, and combine in a right-to-left fashion. The suffix increment and de-reference operators have higher precedence than de-reference.
int *pt = NULL; int a[2] = {1, 2}; pt = a; int m = *++pt; // pt pointer increments before de-referencing, m = 2; --pt; int n = *pt++; // Because of the suffix effect, assign values first, then pt pointer increments, not values. // n = 2, pt points to a[1]
Variables defined in a loop block hide external variables of the same name and are reused outside the block.
int x = 10; for (int i = 0; i < 3; i++) { cout << x << endl; // x = 10 int x = 20; cout << x << endl; // x = 20 } cout << x << endl; // x = 10
The comma operator guarantees that the values of the comma expression are the last part of the value calculated sequentially from left to right.
int n = (1, 2, 3); cout << n << endl; // n = 3
All three expressions in the for loop can be omitted, and the loop continues when the test expression is omitted.
String array names and string constants themselves represent their addresses.
char ch[] = "go to sleep"; ch == "mate"; // Compare Address Same
The strcmp (str, str2) function compares the size of two strings (input pointer) by a character-by-character comparison (ASCII code), the former smaller than the latter, the function value is negative, the two strings are the same, the function value is 0, otherwise it is positive.
string class strings can be compared directly using a relationship character, the same as strcmp functions.
2. while Loop
The clock() function returns a long or other type of value in indeterminate units, possibly seconds. Header file ctime defines a symbol constant CLOCK_PER_SEC, which refers to the time in system time units, CLOCK_PER_SEC multiplies the return value of clock(), gives the number of seconds, and defines clock_t is the alias of the return type of clock().
Type aliases come in two ways, #define preprocessor and keyword typedef, which can cause problems.
#define CHAR_POINTER char * typedef char * pt; pt p1, p2; // p1, p2 is a char type pointer CHAR_POINTER p3, p4; // Equivalent to char * p3, p4, p4 is a character variable, not a pointer
3. do while loop
Execute the loop body at least once.
4. Scope-based for loops
This method is a new feature of C++11, which performs the same operation on each element of an array or container class. To modify the array value, use a reference.
double prices[3] = {4.8, 10.4, 7.99} for (double x : prices) // Prints each element in turn cout << x << endl; for (int x : {1, 2, 3}) cout << x << ' '; // Print 12 3 in turn for (double &x : prices) // Prints each element in turn x *= 0.8; // prices reduces each element by 0.8 times
5. Loops and text input
CIN ignores spaces and line breaks. Starting with the first character, it is not sent to the program until a carriage return is entered, and the characters before it are in the buffer. C++ function overloading allows multiple functions with the same name, as long as the list of function parameters is different, such as cin.get(), there can be two parameters, one parameter, or no parameters, which are three different functions with the same name. The cin.fail() and cin.eof() functions check whether the input is EOF(ctrl+z), and if so, both return true.
cin.get(ch) returns a cin object and the function is converted to bool type to determine if the input is EOF.
while (cin.get(ch)) // Whether the input ch is EOF or eof, the function returns false
Cin.get (no parameter) returns the type char.
6. Nested loops and two-dimensional arrays
Initializing a two-dimensional array is a comma-only one.
int buts[2][2] = {(1, 2,), (3, 4)}; // Are all commas