|
Open the last two days of C ++ undergraduate school textbooks, a quick review of what aspects of the content of C ++. Although the contents of the book basis for comparison, but there are some points worthy of your own knowledge to strengthen memory. Sub-articles, coupled with their own understanding of the basics of C ++ study notes are summarized as follows.
Recalling the first part of the process-oriented.
C ++ data types
1, the data type modifiers:
1.1 long not only can be modified int, and can also be modified double; but only modifies short int;
1.2 "Only modifier" data types are "modifier int" shorthand, such as unsigned long is unsigned long int shorthand.
2, integer constant: octal numbers begin with 0, hexadecimal numbers begin with 0x or 0X.
3, floating-point constants: 1.23 * 10 ^ 4 in C ++ can be expressed as 1.23E4 or 1.23e4.
4, the escape character:
4.1 If the escape character is back with an integer constant, it must be a zero-prefix octal (note the leading zero may be omitted), or in x-prefixed hexadecimal number (note that x is not 0x);
4.2 If the escape character followed by the character behind which the characters must be lowercase, uppercase only express itself. Such as '\ E' is considered 'E'.
5 variables:
5.1 variable name is actually a symbolic address, when the program is compiled by the system is connected to each variable name is assigned a memory address. Values from variables in the program, in fact, find the corresponding memory address by the variable name, data is read from the storage unit.
5.2 C ++ requires mandatory variable for purposes of the definition:
5.2.1 who have not been previously defined, not as a variable name, variable name to ensure that proper procedures are used;
5.2.2 Each variable is specified as a certain type, at compile time can be assigned to the respective storage units;
5.2.3 will be able to check the legality of the operation performed at compile time based on the variable type. (Eg: a, b of real variables is not allowed remainder operator)
Often the difference between symbolic constants and variables defined in 5.3 #define const definition:
Except that a symbolic constant symbols instead of a sequence of characters, when the preprocessor to replace all symbolic constants specified character sequence, it is not the type does not exist in the storage unit named symbolic constants in memory;
And often variable characteristic variable, which has a type, which exists in the storage unit named in memory.
5.4 C ++ in general use a constant variable. Because often use const statement to define a variable with a data type for easy system type checking, but it also has a function to calculate the initial value of expression.
6, solving logic expressions, not all logical operators have been executed, but must be executed in the next logical operators can be obtained solution expression before executing the operator.
6.1 As a || b || c, is true as long as you do not have a judge b and c;
6.2 As int a = 1, b = 2, c = 3, d = 4, m = 5, n = 6; bool x; x = (m = a> b) && (n = c> d); executing the after the n value is not 0, but is still 6.
7, comma operator:
7.1 int a; (a = 3 * 5, a * 4), a + 5; After execution, a 15, the second sentence of the whole expression is 20;
7.2 comma operator is all the operators in the lowest level.
Program control statements
1, if ... else ... statement, else always the nearest one if pairing.
2, switch ... case ... statements, case is followed by a constant expression (not a common variable), the value of the constant expression must be integer, character or an enumerated type.
3, for (statment1; statement2; statement3), if there is to continue the implementation of the statement in the for loop, skip execution statement3, and the next cycle.
Arrays and structures
1, string manipulation:
1.1 gets (str) function is input from the terminal to a string array of characters, and get a function value, the function value is the starting address of an array of characters. (At the beginning of this section are the following str of type char *)
Expand: cin.get (str, size), each read a whole line, and the enter key generator line breaks to stay in the input queue, usually use this combination: cin.get (str, size) .get ();
cin.getline (str, size), each read a whole line, and the enter key generator newline abandoned; usage and gets (str) almost the same, just more than a size parameter;
getline (cin, string), each read a whole row, and for the string type;
getchar (ch), accepts a character.
Role 1.2 puts (str) is a string (with '\ 0' end of the sequence of characters) to the output terminal.
1.3 strcat (strDest, strSource) is connected to the string string strSource strDest, it returns an array of characters strDest address.
1.4 strcpy (strDest, strSource) is copied to the character array string strSource strDest go, returned as an array of characters strDest address.
1.5 strcmp (str1, str2) compare str1 and str2, if str1 == str2, returns 0; if str1> str2, returns a positive integer; if str1 < str2, returns a negative integer.
1.6 strlen (str) returns the actual length of the string, not including the '\ 0' included.
Next review function pointer and application.
function
1, multi-dimensional arrays as parameters, the size of the first dimension may be omitted (or may not be omitted), but other dimension size must be specified. For example, two-dimensional array parameter, int array [3] [] is not correct, int arry [] [10] correctly. Because the argument is coming from the starting address of the array, row stored in memory, but not regional branches and columns, if you do not specify the number of columns in the parameters, the system can not determine how many lines should be how many columns.
2, in C ++, you can specify a default value for the parameter in the function call, do not specify the parameters corresponding to the arguments automatically use the default value. The default parameter C ++ function can be a constant, it can be global variables or global constants, or even a function call. The default parameters can appear only once, if given in the function prototype, we can not give again in the function definition. If you define a function before the function call, the program does not declare the function, the default value shall be given in the function definition; otherwise, if you do not specify a function in the function prototype default values for the parameters in the definition given the default value, the default value can not be used when calling. If there are multiple parameters in a function, the default parameters should be defined by one from right to left.
3, an inline function definition must appear before the first inline function is called, it is because the compiler at compile time, replace the function call statement with the body of the function inline function.
4, a function not only as overloaded functions, but also has a function as default parameters. Such as: int myMax (int x, inty); int myMax (int x, int y, int z = 100) ;, when the function is called if less of a parameter, such as calling "myMax (1,2)", compiled the system can not be determined using an overloaded function or a function with default parameters, appears ambiguous, the system can not be performed.
5, the use of global operator "::", you can access the global variables in the local scope. For example, the global variable x, and have a local variable x, in the scope of the local variable you want to use the global variable x, then use the ":: x".
6, pre-assigned static variables in the program at compile time, and before the program execution will be determined storage unit. When you define a static local variable, if at the same time develop the initial value, the initial value is before the program begins execution will be set after each call function is no longer reset the initial value, but retain the end of the last function call value.
7, if the definition of a static variable, the initial value is not specified, the system automatically assign a static variable to the full binary information as Initial zero. (However, in order to facilitate the transplant procedure, and edited in the program should be explicitly given the initial value of local variables static)
8 static global variables for the same function in the source file is accessible, but with the general global variables, it can not be any other functions to access the source file. (Static function has the same access restrictions)
9, the register variable (register): Only int / char / pointer local variables and parameters can be stored in the register class. The storage class is specified as a local variable registers, to remind the compiler that this variable is used very frequently in the program, when allocating memory for the variable, if possible, as far as possible register allocation for it, because access to the register than faster access to the storage unit. A parameter storage class is specified as register, possibly because they want to visit some special drivers, drivers for these devices is the requirement to register with the system parameters to pass information.
10, you can use "# undef identifier" command to terminate the scope of macro definitions.
11, with the parameters of the macro, the macro expansion is real in parentheses after the name of the macro program within the parameters from left to right in the title intact macro substitution parameters.
Example: #define PI 3.1415
#define S (R) = PI * R * R
If you call S (10.0 + 10.0), we can not get the desired result. Actual results: 3.1415 * 10.0 * 10.0 + 10.0 + 10.0. If you are using inline functions, we can solve this problem.
Pointers and references
1, with the pointer or index can be found in the array elements. By the following standard method is relatively straightforward, but more time-consuming to find the array elements. Especially in the circular reference array element values, with the pointer calculated rule does not have to be repeated each time the address.
2, point constant pointers: a pointer to a variable constant. Example: const char * name = "fang"; then can not use the following similar statement name [3] = 'a'; but because name is a pointer to an ordinary pointer variables constant, rather than the regular pointer, so you can change the name of the point, the following statement is permitted name = "pei".
3, often Pointer: the pointer itself is declared as a constant, rather than the object it points to life is a constant. Create a constant pointer can not move it is to create a fixed-pointer, but the data to which it refers may be changed. When you define a pointer constant must be initialized. Example: char * const name = "fang"; allow name [3] = 'n', not allowed to name = "pei".
4, often pointing pointer constant: the pointer itself does not change, does not change the value of points. Example: const char * const name = "fang".
5. Quote: reference is an alias for a variable or constant identifier plays. int val; int & rval = val; the val and rval refers to the same variable, they are used exactly the same.
6, when the compiler see the "&" is not behind its identifier allocated memory space, but simply it refers to that identifier has the memory space assigned to it.
7, when you declare a reference type variable, you must initialize it, that must be stated objects it references cited in the declaration. The referenced object must have a corresponding memory space. (Function parameter is a reference type, "not initialized" because at the time the function is called, the argument is assigned to the formal parameter)
8, can not be declared void type of reference (void itself because it means that no data type, meaning there is no reference to it); not a reference to the name of an array (because the starting address of the array expressed in the name, which itself is not a variable); You can not define a pointer pointing to a reference type (because the reference itself is just a symbol, it does not have any memory space).
9, when defined by const reference, that they can not change the value referenced by reference space. In addition, when a constant variable references must be cited this definition as const.
10, the biggest use is cited as a parameter or return value type, so the expansion function is passed functional data. It should be noted: a reference parameter corresponding argument must have a corresponding memory space that argument must have a valid memory space to be able to be referenced in this space. Usually cited as function parameters in the following two situations: 1) function needs to return multiple values of the occasion; 2) the structure of the parameters of the function or class, because the general will take up more memory space, if you pass by value will be required allocate more stack space to store parameter values, the need for large amounts of data copy operations, it will consume more space and time. (In fact, this two occasions pointers can achieve the same effect) |
|
|
|