C ++ primer 3rd English version Reading Notes + exercise code

Source: Internet
Author: User
Chapter 1:

1. Currently, good compilers supported by the iso98 Standard C ++ include GCC and vc7.1 (Visual Studio. NET 2003)

2. Due to different implementations of various platforms and compilers, the extension of the C ++ header file (suffix) is not uniform. Therefore, in the current C ++ standard, the header file does not contain any suffix, for example, # include <iostream> is no longer # include <iostream. h>

3. Currently, the C ++ standard must use namespace to call any library. For example, using namespace STD is a standard library that uses C ++, if you call any library without namespace (including third-party libraries), the compiler reports errors that do not know the library functions.

4. The C ++ standard pre-defines two names, __line _ and _ file __, representing the source code line number and file name, which can be directly referenced in the code.

5. During output, we can use Endl to replace "\ n", because Endl can not only insert a newline in the row, but also flush output buffer

Chapter2

1. int * pint = new int (1024); // a new int-type object with an initial value of 1024

2. int * Pia = new int [4]; // a new int-type array, containing four elements. Pia is the address of the first element of the array.

3. The new object must be destroyed using Delete. If the new object is not used, the system will automatically destroy and recycle it. Delete an object, use Delete pint, delete an array, and use Delete [] Pia

4. namespace. The first method is <namespace >::, such as STD: cout; the second method is using namespace STD, all subsequent library calls in the code will be assumed by the compiler as the library below STD; the third method is namespace Lib = disney_feature_animation; then the Lib: <Method Name> can be called, it is equivalent to assigning an easy-to-remember alias to a long, hard-to-remember namespace. For more details, refer to the namespace chapter.

5. arrays exist in C ++, but vector is provided as a better alternative to arrays in the standard library. For example:

Vector <int> ivec (10); // a vector with ten int objects
Vector <string> SVEC (10); // a vector with 10 string objects

There are more usage:

// Ways of creating a vector object
Vector <int> vec0; // empty vector

Const int size = 8;
Const int value = 1024;

// Vector of size 8,
// Each element initialized to 0
Vector <int> vec1 (size );

// Vector of size 8,
// Each element initialized to value 1024
Vector <int> vec2 (size, value );

// Vec3 is of size 4
// Initialized to the four values of IA
Int Ia [4] = {0, 1, 1, 2 };
Vector <int> vec3 (IA, Ia + 4 );

// Vec4 is a copy of vec2
Vector <int> vec4 (vec2 );

The attachment Code contains an example of how to use a vector (including how to traverse a vector)

Chapter3 C ++ Data Types

Section 1 literal constant
1. Char, short, Int, and long are all numeric types. They both have two types: signed and unsigned. The signed type, that is, the highest bit represents the sign bit, the unsigned type, that is, all bits are used to represent numbers. Therefore, the value range of the signed type is-128-127, and that of the unsigned type is 0-255.
2. There are three methods to represent numbers: decimal, octal, and hexadecimal. The octal is to add 0 to the front of the number, and the hexadecimal is to add 0x to the front of the number.
3. integer is regarded as signed by default. To declare an unsigned constant integer, you can add U/u to the end of the number (case-insensitive, it is recommended to use uppercase ). Similarly, if you want to express a constant Integer as a long type, you can add L/L to the end (both case and case are acceptable. We recommend that you use uppercase letters ). For example, 128u 1024ul 1l 8lu
4. floating point number, such as 3.14159. By default, the constant floating point number is considered to be of the double type. If you want to define it as a float type with single precision, you can add F/f after the constant (both case and case are acceptable, it is recommended to use uppercase letters), such as 3.14159f. Of course, if the floating point is represented by scientific and technical means, this is not acceptable.
5. Character-Char. Constant characters use single quotes, such as 'A '. In addition, there are some escape characters, such as '\ n' \ t', which can also be expressed in octal notation (indicating that the character is also a numeric type ), for example, \ 7 (Bell) \ 14 (newline) \ 0 (null) \ 062 ('2 '). Add an L (uppercase) before the constant character to indicate that this is a wide-character. The corresponding type is wchar_t. The width character can be used to represent Chinese characters.
6. String. A string is a string of double quotation marks. A string that cannot be expressed in one row. A back-slash can be used at the end of the row to continue. Before a constant string, you can also use L to represent a wide string consisting of Wide-Char, such as l "hello". Note that the ending character \ 0 of the wide string is also a wide character.
7. Differences between characters and strings. The end of the character is not \ 0, and the end of the string is a \ 0 character. Therefore, for Compiler, 'A' and "A" are different.
8. When two strings are adjacent, compiler automatically connects the two strings and adds a \ 0 at the end. For example, "two" "some" will be expressed as twosome, but note that a string and a wide string cannot be connected, such as "two" L "some ", this is not acceptable.

Section 2 variable
1. variables are different from text constants and constants (variables modified with const). variables have two attributes: read value and Location Value, the latter two generally only have read value (that is, we generally cannot operate on their location value ).

Section 3 pointer types
1. When defining a pointer variable, it is strongly required to write * to the beginning of the variable rather than following the type. For example, string * ps, rather than string * ps. The reason for this is to misunderstand the definition of multiple pointers at the same time. For example, string * PS and PS2. This defines a PS pointer and a PS2 string variable. If this is used, it is easy to misunderstand that PS and PS2 are pointers, we need to change it to this: string * ps, pS2; which is clear and difficult to misunderstand.
2. int * Pi = 0; set the pointer to null (null is 0)
3. pointers of different types cannot be assigned values to each other. For example:
Double dval; double * Pd = & dval;
Int IPI; int * Pi = & IPI;
Pi = Pd; // error!
The error here is not because the pointer values cannot be assigned to each other (in essence, No matter what pointer, its own values only occupy four bytes of space. In a 32-bit system, therefore, it is okay to assign values to each other), but because PI and PD point to different variable types, the compiler explains the objects they point, the object size and hierarchy are also different. Therefore, pointers of different types cannot be assigned to each other.
4. Void * indicates that this is a pointer, but the variable type pointed to by this pointer is unknown. Therefore, void * can only be used to assign values and compare them with some pointer addresses.
5. pointer operation: pointer arithmetic. For example:
Int I = 9;
Int * Pi = & I;
Pi = PI + 2;
As shown above, after the PI + 2 operation, the PI address will be removed from the two int values. That is to say, when a pointer is operated, N object addresses will be moved forward/backward according to the size of the object pointed to by the pointer. In the most typical case, in an array, if A is a pointer to the address starting with the array, then after a + 2, it will point to the third element of the array!

 

Related Article

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.