A few C + + syntax to use for data structure

Source: Internet
Author: User

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&AMP;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

Related Article

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.