Nine degrees OJ topic 1069: Find student information continuation: custom sort collation

Source: Internet
Author: User
Tags strcmp

In the previous topic used two points to find, and the precondition of binary search is the original data order, it needs to be sorted first.

Consider sorting the data by using the sort function in the STL, and you need to give the collation because it is not the basic data type but the custom struct cannot automatically sort.

There are two ways of doing this:

1. Write CMP functions

BOOL CMP (stu A,stu b) {

Return strcmp (a.stu_no,b.stu_no) <0;

}

Then call sort (buf,buf+n,cmp);

2. The less-than operator that directly defines the struct (sort only needs to be sorted by less than the relationship) to illustrate the collation

struct stu{

...

BOOL operator< (const STU & x) const{

Return strcmp (stu_no,x.stu_no) <0;

}

}BUF[1000];

Nine degrees OJ topic 1069: Find student information continuation: custom sort collation

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.