C++笔记(to be cont'd)

时间:2023-03-09 09:20:08
C++笔记(to be cont'd)

最近在看这个:LearnCpp

主要是一些我自己容易忽视的地方,因为原来的网站更新很快,所以章节序号不一定准


CH1.10 Preprocessing

The preprocessor copies the contents of the included file into the including file at the point of the #include directive

Conditional Compilation:

#ifndef SOME_UNIQUE_NAME_HERE
#define SOME_UNIQUE_NAME_HERE
#endif

CH2.1 Fundamental variable definition, initialization, and assignment

Favor implicit initialization over explicit initialization

int nValue = 5; // explicit initialization
int nValue(5); // implicit initialization

Uniform(list) initialization in C++11

int value{}; // default initialization to 0
int value{4.5}; // error: an integer variable can not hold a non-integer value

If you’re using a C++11 compatible compiler, favor uniform initialization


CH2.4 Integers

While short int, long int, and long long int are valid, the shorthand versions short, long, and long long should be preferred. In addition to being less typing, adding the prefix int makes the type harder to distinguish from variables of type int. This can lead to mistakes if the short or long modifier is inadvertently missed.

long long int lli; // valid
long long ll; // preferred

All integer variables except char are signed by default. Char can be either signed or unsigned by default (but is usually signed for conformity).

Generally, the signed keyword is not used (since it’s redundant), except on chars (when necessary to ensure they are signed).

Favor signed integers over unsigned integers


CH2.4a Fixed-width integers
<cstdint>:fixed width integers: int8_t/uint8_t/...uint64_t

Until this is clarified by a future draft of C++, you should assume that int8_t and uint8_t may or may not behave like char types.

int can be used when the integer size doesn’t matter and isn’t going to be large.

Fixed-width integers should be used in all other cases.

Only use unsigned types if you have a compelling reason.


CH2.5 Floating point numbers

Summation precision: Kahan summation algorithm

#include <iomanip> // for std::setprecision()

Float values have between 6 and 9 digits of precision, with most float values having at least 7 significant digits. Double values have between 15 and 18 digits of precision, with most double values having at least 16 significant digits. Long double has a minimum precision of 15, 18, or 33 significant digits depending on how many bytes it occupies.

Favor double over float unless space is at a premium, as the lack of precision in a float will often lead to challenges.

Rounding Error matters 0.1+...+0.1 \neq 1.0. Ref. CH3.5 -- Relational operators


CH2.7 Chars

Note that even though cin will let you enter multiple characters, ch will only hold 1 character. Consequently, only the first input character is placed in ch. The rest of the user input is left in the input buffer that cin uses, and can be accessed with subsequent calls to cin.

\n and endl:

Use std::endl when you need to ensure your output is output immediately (e.g. when writing a record to a file, or when updating a progress bar). Note that this may have a performance cost, particularly if writing to the output device is slow (e.g. when writing a file to a disk).

Use ‘\n’ in other cases.

wchar_t should be avoided in almost all cases (except when interfacing with the Windows API). Its size is implementation defined, and is not reliable. It has largely been deprecated.

You won’t need to use char16_t(UTF-16) or char32_t(UTF-32) unless you’re planning on making your program Unicode compatible and you are using 16-bit or 32-bit Unicode characters.


CH2.9 Const, constexpr, and symbolic constants

Making a function parameter const does two things. First, it tells the person calling the function that the function will not change the value of myValue. Second, it ensures that the function doesn’t change the value of myValue.

Any variable that should not change values after initialization should be declared as const (or constexpr in C++11).

Avoid using #define to create symbolic constants, but use const variables to provide a name and context for your magic numbers.

A recommended way:

  1. Create a header file to hold these constants
  2. Inside this header file, declare a namespace
  3. Add all your constants inside the namespace (make sure they’re const)
  4. #include the header file wherever you need it

Use the scope resolution operator (: