C ++ set custom sorting rules (nyist 8)

Source: Internet
Author: User

Most C ++ containers are automatically sorted. Therefore, when you use these containers, the element types you add must be relatively large. If not, you need to customize the sorting rules, for example, your custom struct:

# Include <iostream> # include <set> using namespace std; struct ju {int id, x, y; bool operator <(const ju &) const // sort and repeat {if (id =. id) {if (x =. x) return y <. y; else return x <. x;} else return id <. id ;}} tt; set <ju> my; set <ju>: iterator it; int main (int argc, char * argv []) {int t, n, i, j; cin> t; while (t --) {cin> n; my. clear (); for (I = 0; I <n; I ++) {cin> tt. id> tt. x> tt. y; if (tt. x <tt. y) swap (tt. x, tt. y); my. insert (tt );} For (it = my. begin (); it! = My. end (); it ++) cout <(* it ). id <"" <(* it ). x <"" <(* it ). y <endl;} return 0 ;}

 

Related Article

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.