| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1910 人关注过本帖
标题:编写重载函数min()
只看楼主 加入收藏
泰艾我勒
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2019-11-17
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
编写重载函数min()
编写重载函数min(),分别计算int、double、float、long类型数组中的最小值。
程序如下,请完善该程序的设计:
#include <iostream>
using namespace std;
int min(int [],int);
double min(double[],int);
float min(float[],int);
long min(long[],int);
int main(){
         int a[6]={2,22,0,-6,67,-111};
         int aa[4]={5,19,2,28};
         double b[8]={2.2,62,-6.1,500,68.2,-500.345,-8,1000};
         float c[4]={3.2,-8.61,699,33};
         long d[3]={3265891,14789,-63256};
         cout<<"the least number in a[6] is "<<min(a,6)<<endl;
         cout<<"the least number in b[8] is "<<min(b,8)<<endl;
         cout<<"the least number in c[4] is "<<min(c,4)<<endl;
         cout<<"the least number in d[3] is "<<min(d,3)<<endl;
         cout<<"the least number in aa[4] is "<<min(aa,4)<<endl;
 return 0;

}
//你的代码将被嵌在这里
我需要其中一个函数就可以了:int min(int [],int);
搜索更多相关主题的帖子: cout int the number min 
2019-11-28 16:31
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
别人写的未必是你需要的,比如
程序代码:
#include <algorithm>

int    min( int    a[], int n ) { return *std::min_element(a,a+n); }
double min( double a[], int n ) { return *std::min_element(a,a+n); }
float  min( float  a[], int n ) { return *std::min_element(a,a+n); }
long   min( long   a[], int n ) { return *std::min_element(a,a+n); }


还是给你一个 std::min_element 的实现作参考吧
程序代码:
template<class ForwardIt> ForwardIt
std::min_element( ForwardIt first, ForwardIt last )
{
    if( first == last )
        return last;

    ForwardIt smallest = first;
    for( ++first; first!=last; ++first )
        if( *first < *smallest )
            smallest = first;
    return smallest;
}

2019-11-28 16:41
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:20 
程序代码:
#include <iostream>
using namespace std;

template<typename T, size_t N>
T min( const T (&arr)[N] )
{
    T ret = arr[0];
    for( size_t i=1; i<N; ++i )
        if( arr[i] < ret )
            ret = arr[i];
    return ret;
}

int main( void )
{
    int a[] = { 2, 22, 0, -6,67, -111 };
    double b[] = { 2.2, 62, -6.1, 500, 68.2, -500.345, -8, 1000 };
    float c[] = { 3.2f, -8.61f, 699.f, 33.f };
    long d[] = { 3265891, 14789, -63256 };

    cout << "the least number in a[6] is " << min(a) << endl;
    cout << "the least number in b[8] is " << min(b) << endl;
    cout << "the least number in c[4] is " << min(c) << endl;
    cout << "the least number in d[3] is " << min(d) << endl;
}
2019-11-28 16:47
快速回复:编写重载函数min()
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016104 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved