Multi-keyword ordering (with overloads for operators (<< operators and >> operators)

Source: Internet
Author: User

A sort time limit: ms | Memory limit:65535 KB Difficulty:3
Describe
Now there are a lot of rectangles, each rectangle has a number, this number can be repeated, also know that the width and length of the rectangle, the number, length, width are all integers; now it's required to sort by a bit (the default collation is small to large);
1. Sort by number from small to large
2. For the numbers equal to the rectangle, according to the oblong long sort;
3. If the numbers and lengths are the same, sort by the width of the rectangle;
4. If the number, length, and width are the same, only one rectangle is reserved for sorting, the extra rectangles are removed, and the final sequence displays all rectangles in the specified format;
Input
The
first line has an integer 0<n<10000, which indicates that there is an n set of test data, and the first row of each group has an integer 0<m<1000, which indicates a M-rectangle, and the next M-line has three numbers, and the first number represents the number of the rectangle.
The second and third numeric large representations are long, the values are small, the representation is wide, and the equality indicates that this is a square (data contract length and width are less than 10000);
Output
sequential output The number length of all eligible rectangles for each set of data
Sample input
181 1 11 1 11 1 21 2 11 2 22 1 12 1 22 2 1
Sample output
1 1 11 2 11 2 22 1 12 2 1
My answer:
#include <iostream>#include<algorithm>#include<cmath>using namespacestd;Const intMAX =100000; typedefstructcil{ints; intl; intW; BOOLFlag;} Cil;cil A[max];intCompare (CIL A, CIL b) {if(A.S! = B.S)returnA.S <B.S; if(A.L! = B.L)returnA.L <B.L; if(A.W! = B.W)returnA.W <B.W;}intMain () {intN; CIN>>N;  while(n--)    {        intm; CIN>>m;  for(inti =0; I < m; i + +) {cin>>a[i].s>>a[i].l>>A[I].W; if(A[i].l <a[i].w) Swap (A[I].L,A[I].W); A[i].flag=1; } sort (A,a+m,compare);  for(inti =0; I < m; i + +)        {            if(I! =0&& A[i].s = = a[i-1].S&AMP;&AMP;A[I].L = = a[i-1].L &AMP;&AMP;A[I].W = = a[i-1].W) a[i].flag=0; }           for(inti =0; I < m; i++)        {            if(a[i].flag) cout<<a[i].s<<" "<<a[i].l<<" "<<a[i].w<<Endl; }    }    return 0;}

Refer to the Master code:
#include <iostream>#include<Set>#include<iterator>using namespacestd;structrect{intNum,length,width; };BOOL operator< (Constrect& R1,Constrect&R2) {    returnR1.num<r2.num | | R1.num==r2.num && R1.length<r2.length | | R1.num==r2.num&&r1.length==r2.length &&r1.width<R2.width;} IStream&operator>> (istream&inch,rect&R) {    inch>>R.num; intb; CIN>>a>>b; R.length=Max (A, b); R.width=min (A, b); return inch;} Ostream&operator<< (ostream& out,Constrect&R) {    return  out<<r.num<<" "<<r.length<<" "<<r.width;}intMain () {intnum; CIN>>num;  while(num--)    {        Set<Rect>rs;        Rect R; intN; CIN>>N;  while(n--) {cin>>R;        Rs.insert (R); } copy (Rs.begin (), Rs.end (), Ostream_iterator<Rect> (cout,"\ n")); }}  

Learn about overloading of operators:
1. Overloads of the output operator 〈〈:
In order to be consistent with the IO standard library, the operator should accept ostream& as the first parameter, a reference to the class type Const object as the second parameter, and return a reference to the Ostream parameter.
ostream& operator << (ostream& os, const rect& r) {OS <<//....return os;}
Example:
Overloading an Sales-iterm class with an output operator
<pre name= "code" class= "CPP" >ostream& operator << (ostream& out, sales_iterm& s) {out<< s.isbn<< "\ t" <<s.units_sold<< "\ T" <<s.revenue<< "\ T" <<s.avg_price ();   "\ t" horizontal tab, jump to the next space position; return out;}
 
 
The first parameter is a reference to ostream that produces output on that object. The ostream is non-const because writing to the stream alters the state of the stream. The parameter is a reference because the Osteram object cannot be copied.
The second parameter should generally be a reference to the class type to be output. The parameter is a reference to avoid copying arguments, which can be const, because (in general) outputting an object should not alter the object. Make the formal parameter a const reference, you can use the same definition to output both const and non-const objects.
The return type is a ostream reference, and its value is usually the output operator Ostream object.
It should be noted that in general, the output operator should output the contents of the object for minimal formatting, and they should not output newline characters (newline characters have the effect of emptying the output buffer.)
Did not read  ...
 
 
 
 
2. Overloading of input operators >>
     Like the output operator, the first parameter of the input operator is a reference, a pointer to the stream it is reading, and a reference to the same stream is returned. The second parameter of the object is the non-
Const reference, the reference must be non-const, because the purpose of the input operator is to read the data into the object.
     More important but often enough, the input and output operators have the following differences: The input operator must handle the error and the likelihood of the end of the file.
 istream& operator  >> (istream& in , Sales_item& s) { double   price;  in  >>s.isbn>>s.units_sold>>    price;  if  (in         ) s.revenue  = s.units_sold * price;         else   s  = Sales_item (); //    input Failed:reset object to the default state  return  in  ;}  

Overloading of arithmetic operators and relational operators when you encounter a second write ...

Multi-keyword ordering (with overloads for operators (<< operators and >> operators)

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.