In STL applications, we often use struct, which requires us to reload operators with specific requirements. For example, in STL, sorting is performed by default using a smaller number. Therefore, when sorting the struct, We need to reload the minor signs.
Example:
# Include <map>
# Include <iostream>
# Include <string>
Using namespace std;
// Student Information
Typedef struct tagStudentInfo
{
Int nID;
String strName;
Bool operator <(const tagStudentInfo & A) const
{
If (nID <A. nID) return true; // compare nID first
If (nID = A. nID) return strName. compare (A. strName) <0; // compare strName
Return false;
}
} StudentInfo, * pstudentInfo;
Int main ()
{
// Map scores with student information
Map <StudentInfo, int> mapStudent;
StudentInfo studentInfo;
StudentInfo. nID = 1;
StudentInfo. strName = "student_one ";
MapStudent. insert (map <StudentInfo, int >:: value_type (studentInfo, 90 ));
StudentInfo. nID = 2;
StudentInfo. strName = "student_two ";
MapStudent. insert (map <StudentInfo, int >:: value_type (studentInfo, 80 ));
StudentInfo. nID = 2;
StudentInfo. strName = "student_three ";
MapStudent. insert (map <StudentInfo, int >:: value_type (studentInfo, 80 ));
Map <StudentInfo, int >:: iterator iter;
For (iter = mapStudent. begin (); iter! = MapStudent. end (); iter ++)
Cout <iter-> first. nID <iter-> first. strName <endl <iter-> second <endl;
Return 0;
}
<