C++ Primer 读书笔记1(一)

2015-01-26 23:12:53 · 作者: · 浏览: 24

1、在Windows系统中输入文件结束符的方法为Ctrl+Z

2、如何选择类型?

1) Use an unsigned type when youknow that the values cannot be negative

2) Use int for integer arithmetic.short is usually too small and, in practice, long often has the same size asint. If your data values are larger than the minimum guaranteed size of an int,then use long long.

3) Do not use plain char or boolin arithmetic expressions. Use them only to hold characters or truth values.Computations using char are especially problematic because char is signed onsome machines and unsigned on others. If you need a tiny integer, explicitlyspecify either signed char or unsigned char.

4) Use double for floating-pointcomputations; float usually does not have enough precision, and the cost ofdouble-precision calculations versus single-precision is negligible. In fact,on some machines, double-precision operations are faster than single. Theprecision offered by long double usually is unnecessary and often entailsconsiderable run-time cost.

signed char c2 = 256;   // assuming 8-bit chars, the value of c2 is undefined

3、If we assign anout-of-range value to an object of signed type, the result is undefined. Theprogram might appear to work, it might crash, or it might produce garbageva lues.

4、Caution: Don’t Mix Signed and Unsigned Types.

5、 'a' // character literal

"HelloWorld!" // string literal

The type of a string literal is array of constant chars. Thecompiler appends a null character (’\0’) to every string literal. Thus, theactual size of a string literal is one more than its apparent size. Forexample, the literal 'A' represents the single character A, whereas the stringliteral "A" represents an array of two characters, the letter A andthe null character.

// multiline string literal
 std::cout << "a really, really long string literal "             
	      "that spans two lines" << std::endl;

6、When you write a long literal,use the uppercase L; the lowercase letter l is too easily mistaken for thedigit 1.

7、The word nullptr is a pointerliteral.

8、Initialization is notassignment. Initialization happens when a variable is given a value when it iscreated. Assignment obliterates an object’s current value and replaces thatvalue with a new one.

9、The generalized use of curlybraces for initialization was introduced as part of the new standard. This formof initialization previously had been allowed only in more restricted ways,this form of initialization is referred to as list initialization.

When used with variables of built-in type, this form ofinitialization has one important property: The compiler will not let us listinitialize variables of built-in type if the initializer might lead to the lossof information:

long double ld = 3.1415926536; 
int a{ld}, b = {ld}; // error: narrowing conversion required 
int c(ld), d = ld;   // ok: but value will be truncated
10、We recommend initializing everyobject of built-in type. It is not always necessary, but it is easier and saferto provide an initializer until you can be certain it is safe to omit theinitializer.

11、A declaration makes a name known to the program. A file that wants touse a name defined elsewhere includes a declaration for that name. Adefinition creates the associatedentity.

extern int i;   // declares but does not define i 
int j;          // declares and defines j
extern double pi = 3.1416; // definition

It is an error to provide an initializer on an externinside a function.

Note: Variables must be defined exactly once but can be declared manytimes.

12、 Warning: It isalmost always a bad idea to define a local variable with the same name as aglobal variable