Home > Back-end >  Access to the members of the class object function problem set container.
Access to the members of the class object function problem set container.

Time:12-25

 # include & lt; Iostream> 
# include & lt; Set>
# include & lt; String>
using namespace std;
//custom data sorting
The class Person {
Public:
The Person (string name, int age) {
This - & gt; M_Name=name;
This - & gt; M_Age=age;
}

Const string getName () {return this - & gt; M_Name; }
Const int getAge () {return this - & gt; M_Age; }

Private:
String m_Name;
Int m_Age;
};

Void test04 () {
Set SPerson;
The Person p1 (" liu ", 35);
The Person p2 (" cao ", 45);
The Person p3 (" sun quan ", 40);
The Person p4 (" zhaoyun, 25);
The Person p5 (" zhang fei ", 35);
The Person p6 (" guan yu ", 35);
SPerson. Insert (p1);
SPerson. Insert (p2);
SPerson. Insert (p3);
SPerson. Insert (p4);
SPerson. Insert (p5);
SPerson. Insert (p6);
For (set : : const_iterator it=sPerson. The begin (); It!=sPerson. End ();
It++)
Cout & lt; }

Int main () {
Test04 ();
return 0;
}


The above code compiled through, can you tell me where the problem is, thank you.

CodePudding user response:

In the set in a custom type Person, can be overloaded in the Person & lt; The operator
as follows[code=c + +]
The class Person {
Public:
The Person (string name, int age) {
This - & gt; M_Name=name;
This - & gt; M_Age=age;
}

Const string getName () {return this - & gt; M_Name; }
Const int getAge () {return this - & gt; M_Age; }
Boolean operator & lt; (const Person& B) const {
Return this - & gt; GetName () & lt; B.g etName ();
}
Private:
String m_Name;
Int m_Age;
};
[/code]

CodePudding user response:

To give the Person to define & lt; Operators within the set with & lt; The sorting
  • Related