撰寫樣板函數

laiwing 2009-07-11 11:57:53
撰寫樣版函數 max5(), 其引數是含5個型態T元素的陣列,令一個引數表示陣列元素個數的整數.此函數回傳陣列中最大的元素.
在程式中用6個int值的陣列和4個double 值的陣列分別測試函數.

這個程序也應該包含一個特定化,其引數是char指標的陣列,指標的個數為第二個引數,回傳最長的自串地址.

-----華麗的分割線-----

上部分我寫好了

#include <iostream>
#include <cstdlib>
using namespace std;

template <class NUM>
NUM* max5(NUM T[],int n);

int main()
{
int T1[6] = { 1,2,6,4,5,1};
double T2[4] = { 1.1 , 23.8 ,4 ,5};

cout << *max5(T1,6) << " " << *max5(T2,4) << " " ;

system("pause");

return 0;
}

template <class NUM>
NUM* max5 (NUM T[],int n)
{
NUM temp = T[0];
for (int i = 1;i<n;i++)
if ( temp < T[i] )
temp = T[i];

int j = 0;
for (j;j<n;j++)
if ( T[j] == temp )
break;


return &(T[j]);
}


下一步我不知道如何寫
...全文
25 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
baihacker 2009-07-12
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 Fleeboy 的回复:]
引用 7 楼 baihacker 的回复:
C/C++ code
#include <iostream>
#include <cstdlib>
using namespace std;

template <class NUM>
NUM* max5(NUM T[],int n);
template <class NUM>
NUM* max5 (NUM T[],int n)
{
NUM temp = T[0];
for (int i = 1;i <n;i++)
if ( temp < T[i] )
temp = T[i];

int j = 0;
for (j;j <n;j++)
if ( T[j] == temp …
[/Quote]
你少打了一个s
c++ templates
Fleeboy 2009-07-12
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 baihacker 的回复:]
C/C++ code
#include <iostream>
#include <cstdlib>
using namespace std;

template <class NUM>
NUM* max5(NUM T[],int n);
template <class NUM>
NUM* max5 (NUM T[],int n)
{
NUM temp = T[0];
for (int i = 1;i<n;i++)
if ( temp < T[i] )
temp = T[i];

int j = 0;
for (j;j<n;j++)
if ( T[j] == temp )
break;


[/Quote]
顶飞雪大哥的

顺便建议楼主看一本书《C++ Template》
baihacker 2009-07-12
  • 打赏
  • 举报
回复

#include <iostream>
#include <cstdlib>
using namespace std;

template <class NUM>
NUM* max5(NUM T[],int n);
template <class NUM>
NUM* max5 (NUM T[],int n)
{
NUM temp = T[0];
for (int i = 1;i<n;i++)
if ( temp < T[i] )
temp = T[i];

int j = 0;
for (j;j<n;j++)
if ( T[j] == temp )
break;


return &(T[j]);
}
typedef const char* pcChar;
template <>
pcChar* max5 (pcChar T[],int n)
{
int len = strlen(T[0]);
int pos = 0;
for (int i = 1;i<n;i++)
if ( len < strlen(T[i]) )
len = strlen(T[i]), pos = i;
return &(T[pos]);
}
int main()
{
int T1[6] = { 1,2,6,4,5,1};
double T2[4] = { 1.1 , 23.8 ,4 ,5};
const char* T3[] = {"1","12","123","23", "3"};
cout << *max5(T1,6) << " " << *max5(T2,4) << " " ;
cout << *max5(T3, 5) << endl;
system("pause");

return 0;
}
机智的呆呆 2009-07-11
  • 打赏
  • 举报
回复

template <>
char** max5 (char* T[],int n)//這樣特化
{
return 0;
}
大前置 2009-07-11
  • 打赏
  • 举报
回复
繁体的
laiwing 2009-07-11
  • 打赏
  • 举报
回复

template <class NUM>
NUM* max5(NUM T[],int n);

template <> char* max5<char>(char T[],int n);


之間寫了這樣的一個樣板
發現不行...

他分不清
int T1[6] = { 1,2,6,4,5,1};

char* T3[5] = { "A" , "AAA" , "AAAA" , "AAAAA" ,"A" };
的差別
laiwing 2009-07-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 luwenzuo 的回复:]
繁体的
[/Quote]

下次寫英語... >.<
laiwing 2009-07-11
  • 打赏
  • 举报
回复
頂起來阿~
Walf_ghoul 2009-07-11
  • 打赏
  • 举报
回复
ls....

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧