preface

The C++ language has been around for more than 30 years, mainly in industries that need to run efficiently, such as game development and efficient servers.

There is no denying that C++ is more difficult to learn than other languages. Its difficulty mainly lies in its complexity. How to get started with C++, most people have had a headache. Speaking of entry, we first master the basic grammar, C++ grammar and C language grammar is very similar.

**1. Int: any integer in the range of 10^9 can be defined as an int.

** If the assignment of long long is greater than the initial value of 2^ 31-1, it needs to be followed by LL, otherwise a compilation error will occur.

Printf (“%f”,c); Statement, %f is the output format of float and double. When using scanf, float is entered as %f and double as %lf.

** If (n) indicates if(n! =0), so if(! N) said the if (n = = 0)

** note that C does not allow for A for(A; B; C) statement A defines variables (e.g. Inta =0; Is not allowed), but in C++ it is

Array definition: ** If the array size is large (10^6 level or higher), it needs to be defined outside the main function, otherwise the program will exit abnormally.

If we define an array int array[10]={12,1,6,4}. If only the first four elements of the array are assigned, the elements that are not assigned default to 0. If the array has no initial value, each element in the array may be a random number (not necessarily 0). So if you want to assign a value of 0 to the entire array, here are four simple methods:

  • int array[10]={};
  • int array[10]={0};
  • Memset: memset(array,0,sizeof(array)); Int array[10]; sizeof(array)=4*5=20

Memset is byte assignment. Format memset(array name, value,sizeof);

  • The fill function: the fill (array, the array + 5, 8); // Array from array[0] to array[4] is assigned a value of 8, fill(start address, end address, value);

**memset() requires headers or <string.h>. And memset() can only assign initial values of 0 and -1 to arrays, because memset() can only assign values in bytes to fill(). And fill() can assign an arbitrarily identical value to a region of an array or container.

8. Two-dimensional array assignment:If the two dimensional array array [4] [5] = {{1, 2, 3, 4, 5}, {2, 2, 3}, {}, {4,2,3,5}}; You can see that the third line is skipped with {}(It will not compile without parentheses)

Character array initialization: ** character arrays can be initialized by assigning strings directly (initialization only; assigning entire strings directly is not allowed elsewhere in the program).

Char STR [10]; scanf(“%s”,str); pritnf(“%s”,str); Note that character arrays are entered without the ampersand operator. %s uses a space or a newline to identify the end of a string. Gets uses a newline character \n to identify the end of a string. Puts outputs a line of string followed by a newline (\n)

**11. Importance of character array length: the ** character array must be at least one longer than the actual length of the stored string, as the terminator ‘\0’. Note Arrays of type int are not required. And if you don’t use scanf’s %s format or the gets function to input strings (such as getchar), be sure to manually append ‘\0’ to each early string, otherwise the printf and puts output strings will not recognize the end of the string and will output a lot of garbled characters.

Sscanf :**sscanf: int n; char str[10]=”123″; sscanf(str,”%d”,&n); Sprintf: int n; sprintf: int n; sprintf: int n; sprintf: int n; char str[10]=”123″; sprintf(str,”%d”,n); // Sprintf writes n as “%d” to STR (from right to left)

**13. Reference & :** Note to distinguish the referenced & from the address-fetch operator &. A reference does not take an address. A reference does not make a copy, but gives the original variable a separate name. Because references are aliases for producing variables, constants cannot use references. So:

Instead of writing it as swap(&a,&b), we must store &a and &b with pointer variables P1 and p2, and pass the pointer variables as arguments.

14. Access elements within the structure:

Id stu,name stu.next

Access the pointer variable p elements of writing: (* p). Id < = > p – > id (* p). The name < = > p – > name (* p). Next < = > p – > next

15. Initialization of the structure

** Note: ** if you redefine the constructor yourself, the default generated constructor will be overridden. If you define struct variables without initialization, add them manually.