标题:模板的显示具体化(指针数组)
只看楼主
dtxwz
Rank: 2
等 级:论坛游民
帖 子:79
专家分:45
注 册:2011-9-18
结帖率:100%
已结贴  问题点数:20 回复次数:3 
模板的显示具体化(指针数组)
#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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:20 
程序代码:
#include <iostream>
#include <cstring>
using namespace std;

template<typename T> T max( const T* a, size_t n );
template<> char* max<char*>( char* const a[], size_t n );
template<> const char* max<const char*>( const char* const a[], size_t n );

int main( void )
{  

    int b[6] = { 1,2,3,4,5,6 };
    double c[4] = {1.1,2.2,3.3,4.4};
    cout << max(b,6) << endl;
    cout << max(c,4) << endl;

    const char* d[5] = {"a","bce","de","fg","a"};
    cout << max(d,5) << endl;
}

template<typename T> T max( const T* a, size_t n )
{
    size_t index = 0;
    for( size_t i=1; i<n; ++i )
        if( a[i] > a[index] )
            index = i;
    return a[index];
}

template<> char* max<char*>( char* const a[], size_t n )
{
    char* p = a[0];
    size_t maxlen = strlen( p );
    for( size_t i=1; i<n; ++i )
    {
        size_t len = strlen( a[i] );
        if( len > maxlen )
        {
            maxlen = len;
            p = a[i];
        }
    }
    return p;
}

template<> const char* max<const char*>( const char* const a[], size_t n )
{
    return max<char*>( const_cast<char**>(a), n );
}
2018-08-10 14:06
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
程序代码:
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

int main( void )
{ 
    int a[6] = { 1,2,3,4,5,6 };
    cout << *max_element(begin(a), end(a)) << endl;

    double b[4] = {1.1,2.2,3.3,4.4};
    cout << *max_element(begin(b), end(b)) << endl;

    const char* c[5] = { "a","bce","de","fg","a" };
    cout << *max_element(begin(c), end(c), [](const char* a,const char* b){return strlen(a)<strlen(b);}) << endl;
}
2018-08-10 14:13
dtxwz
Rank: 2
等 级:论坛游民
帖 子:79
专家分:45
注 册:2011-9-18
得分:0 
我知道了。把char *全换成const char *就可以了
2018-08-10 15:10



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-489133-1-1.html




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

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