Demo entry 6364850

c++

   

Submitted by anonymous on May 17, 2017 at 08:48
Language: C++. Code size: 1.0 kB.

#include<iostream>
using namespace std;
string Min1(string *a,int n){
    string min=a[0];
    for(int i=0;i<n;i++){
        if(a[i]<min)min=a[i];
    }
    return min;
}
float Min1(float *a,int n){
    float min=a[0];
    for(int i=0;i<n;i++){
        if(a[i]<min)min=a[i];
    }
    return min;
}
int Min1(int *a,int n){
    int min=a[0];
    for(int i=0;i<n;i++){
        if(a[i]<min)min=a[i];
    }
    return min;

}
//重载实现

template<typename T>
T Min2(T *a,int n){
    T min=a[0];
    for(int i=0;i<n;i++){
        if(a[i]<min)min=a[i];
    }
    return min;
}
//函数模板实现

int main(){
    string s[5]={"ssad","dfggg","axcvg","loossd","zdfddc"};
    float f[5]={15.5,55.59,8.52,4.33,9.66};
    int i[5]={12,58,44,9,25};
    cout<<"重载实现:"<<endl;
    cout<<Min1(s,5)<<endl;
    cout<<Min1(f,5)<<endl;
    cout<<Min1(i,5)<<endl;

    cout<<"函数模板实现:"<<endl;
    cout<<Min2(s,5)<<endl;
    cout<<Min2(f,5)<<endl;
    cout<<Min2(i,5)<<endl;
    return 0;
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).