By default, STL uses less than signs for sorting. The above Code does not have any problems in sorting, because the above keyword is int type, which itself supports less than number calculation. In some special cases, for example, if the keyword is a struct, a problem occurs when sorting is involved, because it is not less than number operations, and the insert and other functions cannot be used during compilation. The following two methods are provided to solve this problem.
The first type: less than the serial number overload, the program example
# Include <map> <br/> # include <string> <br/> # include <iostream> <br/> using namespace STD; <br/> typedef struct tagstudentinfo <br/>{< br/> int NID; <br/> string strname; <br/>} studentinfo, * pstudentinfo; // Student Information <br/> int main () <br/>{< br/> // map scores with Student Information <br/> Map <studentinfo, int> mapstudent; <br/> Map <studentinfo, int >:: iterator ITER; <br/> studentinfo. nid = 1; <br/> Stu Dentinfo. strname = "student_one"; <br/> mapstudent. insert (pair <studentinfo, int> (studentinfo, 90); <br/> studentinfo. nid = 2; <br/> studentinfo. strname = "student_two"; <br/> mapstudent. insert (pair <studentinfo, int> (studentinfo, 80); <br/> studentinfo. nid = 3; <br/> studentinfo. strname = "student_three"; <br/> mapstudent. insert (pair <studentinfo, int> (studentinfo, 70); <br/> for (iter = mapstuden T. Begin (); iter! = Mapstudent. end (); ITER ++) <br/>{< br/> cout <ITER-> first. NID <Endl <ITER-> first. strname <Endl <ITER-> second <Endl; <br/>}< br/> return 0; <br/>}< br/> the above program cannot be compiled and passed. It is OK as long as the minor part of the code is reloaded, as shown below: <br/> typedef struct tagstudentinfo <br/>{< br/> int NID; <br/> string strname; <br/> bool operator <(tagstudentinfo const & _) const <br/>{< br/> // This function specifies the sorting policy, which is sorted by NID. If NID is equal, sort by strname <br/> If (NID <_. NID) return true; <br/> If (nid = _. NID) return strname. compare (_. strname) <0; <br/> return false; <br/>}< br/>} studentinfo, * pstudentinfo; // Student Information <br/>
Type 2: the application of the imitation function. At this time, there is no direct minor overload in the struct.
# Include <map> <br/> # include <string> <br/> # include <iostream> <br/> using namespace STD; <br/> typedef struct tagstudentinfo <br/>{< br/> int NID; <br/> string strname; <br/>} studentinfo, * pstudentinfo; // Student Information <br/> class sort <br/> {<br/> Public: <br/> bool operator () (studentinfo const & _, studentinfo const & _ B) const <br/>{< br/> If (_. NID <_ B. NID) return true; <br/> If (_. nid = _ B. NID) Return _. Strname. compare (_ B. strname) <0; <br/> return false; <br/>}< br/>}; <br/> int main () <br/>{< br/> // ing scores with Student Information <br/> Map <studentinfo, Int, sort> mapstudent; <br/> Map <studentinfo, int >:: iterator ITER; <br/> studentinfo. nid = 1; <br/> studentinfo. strname = "student_one"; <br/> mapstudent. insert (pair <studentinfo, int> (studentinfo, 90); <br/> studentinfo. nid = 2; <br /> Studentinfo. strname = "student_two"; <br/> mapstudent. insert (pair <studentinfo, int> (studentinfo, 80); <br/> studentinfo. nid = 3; <br/> studentinfo. strname = "student_three"; <br/> mapstudent. insert (pair <studentinfo, int> (studentinfo, 70); <br/> for (iter = mapstudent. begin (); iter! = Mapstudent. end (); ITER ++) <br/>{< br/> cout <ITER-> first. NID <Endl <ITER-> first. strname <Endl <ITER-> second <Endl; <br/>}< br/> return 0; <br/>}< br/>