C + + Primer reading notes finishing (i)

Source: Internet
Author: User

1. When reading a variable amount of input data, the CIN statement can be placed in the conditional judgment statement, if the stream's state is valid, the read succeeds, otherwise the read fails.

For example:

 while (cin>>value)        /* */

2. The difference between the top-level const and the underlying const

1) Top-level const means that arbitrary objects are const (that is, constants)

For example:

Const int 0;  // I is the top-level const constant int 0 ; INT *const ptr = &i;  // PTR is the top-level const constant

2) The underlying const is typically used in references and pointers, meaning that the object or reference to which the pointer refers is a constant

For example:

Const int ;    // top-Level const Const int &ref = CI   //ref is the underlying constconstint//  PTR is the underlying const

3. Constant expressions and constexpr

A constant expression is an expression whose value does not change and can be evaluated during compilation . Allows a variable to be declared as a constexpr type for the compiler to validate the variable

Whether the value is a constant expression, if it is not, it will be an error.

In a constexpr declaration, if a pointer is defined, the qualifier constexpr is valid only for pointers, regardless of the object that the pointer refers to.

For example:

Const int // p is a pointer to an integral type constant Const int // Q is a constant pointer to an integral type

Be aware of the difference between the two.

4.const and type aliases

Look at the following code:

Char *pstring; Const 0 // CStr is a constant pointer to char const pstring *ps;       // PS is a pointer to an object that is a constant pointer to a char

Pstring is originally a char-type pointer, but with a typedef it represents a type, the char* type, remembering thatConst is a modification of a given type .

const pstring CStr = 0; the const in is CStr, while the const pstring *ps , in which the const modifier is the object to which the derivation is directed, and PS itself is not a constant.

If it is a const pstring * Const PS , then PS is a constant pointer to the object that is a constant pointer to char, that is, PS is a two-weight pointer.

5.decltype

decltype (expression) , if the content of an expression is a dereference operation, then Decltype will get the reference type, for example:

int *p = &i; //

Remember: 1) The result of decltype (expression) (double-sided brackets) is always a reference and the decltype (expression) result is a reference only if the expression itself is a reference.

2) Decltype The type that is obtained when acting on an array (or function name) is a pointer to an array (or functions) rather than to an array (or function). (This sentence appears in the C++primer 5 times)

6.getline function

The Getline function has a member version and a non-member version, and the member version is in the standard library container, where the non-member version is introduced. Non-member versions are:

getline (Input stream, string object, specified delimiter) , the function reads the object from a given input stream into a string object until the specified delimiter is encountered, specifying

Delimiter is typically a newline character.

7. Scope for statement

The form is:

 for (declaration:expression)    {/**/}

Where the expression part is an iterative object, the declaration section is responsible for defining a variable to access each element of an iterative object individually, each iteration, declaration part

Variables are initialized to the next element in the expression section, for example:

vector<int> VEC = {1,2,3,4};  for (int  i:vec)    std::cout<<i<<'; /* */

The definition of a range for statement comes from a traditional for statement that is equivalent to:

 for (Auto Beg = V.begin (), end = V.end (); beg!=end;++Beg)       /* */}

Note: 1) You cannot add elements of a vector or other container through the range for statement. Because the value of end () is stored in the range for statement, and a single element is added to the sequence, the value of the End function may become invalid.

2) The range for statement can also be used to iterate through the elements in the array.

Not to be continued ...

C + + Primer reading notes finishing (i)

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.