主题:C++求救关于字符窜数组和模板的问题,恳请大牛回答!
代码一和二的区别本来只在于字符窜数组存的方式是char sz[][80]还是char* sz[],却引来这么多问题,麻烦高手指点了[em4]
问题在这::::::::::::::
下面的代码二用不了template<>,即显示具体化,是为什么?
还有函数
char* maxn(char st[][80],int n)
那个八十非设定不可吗?
代码一:
#include<iostream>
#include<cstring>
using namespace std;
template
Any maxn(Any sz[],int n);
template <>char* maxn(char *st[],int n);
int main()
{
int int_sz[6]={12,06,23,44,15,51};
double double_sz[4]={12.0,0.333333,12.858,12.66666666};
char *char_sz[5]={"Hello ","hi! ","Love! ","I ","You "}; //指针形式字符串数组
cout cout cout return 0;
}
template
Any maxn(Any sz[],int n)
{
Any max=0;
for (int i=0;i {
if (sz[i]<max) max=sz[i];
}
return max;
}
template <>char* maxn(char *st[],int n) //指针形式
{
int max=0;
for (int i=0;i {
if (strlen(st[i])<strlen(st[max])) max=i;
}
return st[max];
}
代码二:
#include<iostream>
#include<cstring>
using namespace std;
template
Any maxn(Any sz[],int n);
char* maxn(char st[][80],int n); //用这种方法就不可以用template,而且要限定为80
int main()
{
int int_sz[6]={12,06,23,44,15,51};
double double_sz[4]={12.0,0.333333,12.858,12.66666666};
char char_sz[5][80]={"Hello ","hi! ","Love! ","I ","You "};
cout cout cout return 0;
}
template
Any maxn(Any sz[],int n)
{
Any max=0;
for (int i=0;i {
if (sz[i]<max) max=sz[i];
}
return max;
}
char* maxn(char st[][80],int n)
{
int max=0;
for (int i=0;i {
if (strlen(st[i])<strlen(st[max])) max=i;
}
return st[max];
}
问题在这::::::::::::::
下面的代码二用不了template<>,即显示具体化,是为什么?
还有函数
char* maxn(char st[][80],int n)
那个八十非设定不可吗?
代码一:
#include<iostream>
#include<cstring>
using namespace std;
template
Any maxn(Any sz[],int n);
template <>char* maxn(char *st[],int n);
int main()
{
int int_sz[6]={12,06,23,44,15,51};
double double_sz[4]={12.0,0.333333,12.858,12.66666666};
char *char_sz[5]={"Hello ","hi! ","Love! ","I ","You "}; //指针形式字符串数组
cout cout cout return 0;
}
template
Any maxn(Any sz[],int n)
{
Any max=0;
for (int i=0;i {
if (sz[i]<max) max=sz[i];
}
return max;
}
template <>char* maxn(char *st[],int n) //指针形式
{
int max=0;
for (int i=0;i {
if (strlen(st[i])<strlen(st[max])) max=i;
}
return st[max];
}
代码二:
#include<iostream>
#include<cstring>
using namespace std;
template
Any maxn(Any sz[],int n);
char* maxn(char st[][80],int n); //用这种方法就不可以用template,而且要限定为80
int main()
{
int int_sz[6]={12,06,23,44,15,51};
double double_sz[4]={12.0,0.333333,12.858,12.66666666};
char char_sz[5][80]={"Hello ","hi! ","Love! ","I ","You "};
cout cout cout return 0;
}
template
Any maxn(Any sz[],int n)
{
Any max=0;
for (int i=0;i {
if (sz[i]<max) max=sz[i];
}
return max;
}
char* maxn(char st[][80],int n)
{
int max=0;
for (int i=0;i {
if (strlen(st[i])<strlen(st[max])) max=i;
}
return st[max];
}