| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1140 人关注过本帖
标题:模板的显示具体化(指针数组)
取消只看楼主 加入收藏
dtxwz
Rank: 2
等 级:论坛游民
帖 子:79
专家分:45
注 册:2011-9-18
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
模板的显示具体化(指针数组)
#include "stdafx.h"
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<array>
#include<ctime>
#include<fstream>
#include<cstdlib>
#include<cctype>
using namespace std;
template<typename T>
T max(T *a,int n);
template<> char *max<char *>(char *p[], int n);
int main()
{   
    int b[6] = { 1,2,3,4,5,6 };
    double c[4] = {1.1,2.2,3.3,4.4};
    const char *d[5] = {"a","bce","de","fg","a"};
    cout << max(b,6) << endl;
    cout << max(c,4) << endl;
    cout << max(d, 5) << endl;
    return 0;
}
template<typename T>
T max(T *a,int n)
{
    T max = a[0];
    for (int i = 1; i < n; i++)
    {
        if (a[i] > max)
            max = a[i];
    }
    return max;
}
template<>char *max<char *>( char *p[], int n)
{
    int max = strlen(p[0]);
    char *t  = p[0];
    for (int i = 1; i < n; i++)
    {
        if (strlen(p[i]) > max)
        {
            max = strlen(p[i]);
            t = p[i];
        }
    }
    return t;
}
具体化的是返回最长的字符串的地址
标红的类型应该怎么写才能进入具体化。char **编译不能通过 char *又不是对应的类型
搜索更多相关主题的帖子: include template max int char 
2018-08-10 13:27
dtxwz
Rank: 2
等 级:论坛游民
帖 子:79
专家分:45
注 册:2011-9-18
收藏
得分:0 
我知道了。把char *全换成const char *就可以了
2018-08-10 15:10
快速回复:模板的显示具体化(指针数组)
数据加载中...
 
   



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

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