C ++ getting started

Source: Internet
Author: User

To use cout and cin, you must include the input and output libraries provided by C ++. # Include <iostream> standard input/output header file

<String>

Using namespace std; // defines the namespace used in the program. If this command is not added, the following cout will be changed to std :: cout (cout operation in std scope)

 

Variables in C ++ can be defined and used at any time.

 

When the input and output library is used, the output will be kept in the buffer (Optimized Output operation ).

Refresh the buffer:

1. The buffer is full.

2. The request library reads data from the standard input.

3. Request to refresh the buffer

 

String initialization method:

Const string hao = "hello" + "!!" // Define a string and initialize it as hello !!

Const string hu = "hu" + hao. // use the + operator to connect two strings and define another string as huhello !! Two string constants cannot be connected. This + produces different effects because the operands on both sides of the string are different. Different objects of the same operator have different effects and are considered as heavy-duty operators.

Function overloading allows several functions to have the same function name. However, different input parameters make different functions.

Const string spaces (hu. size (), ''); // specify a string and fill in the content of the specified length.

 

Character constants and string constants:

Character constants are enclosed by single quotes, while string constants are enclosed by double quotes.

 

Const Keyword:

When defining a variable, it ensures that the variable can only be read but cannot be changed during the lifetime of the variable. An initial value must be included in the definition.

 

 

String object

 

Some stream controllers, such as endl, output indicates a line feed.

Cout <setprecision (3) to set a specific number of valid digits for subsequent output of the stream.

 

For (I = 2; I <5; I ++ ){

// Form a left closed, right open range [), value range: 2, 3, 4

}

If the variable is defined in the for initialization phase, the variable cannot be accessed after it.

 

The vector type is a container that stores data sets.

Vector <double> homework; // defines a vector-type storage container (named homework). Each data type is double.

A vector of double type and its name is homework.

 

Method of access Vector element homework [mid]

Using the sort algorithm in the library, sort (homework. being (), homework. end (); sort the data in the container in non-descending order (insert sort, O (Nlog (N )).

 

If (size = 0)

Throw domain_error ("media of an empty vector ")

If the program throws an exception, it terminates the execution at the place where the exception is thrown and transfers it to another part of the program, and provides an exception object to this part. The exception object contains information that the caller can use to handle exceptions.

 

When a function is required to return multiple values, the usual practice is to use the return function to execute the operation successfully or fail, the operation result is transmitted using the reference parameter or pointer parameter of the function.

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.