'C++ primer plus' reading notes of two

  • 2020-04-02 02:51:15
  • OfStack

The fifth chapter

1. For loop -- for(initialization; The test - expression; Update-expression) body// test-expression is converted to bool, 0 is false, and non-zero is true

Expressions -- expressions are values or combinations of values and operators. The value of an assignment expression is the value of its left member, while the assignment operator is combined from right to left.

3, a++ and ++a --

(1) for built-in types, the two are equally efficient.

(2) if the operator is overloaded, for the class, the prefix will increase the value by 1 and return the result; The suffix makes a copy and returns a copy after adding 1. So prefixes are more efficient than suffixes.

4. Comma operator --

(1) in the for loop, multiple expressions are merged into one: i++, j++;

(2) declaration: int I, j;

(3) the comma expression evaluates the first expression and then the second expression. The value of the comma expression is the value of the second part.

(4) the comma expression is the expression with the lowest priority.

5. STRCMP () -- compares two strings. Accept 2 string addresses A and B as arguments. AB also returns 0, A in alphabetical order before B, returns A negative number, otherwise returns A positive number.

(the quoted string constant is its address.)

6, clock() -- returns the system time used after the program began to execute. This value is divided by CLOCKS_PER_SEC to get the number of seconds.

7, type alias -- #define AA char// use AA as the alias of char, all AA will be replaced by char

Or typedef AA char

Cin -- cin. Get () ignores Spaces and newlines. The input sent to cin is buffered. Press enter, and the input is sent to the program.

  Cin. Get (ch) gets every character. Its arguments are declared as reference types, so the function can modify the values of its arguments.

9. EOF -- many PC programming environments treat Ctrl+Z as simulated EOF. After detecting EOF, cin sets two bits (eofbit and failbit) to 1. Eof () and fail() are used to see if they are set.

So the condition for the loop to wait for input can be set like this: while(cin. Fail () == false) {} or whle(!) Cin. Fail () {} or while (cin) {} or while (cin) get (ch)) {}

(normally, EOF is defined as -1)

Chapter vi

10. Operator --! Operators take precedence over all relational and arithmetic operators.

The logical AND operator takes precedence over the logical OR operator.

C++ ensures that the program evaluates logical expressions from left to right.

11. Cctype -- character function library. If isalpha(ch) determines whether a character isa letter, it returns nonzero if it isa letter, or zero if it is not.

12. Text IO -- when typing with cin, the program treats the input as a series of bytes, each of which is interpreted as a character encoding.

Chapter vii.

Define the function --

Void functionName(parameterList) {}

(2) return value: typeName functionName(parameterList) {}

(note! The type of the return value cannot be an array, it can be any other type.

14. Function prototype --

(1) function prototype can greatly reduce the probability of program error and improve efficiency.

(2) the function prototype does not require the variable name, a list of types is enough.

(3), bracket is empty and bracket with void is equivalent, do not specify the parameter list should use ellipsis -- void haha(...) ;

  Functions and two-dimensional arrays -- the type of pointer is what is left of the pointer name in the pointer declaration statement

For: int data [3] [4] = {{1, 2, 3, 4}, {5,6,7,8}, {4,3,2,1}}; Int total = sum (data, 3); What is the prototype of sum?

(1) the prototype is: int sum (int (*a) [4], int size);

So int (*) [4], that is, put this pointer to int[4]. So the type of data points to an array of four ints

So int *a[4] is of type int * [4], this pointer to int, there are 4 of them, so it's an array of 4 Pointers to int.

(2) function definition: int sum (int a[][4],int size);

A [r][c] = *(*(a + r) + c);

Recursion -- each recursive call creates its own set of variables.

(note! C++ does not allow main() to call itself.

17, function pointer --

(1) function address: the address of a function is the starting address of the memory where its machine language code is stored. If think() is a function, then think is its address.

(2) declare pointer function: function: double PAM (int); The pointer function is: double (*pf)(int) = PAM; // pf is a pointer to a function.

Chapter viii.

18. Compilation process -- the final product of the compilation process is an executable program (consisting of a set of machine language instructions).

When the program runs, the operating system loads these instructions into the computer's memory, so each instruction has a specific memory address. The computer then gradually executes the instructions.

19. Function call -- when a function call instruction is executed, the program stores the memory address of the instruction immediately after the function call, copies the function parameters to the stack, and jumps to the memory unit marking the starting point of the function.

Execute the function code (and perhaps put the return value into a register), then jump back to the instruction where the address was saved.

Inline functions -- the compiler USES the corresponding function code instead of the function call.

The keyword inline precedes the function declaration and the keyword inline precedes the function definition. The prototype is usually omitted and is defined directly at the prototype.

21. Reference variables -- used primarily as arguments to functions that use the original data, not a copy of it.

(1) create: int rats; Int & a = rats;

(2) the reference must be initialized at the time of declaration, and cannot be declared first and then initialized. You also cannot set a reference by assigning a value.

(3) once a reference is associated with a variable, it remains loyal.

(4) if the reference parameter is const, if the argument type is correct but not the lvalue or the type is incorrect but can be converted to the correct type, a temporary variable will be created.

(5) when returning a reference, the memory unit reference that no longer exists when the function terminates should be avoided.

22. Lvalue --

(1) data objects that can be referenced. Variables, array elements, structure members, references, and dereferenced Pointers.

(2) non-lvalues, including literal constants and expressions containing multiple terms.

(3) conventional variables belong to modifiable left values, while const variables belong to unmodifiable left values.

23, right - value reference -- can point to the right - value reference, using the && declaration. For example: double & rref = STD :: SQRT (26.00);


Default parameters -- set the default value of function parameters by function prototype.

(1) the default value must be added from right to left.

(2) arguments are assigned to parameters from left to right, and no arguments can be skipped.

25. Function overload -- functions with different parameter lists (eigenvalues) but the same function name.

(1) the type reference and the type itself are regarded as the same characteristic.

(2) the const variable cannot be assigned to a non-const parameter

26. Name modification -- each function name is encrypted according to the parameter type specified in the function prototype to track each overloaded function.

Function templates -- the equivalent of generics in Java

(1) declaration: template < Typename T > Void Swap(T &a, T &b); // typeName can be replaced by class

(2) the function template cannot shorten the executable program, and the final code does not contain any template, only the actual function generated for the program.

(3) generally put the template in the header file.

28. Explicitly materialize -- a function definition that materializes, instead of a template, when matched.

(1) non-template function: void swap(job &, job &);

(2) template function: template < TypeName T > Void swap(T &,T &);

(3) specify explicitly: template < > Void swap < The job > (job &, job &); / / swap < The job > Job is optional

(4) when the compiler selects the prototype: non-template function > Explicit embodiment > Template function

(5) explicit instantiation: template void swap < int > (int, int); / / no after the template < >

(6) implicit instantiation: for a template function, the compiler generates an instance of the containing number by reference to the template. This is usually called implicit instantiation

29. Decltype -- decltype(expression) var; // makes var the same type as expression.

(note! If expression is a function call, the type of var is the same as the return value. If expression is an lvalue, var is a reference to its type.)

Post return type -- specifies the return type for a function.

Such as the template < The class T1, class T2 >   Auto gt of T1 x, T2 y > Decltype (x + y) {... Return x plus y; }


Related articles: