| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1298 人关注过本帖
标题:模版问题 怎么编译不过去了?
只看楼主 加入收藏
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
代码修改如下:

#include <iostream>
#include <cstdlib>

template<class Type>
Type * min(Type * r_array, int size)
{
Type * min_val = NULL;
if(r_array && size>0)
{
min_val = r_array;
for(int i=1;i<size;++i)
{
if(*min_val>r_array[i])
{
min_val = r_array+i;
}
}
}
return min_val;
}


int main()
{
int ia[]={1,5,7,9,0};
double da[]={1.2,2.5,6.1,9.8,10.6};
int size_ia = sizeof(ia)/sizeof(int);
int size_da = sizeof(da)/sizeof(double);
int * i = min(ia, size_ia);

using std::cout;
using std::endl;
if(*i != 0)
{
cout<<\"it is wrong\"<<endl;
}
else
{
cout<<\"it is right\"<<endl;
}
double * d = min(da, size_da);
if(*d!=1.2)
{
cout<<\"it is wrong\"<<endl;
}
else
{
cout<<\"it is right\"<<endl;
}

system(\"pause\");
return 0;
}


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-02-16 16:44
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
递归解法:

#include <iostream>
#include <cstdlib>

template<class Type>
Type * min(Type * r_array, int size)
{
Type * m = r_array;
if(r_array && size>0)
{
if(size == 1)
{
m = r_array;
}
else if(size > 1)
{
Type * rest = min(m+1, size-1);
m = (*m < *rest)?m:rest;
}
}
return m;
}


int main()
{
int ia[]={1,5,7,9,0, -6};
double da[]={1.2,2.5,6.1,9.8,10.6};
int size_ia = sizeof(ia)/sizeof(int);
int size_da = sizeof(da)/sizeof(double);
int * i = min(ia, size_ia);

using std::cout;
using std::endl;
if(*i != -6)
{
cout<<\"it is wrong\"<<endl;
}
else
{
cout<<\"it is right\"<<endl;
}
double * d = min(da, size_da);
if(*d!=1.2)
{
cout<<\"it is wrong\"<<endl;
}
else
{
cout<<\"it is right\"<<endl;
}

system(\"pause\");
return 0;
}


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-02-16 18:03
DarkHero
Rank: 1
等 级:新手上路
威 望:2
帖 子:191
专家分:0
注 册:2006-1-14
收藏
得分:0 
void func() {
// function body
}
我本来意见是和KAI一致的,自从看了Thinking in C++ 之后,我就觉得这样的代码风格很好。
而且在VC 6中编程,用这种风格自己基本不用特别注意代码缩进,编译器自动为你缩进,很方便,而你的那种风格就需要自己不断地空格或\t缩进。
使用这种风格的人已经越来越多了,也很清晰啊,成标准就好了~

for( ; me.alive() ; ) { 淡泊名利,志存高远 } //Forever
2006-02-17 23:15
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
DarkHero,

when you have time, I think it is better to read this.
http://www.possibility.com/Cpp/CppCodingStandard.html

to find the brace-place-policy see this link:
http://www.possibility.com/Cpp/CppCodingStandard.html#brace

of course, you can keep your style as you like.

P.S. I want tell you a very tiny experience, which I think very useful. That is, when you want place the IF clause in your code, or some LOOP clause like as FOR LOOP, even when you write a function for example int main(), you place brace {} pairwise unregarding the code you will write inside the brace {} in the future. This Advantage of this codingstyle is evident. Through using this codingstyle you will never forget to add the End brace '}' somewhere when you write a large program.

Have you understood what I mean. So see a Example:
I always write code with this style, I first write a very rough frame, like this
#include <iostream>
#include <cstdlib>
using namespace std;

int main() // you see this program will at all correct, but fast do nothing.
{ // after I finish typing the begin-brace
// I will type the end-brace, so that I will never forget this.
// then the code is safe.
// and this pair brace will be located in same vertical line,
// so that you will know the context between them.
// You know, you will usually use IDE while writing code, me too.
// But I like just using TextEditor to see my code,
// or see the code from someone else, then my Style will be very practical,
// cause it is relative easy to find the code-block, so that you can understand
// the logic stream.
system("pause");
return 0;
}



自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-02-18 06:25
快速回复:模版问题 怎么编译不过去了?
数据加载中...
 
   



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

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