STL map sorting (heavy load less than sign and function simulation)

Source: Internet
Author: User

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/>

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.