1, Input and output
C + + language does not define any input and output statements, but provides a comprehensive standard library to provide IO mechanism.
iostream contains two basic types: istream and ostream, representing input and output respectively.
cin(istream) is the standard input; cout(ostram) is the standard output; cerr and clog (ostream). cerr is used to output warning and error information, and clog is used to output general information when the program runs.
std::cout<<"ostream:"<<std::endl;//output int i=0; std::cout<<"Please enter a number:"; std::cin>>i;//input
cin (> >) and cout (< <) are the most commonly used in programs.
The endl in the above code segment is equivalent to a newline '\ n';
std::cout is used in the above program instead of cout. The prefix std:: indicates that cout, cin and endl are defined in the namespace named std, and all names defined by the standard library are in the namespace std.
It's too annoying to write all like this: we can use using namespace std; to declare.
#include "iostream" using namespace std; int main(){ cout<<"ostream:"<<endl;//output int i=0; cout<<"Please enter a number:"; cin>>i;//input }
You can write cout, cin and endl directly.
C + + arithmetic type:
It is divided into two categories: integer and floating point
type | meaning | Minimum size |
bool | Boolean type | Undefined |
char | character | 8 bits |
wchar_t | Wide character | 16 bit |
char16_t | Unicode character | 16 bit |
char32_t | Unicode character | 32 bit |
short | Short shaping | 16 bit |
int | plastic | 16 bit |
long | Long shaping | 32 bit |
long long | Long shaping | 64 bit |
float | Single-precision floating-point | 6 significant digits |
double | Double precision floating point number | 10 significant digits |
long double | Extended precision floating point number | 10 significant digits |
Unicode in the above table is the standard used to represent characters in all natural languages.
The value of boolean type (bool) can only be true (true) or false (false).
When we write a program, it is recommended to initialize each built-in type.
Signed and unsigned types
With the exception of Boolean and extended character types, other integers can be divided into signed and unsigned. Signed can represent negative numbers, while unsigned can only represent values greater than or equal to 0.
Type int, short, long, long They are all signed. If you add unsigned in front of them, they can become unsigned. Mainly remember that they are signed. In other cases, unsigned shaping can be abbreviated as unsigned.
Relationship between variable declaration and definition:
extern int i;//This is the declaration i, not the definition i int j;//This is a declaration and definition j
Variables can only be defined once under the same scope, but declared multiple times.
It is recommended that when defining a variable, you define it for the first time, and do not define it in advance.
C + + identifiers are composed of letters, numbers and underscores, and must start with letters or underscores. Identifiers have no limit on length, but are case sensitive, and cannot be named the same as C + + keywords.
Here is also the naming convention of variable names:
1. The identifier shall reflect the actual meaning as much as possible.
2. Variable names are generally in lowercase letters.
3. Self defined class names generally begin with capital letters.
4. If the identifier is composed of multiple words, there should be obvious distinguishing signs between words.
Scope of Name:
Scope: most scopes in C + + language are separated by curly braces. The valid area of the name begins with the declaration statement of the name and ends with the end of the scope where the declaration statement is located.
#include <iostream> int main(){ int sum=0; for(int i=0;i <=10 ; i++){ sum+=i;//Equivalent to sum = sum +i } cout<<"sum= "<<sum<<endl; return 0; }
Global scope (global variable): it can be accessed during the whole process of code execution; local scope (local variable): like the i variable in the above code, it is defined in the for loop. Once it comes out of the loop body, it cannot be accessed, but it can be redefined outside the loop body.
Nested scope: the scope containing other scopes is called outer scope; the included scope is called inner scope.
Suggestion: if a function may use a global variable, do not define a local variable with the same name.
Reference (alias):
A reference is another name for an object.
int ival =1024; int &refval = ival;//This refval is another name of the variable ival, which operates in the same space
When we use a reference, we must initialize it. Otherwise, an error will be reported. A reference is not an object, it is just another name of an existing object.
Pointer (base):
A pointer is a composite type that points to another type. The pointer itself is an object, and the pointer does not need to be given an initial value at the time of definition.
int *p1,*p2; //Both p1 and p2 are pointers to int objects double dp , *dp1; //dp is a normal double object, * dp1 is a pointer to a double object int ival = 42; int *p = &ival; //p is the address where the variable ival is stored, or p is a pointer to the variable ival //The type pointed to by the pointer must be the same as the type of content pointed to. The following is wrong: int ival1 =43; char * p1 =&ival1;//Their types do not match
Null pointer: the pointer does not point to any object.
int *p1 = nullptr;//A method of introducing the new standard of C++11 int *p2 = 0; int *p3 = NULL; //The above three are null pointers, which do not point to any object
C + + programmers should use nullptr to initialize NULL pointers and avoid using NULL;
Accessing objects with pointers: if the pointer points to an object, we can use the dereference character (*) to access the object pointed to by the pointer.
int ival =42; int *p =&ival; cout<< *P;//The value 42 of ival can be output through the dereference of pointer P; *P =0;//The value of the object pointed to by the pointer can be changed through the pointer cout<< *p;//Output 0
It is recommended to initialize all pointers. Using uninitialized pointers is a major cause of runtime errors.
When the pointer is conditional:
The condition corresponding to any non-0 pointer is true.
int ival = 1024; int *pi = 0 ;//Is a null pointer with a value of 0 int *pi2 = &ival; if(pi)//The condition is false because the value of pi is 0 //... if(pi2) //The value of pi2 pointing to the object is not 0 because some conditions are true //...
void* Is a special pointer type, which can store the address of any object. We can't directly operate the object it points to, because we don't know what the type of object it points to, and what it can do is relatively limited. From the perspective of void *, memory space is only memory space, and we can't access the objects stored in memory space. (this is just for understanding)
double obj =3.14 , *pd = &obj; void * pv =&obj;//void * can store the address of any type of object; obj can be any type of object pv= pd; //pv can store any type of pointer
Declaration of composite type:
int i =1024 , *p =&i , &r =i ; //i is an integer number, p is an int pointer, and r is an int reference int* p; // p is an int pointer, not an int * int* p1 , p2; //p1 is a pointer to type int, and p2 is a variable of type int
Pointer to pointer: * * indicates pointer to pointer, * * * indicates pointer to pointer. and so on
int ival =1024; int *p =&ival;//p points to a number of Int type int **pp=π//pp points to an Int pointer
Reference to pointer:
The reference itself is not an object, so a pointer to the reference cannot be defined. However, the pointer is an object, so there is a reference to the pointer
int i =42; int *p; //p is an int pointer int *&r = p ;//r is a reference to pointer p r= &i;//r refers to a pointer. Assigning & i to r is to make p point to i *r =0;//Dereference r to get i, that is, the object pointed to by p, and change the value of i to 0
The easiest way to understand the type of r is to read the definition of r from right to left. When we encounter more complex problems
const qualifier:
When we want to define that the value of a variable cannot be changed, we can add the const qualifier to the limit (integer constant)
const int du = 520; du = 520; //This is wrong. The value of du cannot be changed.
Once a const object is created, its value cannot be changed, so the const object must be initialized.
int i= 42; cin>>i; const int ci =i; //The value of i is copied to ci int j = ci; //The value of ci is copied to j
You can use a defined object to initialize a const object. When we enter the value of i and copy it to ci, the value of ci cannot be changed.
By default, const object is only valid in the file, that is, it is only valid in the file that declares and defines it. Other files cannot use it. We can use extend to implement it:
extern const int i=1100;
To share const objects among multiple files, you must add the extend keyword before the definition of the variable.
const reference (constant reference):
You can bind references to const objects (references to constants). Unlike ordinary references, a reference to a constant cannot be modified by the object it binds to:
const int ci = 1024; const int &ri =ci;//Correct: the referenced object is a constant ri = 42; // Error: ri is a reference to a constant int &r2= ci; //You cannot have a non constant reference point to a constant object
The type of the reference must be consistent with the type of the referenced object. There are two exceptions:
It is allowed to use any expression as the initial value when initializing a constant reference, as long as the result of this expression can be converted to the type of reference.
double dval = 3.14; const int &ri1 =dval; //The above code compiler will change to the following: const int temp = dval; const int &ri1 =temp;//Thus ri1 binds a temporary quantity
If ri1 in the above code is not a constant, the reference is bound to a temporary quantity. This behavior will be illegal in C + +. The following code is wrong and will report an error. It is not allowed to write like this!!!
double dval =3.14; int &ri=dval;
A reference to const may refer to an object that is not const:
//The reference to const may be a non const object int i = 42; int &fi= i; //Bind reference fi to object i const int &rh=i;// rh also binds object i, but it is not allowed to change the value of i through rh i=0;//i is not a constant, so you can change its value rh=0; //Error, rh is a constant reference and its value cannot be changed directly cout<<"i= "<<i<<endl; cout<<"rh= "<<rh<<endl;
Pointer and const:
To store the address of a constant object, you can only use a pointer to a constant;
const double pi = 3.14; double *ptr = π//Error because Ptr is an ordinary pointer const double *cptr = π //Correct, they are of the same type *cptr = 42;//Error, cannot assign value to * cptr
Allows a pointer to a constant to point to a non constant object. Like constant references, pointers to constants do not specify that the object they refer to must be a constant. A pointer to a constant simply requires that the value of an object cannot be changed through this pointer, and does not specify that the value of that object cannot be changed through other ways.
const Pointer (constant pointer):
A pointer is an object, so it is allowed to define a pointer itself as a constant. Constant pointers must also be initialized. Once initialized, its value (the address where it is stored) can no longer be changed.
Put * before const keyword to indicate that the pointer is a constant (invariant is the value of the pointer itself, not the value pointed to).
int nub= 0; int *const cur = & nub;//cur will always point to nub and will not change cosnt double pi = 3.124; const double *const pip =π//pip is a constant pointer to a constant object
When you encounter a complex definition, use the method we talked about earlier: read from right to left.
Take cur in the above code as an example: next to cur is const (from right to left), indicating that cur itself is a constant, and then the next symbol is *, indicating a pointer. Finally, cur is a constant pointer. (if you see here, please use this method to say the type of pip and type it in the comment area)
Top const:
Top const: indicates that the pointer itself is a constant.
Underlying const: indicates that the object referred to by the pointer is a constant.
The pointer can be both the top const and the bottom const.
int i = 0; int *const p1 =&i; //The value of P1 cannot be changed. This is a top-level const const int ci = 42; //The value of ci cannot be changed. This is a top-level const const int *p2 =&ci;//It is allowed to change the value of p2, which is an underlying const const int *const p3 = p2; //The const on the right is the top const, and the const on the left is the bottom const const int &r =ci ; //Consts used to declare references are the underlying const s
When copying an object, there is a clear difference between the top const and the bottom const, but the top const is not affected:
i =ci ;//Correct: copy the value of ci. ci is a top-level const, which has no effect p2 = p3;//Correct: the object types pointed to by p2 and p3 are the same, and the const part at the top level of p3 does not affect. //The copy operation does not change the value of the copied object. Whether the copied in and copied out objects are constants has no effect.
The limitations of the underlying const cannot be ignored:
When performing a copy operation, the copied object must have the same underlying const qualification, or the data types of the two objects must be convertible. Generally speaking, an extraordinary quantity can be converted into a constant, but not vice versa:
int *P = p3;// Error: p3 has the definition of underlying const, but P does not p2 = p3;//Correct: both p2 and p3 are the underlying const p2 = &i; //Correct: int * can be converted to const int* int &r =ci;//Error: normal Int & cannot be bound to Int constant const int &r2= i;//Correct: const Int & can be bound to a normal Int
In the above code, p3 is both the top-level const and the bottom-level const. When copying p3, you can ignore that it is a top-level const, but you must be clear that the object it points to is a constant (pay attention to its underlying const). The value of p3 can be assigned to p2 because they both have the properties of the underlying const. Although p3 also has the properties of the top const, this assignment will not have any impact.