C + + Learning notes (ii)--Basic knowledge __c++

Source: Internet
Author: User

1. Comma operator

A comma expression is a comma-separated array of expressions that are computed from left to right. the result of a comma expression is the value of its rightmost expression . If the rightmost operand

Is the left value, the value of the comma expression is also the left value. This type of expression is typically used for a For loop:

int cnt = Ivec.size ();

for (Vector<int>::size_type IX = 0; IX!= ivec.size (); ++size,--cnt)

{

Ivec[ix] = cnt;

}

The For statement above causes IX to increase by 1 and CNT to subtract 1 in a circular expression. Each loop modifies the value of IX and CNT. When the condition judgment of the Test IX is established, the program will be the next element

Reset to the current value of the CNT.


2. Three mesh (yuan) operator (?:)

Max = (A > B)? A:b

if (a>b) max = A;

else max = b;


X>y 1:1.5 converts 1 to double


3. Function

(1) Built-in functions:

Calling a function requires a certain amount of time and space overhead. C + + provides a way to improve efficiency by locking the calling function code directly into the main function at compile time, this embedding

The functions in the main function are called built-in functions.

Note: Simple functions that are small and frequently called are suitable for declaring as inine functions.

(2) Overload of the function

overloaded functions have the same function name, number of arguments, parameter type, parameter order at least one different

(3) function template

function templates apply to the same function body, the function of the same number of parameters and different types of cases

Template <typename t>//template declaration in which T is a type parameter can also be template <class t>

T Max (t A, T B, t C)//define a universal function, using t as the virtual function name

{

if (b>a) a=b;

if (c>a) a=c;

return A;

}


The type parameter can be more than one, and you can determine the number as needed

such as: template < class T1, TypeName t2>


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.