In fact, C + + structure can be understood as a python-like dictionary, I personally understand, but there are differences
First look at the structure
#include <iostream> keyword tags become the name of the new type struct inflatable{ std:: String mystr; struct member char name[]; float volume; Double Price ;};
C + + can omit the keyword struct when declaring a struct variable
Also pay attention to external declarations, and local declarations
#include <iostream>#include<string>#include<cstring>structinflatable{std::stringmystr; Charname[ -]; floatvolume; DoublePrice ;};intMainintargcChar Const*argv[]) { using namespacestd; Inflatable Guest= { "Hello", "Glorious Gloria", 1.88, 29.99 }; Inflatable PAL= { " World", "Audacious Arthur", 3.12, 32.99 }; intA=12.40; Std::cout<< Guest.mystr <<'\ n'; Std::cout<< a <<'\ n'; Std::cout<<"Expand your guest list with <<"<< Guest.name <<">>"<<" and"<<"<<"<< Pal.name <<">>"<<'\ n'; Std::cout<<"You can have both for $"<< Guest.price + pal.price <<'\ n'; return 0;}View Code
Other Structure Properties
#include <iostream>#include<string>#include<cstring>structinflatable{std::stringmystr; Charname[ -]; floatvolume; DoublePrice ;};intMainintargcChar Const*argv[]) { using namespacestd; Inflatable Guest= { "Hello", "Glorious Gloria", 1.88, 29.99 }; Inflatable Choice=Guest; This method calls the member to assign a value
Or
Inflatable choice;
Choice = Guest;
Std::cout<<"Expand your guest list with <<"<< Guest.name <<">>"<<'\ n'; Std::cout<<"Choice CHOICE.MYSTR---->"<< Choice.mystr <<'\ n'; return 0;}
It's OK
struct inflatable{ std::string mystr; Char name[]; float volume; Double = {"hello""glorious"1.11 One};
Of course, you can also not assign a value
Array of structures
You can also create an array of elements as structs, and the methods are identical to creating arrays of primitive types. For example:
#include <iostream>#include<string>#include<cstring>structinflatable{std::stringmystr; Charname[ -]; floatvolume; DoublePrice ;} Mr_glitz= {"Hello","Glorious",1.11, One};intMainintargcChar Const*argv[]) { using namespacestd; Inflatable guests[2] = { {"Hello","Doman",2.1,2.22}, //{"World", "Corleone", 2.2, 3333} }; Std::cout<<"Guests[0].mystr:"<< guests[0].mystr <<'\ n'; Std::cout<<"Guests[1].name:"<< guests[1].name <<'\ n'; return 0;}
Bit fields in the structure
struct torgle_register{ int SN: 4; int :4; BOOL 1 ; BOOL 1 = {truefalse};
C + + composite type (struct)