C + + notes (to be cont ' d)

Source: Internet
Author: User

Recently looking at this: learncpp.com
It's mostly something I can easily ignore.
Note Some notes below, chapter number corresponding to the main page of the chapter number, and the wrong place please advise

ch1.3a
Rule:avoid "using" statement outside of a function body.
======

CH1.10
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
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
While short int, long int, and long long int is valid, the shorthand versions short, long, and long long should is prefer Red. In addition to being less typing, adding the prefix int makes the type harder to distinguish from variables of type int. T His can leads 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 is signed by default. Char can be either signed or unsigned by default (but was usually signed for conformity).
Generally, the signed keyword is not used (since it's redundant), except on chars (when necessary to ensure they was Signe D).
Favor signed integers over unsigned integers


======
ch2.4a
<cstdint>:fixed width integers:int8_t/uint8_t/...uint64_t
Until this was clarified by a future draft of C + +, you should assume that int8_t and uint8_t could or may isn't behave like Cha R types.


int can is used when the integer size doesn ' t matter and isn ' t going to be large.
Fixed-width integers should is used in all other cases.
Only the use of unsigned types if you have a compelling reason.


======
CH2.5
Summation Precision:kahan summation algorithm
#include <iomanip>//For Std::setprecision ()

Float values has between 6 and 9 digits of precision, with the most float values had at least 7 significant digits. Double values has between and digits of precision, with the most double values had at least significant digits. Long Double has a minimum precision of $, or significant digits depending on how many bytes it occupies.
Favor double over float unless space was at a premium, as the lack of precision in a float would often leads to challenges.

Rounding Error matters:0.1+...+0.1 \NEQ 1.0. Ref. CH3.5--relational operators


======
CH2.6
Ture is evaluated as 1


======
CH2.7
Note that even though CIN would let you enter multiple characters, CH would only hold 1 character. Consequently, only the first input character was placed in Ch. The rest of the user input is a left in the input buffer that CIN uses, and can be accessed with subsequent calls to CIN.

\ Endl:
Use Std::endl if you need to ensure your output are output immediately (e.g. when writing a record to a file, or when upd Ating a progress bar). Note that this may has a performance cost, particularly if writing to the output device are slow (e.g. when writing a file to a disk).
Use ' \ n ' in the other cases.

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

You won ' t need-char16_t (UTF-16) or char32_t (UTF-32) unless you ' re planning on Making your program Unicode COMPATIBL E and you are using 16-bit or 32-bit Unicode characters.


======
CH2.8
0x 16; 0 8; 0b 2 (C++14)


======
CH2.9
Making a function parameter const does-things. First, it tells the person calling the function, the function won't change the value of myvalue. Second, it ensures the function doesn ' t change the value of myvalue.

Any variable this should not a change of values after initialization should is declared as const (or constexpr in c++11).

Avoid using #define to the Create symbolic constants, but with the const variables to provide a name and the context for your magic num Bers.

A Recommended:
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 (::) To access your constants in. cpp files


======
CH3.2
Prior to C++11, if either of the operands of the integer division be negative, the compiler is free to round up or down! For example, -5/2 can evaluate-either-3 or-2, depending on which "the" compiler rounds. However, most modern compilers truncate towards 0 (so-5/2 would equal-2). The C++11 specification changed this to explicitly define so integer division should always truncate towards 0 (or put m Ore simply, the fractional component is dropped).

Also prior to c++11, if either operand of the modulus operator are negative, the results of the modulus can be either Negat Ive or positive! For example,-5 2 can evaluate to either 1 or-1. The C++11 specification tightens this and so, a% B always resolves to the sign of a.


======
CH3.3
Be aware of undefined expressions like:x = x + +;
Don ' t use a variable, a side effect (if it modifies some state), applied to it, than once in a given statement.


======
CH3.4
Avoid using the comma operator, except within for loops.
Only with the conditional operator for simple conditionals where it enhances readability.
It's worth noting that the conditional operator evaluates as an expression, whereas If/else evaluates as statements. This means the conditional operator can is used in some places where if/else can is not.
For example, when initializing a const variable:

BOOL Inbigclassroom = false;
const int classsize = Inbigclassroom? 30:20;

There ' s no satisfactory if/else statement for this, since const variables must is initialized when defined, and the Initia Lizer can ' t be a statement.


======
CH3.5
Directly comparing floating point the values using any of the these operators is dangerous. This was because small rounding errors in the floating point operands may cause unexpected results.

Donald Knuth, a famous computer scientist, suggested the following method in its book "The Art of computer programming, Vo Lume ii:seminumerical Algorithms (Addison-wesley, 1969) ":
#include <cmath>

Return true if the difference between A and b is within epsilon percent of the larger of A and b
BOOL Approximatelyequal (double A, double b, double epsilon)
{
Return Fabs (a) <= ((Fabs (a) < Fabs (b) fabs (b): Fabs (a)) * epsilon);
}

The author suggests the following approach
Return true if the difference between A and B are less than Absepsilon, or within relepsilon percent of the larger of a and b
BOOL Approximatelyequalabsrel (double A, double b, double Absepsilon, double Relepsilon)
{
Check If the numbers is really close--needed when comparing numbers near zero.
Double diff = Fabs (A-B);
if (diff <= Absepsilon)
return true;

Otherwise fall back to Knuth ' s algorithm
Return diff <= ((Fabs (a) < Fabs (b) fabs (b): Fabs (a)) * Relepsilon);
}


Comparison of floating point numbers is a difficult topic, and there's no "one size fits all" algorithm the works for Eve Ry case.


======
CH3.6
Any Non-zero integer value evaluates to True when used in a Boolean context. Mixing Integer and Boolean operations can be very confusing, and should be avoided!

Short Circuit evaluation presents another opportunity to show what operators that cause side effects should is used.


======
CH3.8
When the dealing with bit operators, use unsigned integers.
Note that the results of applying, the bitwise SHIFT operators to a signed integer is compiler dependent.


======
ch3.8a
bit mask and bit flags:
Bit flags is typically used in the cases:
1) When you have many sets of identical bitflags.
2) Set options Easily especially there is a lot of options (consider F (arg1,arg2...arg100))

Manage Bitflags:std::bitset

Bit Mask:one Application:color Channel


======
CH4.1
Note that variables inside nested blocks can has the same name as variable inside outer blocks. When this happens, the nested variable "hides" the outer variable. This is called name hiding or shadowing.


======
CH4.2
By convention, many developers prefix global variable names with "G_" to indicate that they is global. This both helps identify global variables as well as avoids naming conflicts with local variables.

By default, non-const variables declared outside of a block is assumed to be external. However, const variables declared outside of a block is assumed to be internal.

Encapsulate the global variable.


======
CH4.3
Static variables offer some of the benefit of the global variables (they don ' t get destroyed until the end of the program) WHI Le limiting their visibility to block scope. This makes them much safer for use than global variables.

C + + notes (to be cont ' d)

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.