C ++ Primer, third edition, Reading Notes

Source: Internet
Author: User

Preface

C ++ Primer, the third edition of this book, was read during the development of Window Mobile 6 last year. After reading the book, I made some notes and it was no longer necessary. Today, I occasionally wrote an article and backed it up. It is said that every time I re-read this book, there will be a new harvest. The so-called gentle and new, we can see the classic of this book.

 

Statement

You are welcome to repost, but please keep the original source of the article :)

Blog: http://www.cnblogs.com

Farmer's uncle: http://over140.cnblogs.com

 

Body

 

1. If a variable is globally defined, the system will ensure that the initialization value is 0. If the variable is locally defined or dynamically allocated through the new expression, the system does not provide it with the initial value 0.

 

2. Generally, it is best to define the pointer as "string * p;" instead of "string * p ;"

 

3. The action of attempting to point a non-const object pointer to a constant object will cause compilation errors.

 

4. The reference type mainly acts on the form parameter P/89 of the function.

 

5. The enumerated Member values do not need to be unique.

  

6. An array cannot be initialized by another array or assigned to another array. In addition, C ++ does not allow the declaration of a reference array (that is, an array composed of references) P/95

 

7. the main purpose of the volatile modifier is to prompt the compiler that the value of this object may be changed without being detected by the compiler. Therefore, the compiler cannot optimize the code that references these objects.

 

8. The result of a comma expression is the value of the rightmost expression (from left to right) P/135

 

9. For more information about bit operators and how to use them, see P/136.

 

10. Container

10.1 associate containers

10.1.1 map P/247

"Key/value" pairs: Keys are used to index map, while values are used to store data.

Insert is recommended for inserting a single element (excluding subscript) P/248-P/249

10.1.2 set P/256

10.1.3 comparison P/247

If you only want to know whether a value exists, set is the most useful. If you want to store (or modify) a related value, map is the most useful.

10.2 ordered container

10.2.1 list

In the non-contiguous memory area, bidirectional traversal is allowed, and insertion and deletion efficiency is high. It does not support random access, and traversal is required. Each element has an additional space overhead of two pointers.

10.2.2 vector

Indicates a continuous memory area. Each element is stored in this area sequentially. Random Access is very efficient, but insertion and deletion efficiency is low (unless it is the last element). The elements on the right must be copied again. (Deque is also an end-to-end continuous memory, but it supports efficient insertion and deletion of elements in the header. It is implemented through two-level array structure .)

Vector Auto-growth mode: allocate two times the current container's storage zone, copy the current value to the newly allocated memory, and release the original memory.

10.2.3 comparison between list and vector (vector or list? P/213)

 

11. Calling a function is much slower than directly calculating conditional operators-the inline mechanism is used to optimize small, few rows, and frequently called functions P/303

 

12. The header file should not contain non-inline functions or object definitions

 

13. Automatic variables frequently used in functions can be declared as register automatic objects using register. The array indexes and pointers that appear in loop statements are good examples of register objects:

For (register int ix = 0; ix <sz; ++ ix)

If the selected variable is frequently used, the register variable can improve the function execution speed.

Note: The keyword register is only a suggestion for the compiler. Some compilers may ignore this suggestion. Instead, they use the register allocation algorithm to find the most suitable candidates and place them in the available registers of the machine. P/337

 

14. If the operand is set to 0, C ++ will ensure that the delete expression will not call the delete () operator and there is no need to test whether the value is 0. P/340

If (pi! = 0) // No need to write this line

Delete pi;

 

15. After the delete pi, the pi becomes an empty pointer. We recommend that you set the pointer to 0 P/340.

 

16. For automatic pointer auto_ptr, see P/341.

 

17. C ++ supports the explicit table initialization mechanism inherited from the C language, similar to the method used to initialize an array:

Data local1 = {0, 0 };

// Equivalent to local1.ival = 0; local1.ptr = 0;

These values are resolved by location based on the declared order of data members. P/566

 

18. The explicit keyword is used to disable implicit conversion of the compiler.

 

19. The Inline destructor must be expanded (during compilation) between each return statement. Therefore, we recommend that you replace the return statement with other variables if possible.

 

20. int a = B + c; // initialization of

Int;

A = B + c; // value assignment operation, which is more efficient

 

21 ."::",".*",".","? : "Four operators cannot be overloaded.

 

22. typeid is used to obtain the actual type of the Object/variable. Usage: typeid (type). name ()

 

23. Function Introduction

Sizeof is used to return the length of bytes of an object or type name. P/132

Isalpha determines whether it is an English letter

# Include <cstring>

Int strlen (const char *) // returns the length of the string

Int strcmp (const char *, const char *) // compare whether two strings are equal

Char * strcpy (char *, const char *) // copy the second string to the first string.

# Include <assert. h>

Assert General preprocessing macro (assert P/12)

# Include <algorithm> // common algorithms including element retrieval, replacement, and reverse order of various data structures. Sort/find/max

# Include <iomanip>

Setw () // similar to scanf, the number of characters it reads can be reduced by 1 P/112

      

 

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.