1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
1 #include<iostream>
2 using namespace std;
3 #include<string>
4 template<class T>
5 struct date{
6 T year;
7 T month;
8 T day;
9 public:
10 date(T y,T m,T d):year(y),month(m),day(d){}
11 friend ostream& operator<<(ostream& o,const date& da)
12 {
13 return o<<da.year<<'-'<<da.month<<'-'<<da.day;
14 }
15 friend bool operator<(date a,date b)
16 {
17 return (a.year<b.year||a.year==b.year&&(a.month<b.month||a.month=b. month&&a.day<b.day));
18 }
19 };
20 template<class Type>
21 Type* max(Type *a,int n)
22 {
23 int ma=0;
24 for(int i=1;i<n;i++)
25 if(a[ma]<a[i])
26 ma=i;
27 return a+ma;
28 }
29
30 template<>
31 date<int>* max(date<int> a[],int n)
32 {
33 int ma=0;
34 for(int i=1;i<n;i++)
35 if(a[ma]<a[i])
36 ma=i;
37 return a+ma;
38 }
39
40 int main()
41 {
42 int a[5]={13,2,22,11,18};
43 int * p=max(a,5);
44 cout<<"Max="<<*p<<' '<<p<<endl;
45 char c[5]={'f','z','v','r','s'};
46 cout<<c[1]<<' '<<&c[2]<<endl;
47 char * q=max(c,5);
48 cout<<"Max="<<*q<<' '<<&q<<endl;
49 double d[5]={13.2,2.1,22.4,11.1,18.2};
50 double * r=max(d,5);
51 cout<<"Max="<<*r<<' '<<r<<endl;
52 date<int> ymd[5]={date<int>(2002,5,7),date<int>(1990,6,7), date<int>(2013,6,8),date<int>(2008,3,1),date<int>(2001,2,3)};
53 date<int> * y=max(ymd,5);
54 cout<<"Max="<<*y<<' '<<y<<endl;
55 }
|