The data structure textbook (see P8,P9) uses the following three C + + knowledge points (recommended for active use in C course design, preparing for upcoming courses).
Note that all programs are saved with the CPP suffix (do not save with. c)
1 input and output (CIN cout replaces scanf printf)
#include <iostream>#include<iomanip>using namespacestd;intMain ()//The main function can not void{ intA; floatb; DoubleC; //basic Input, no conversion instructions such as%d are requiredCin >> a >> b >>C; cout<< a <<' '<< b <<' '<< C <<'\ n'; //keep two decimal placesA =3; b=54.6f; C=1.67895; cout<<fixed<< Setprecision (2) << a <<' '<< b <<' '<< C <<'\ n'; //widthcout << SETW (8) << a <<'\ n'; return 0;}
After entering 1 2 3, the output is as follows:
1 2 3
3 54.60 1.68
3
2. New Delete replaces malloc free
1#include <iostream>2#include <string>3 using namespacestd;4 structStudent {//declaring struct-body type student5 stringname;6 intnum;7 Charsex;8 };9 Ten intMain () One { AStudent *p;//A pointer variable that defines the data that points to a struct type student -p =NewStudent;//using the new operator to open up a space for storing student-type data -P->name ="Wang Fun";//assigning values to members of struct variables theP->num =10123; -P->sex ='m'; -cout << p->name << Endl << p->Num -<< Endl << p->sex << Endl;//output values for each member +Delete p;//Undo the Space - return 0; +}
3. Replacing pointers with references
1#include <iostream>2#include <iomanip>3 using namespacestd;4 5 voidSwapfloat&a,float&B)//The reference a here is considered to be the alias of main function a6 {7 floattemp;8temp =A;9A =b;Tenb =temp; One } A - intMainvoid) - { the floatA, B; -Cin >> a >>b; - Swap (A, b); -cout << a <<' '<< b <<Endl; + return 0; -}
A few C + + syntax to use for data structure