【8.6】c++ primer plus 课后编程答案

来源:互联网 发布:mac dock应用程序丢了 编辑:程序博客网 时间:2024/06/14 05:39

C++ PRIMER PLUS 课后答案 
使用IDE为window7系统下的VS2010

#include <iostream>#include <fstream>#include <cstdlib>#include <Windows.h>#include <string>using namespace std;  template <typename T>T maxn(T * a,int n); template <>char *  maxn(char * a[], int n);  int main(){               char * s[5]={"1","12","123","123466","12345"};         int r[6]={1,4,5,8,2,7};         double d[4]={5.2,7.9,6.3,2.1};         cout<<"int:"<<maxn(r,6)<<endl;         cout<<"double:"<<maxn(d,4)<<endl;         cout<<"string:"<<maxn(s,5)<<endl;         system("pause");            return 0;} template <typename T>T maxn(T * a,int n){               int i=0;         T maxx=a[0];         for(;i<n;i++)         {                  if(maxx<a[i])                          maxx=a[i];         }         return maxx;}template <>   char *  maxn(char * a[], int n){         char * maxs=a[0];         for(int i=0;i<n;i++)                  if(strlen(maxs)<strlen(a[i]))                  {                          maxs=a[i];                  }         return maxs;} 


原创粉丝点击