一段C++代码求改……

liuyanghejerry 2010-04-09 09:43:08
最近看模板元编程,就很想比较一下各种形式代码的效率,所以写了三段生成斐波那契数列的代码,分别是用while循环、函数嵌套、模板元。
结果还是出错了- -||

第一个方法是能够得到结果的(虽然int不够放后面的数的……
第二个方法看着命令行狂跳一段时间之后,居然自动关闭了,纳闷,我明明写了getchar()为什么还会自动关闭呢?可能出现了错误,但不知道在哪,求解……
第三个方法编译能够通过,但是输出只有1和0,背离了预期,也不知道是为什么。以前没用过模板,求解……

//方法一
#include <iostream>
using namespace std;

/*
int main()
{
int a=0,b=1,i=0;
while(i<500)
{
cout<<a<<","<<b<<",";
a=a+b;
b=a+b;
i++;
}
getchar();
}
*/



#include <iostream>
using namespace std;
//方法二
void Feb(int a=0,int b=1,int i=500)
{
if(i=0) return;
cout<<a<<","<<b<<",";
a=a+b;
b=a+b;
i--;
Feb(a,b,i);
}
int main()
{
Feb();
getchar();
}





//方法三
#include <iostream>
using namespace std;

template <int i>
class Feblar
{
public:
static void Feb(int a=0,int b=1)
{
cout<<a<<","<<b<<",";
Feblar<i-1>::Feb(a,b);
}
};

template<>
class Feblar<0>
{
public:
static void Feb(int a,int b)
{
}
};

int main()
{
Feblar<500>::Feb();
getchar();
}


...全文
124 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
aoxuehan0424 2010-04-09
  • 打赏
  • 举报
回复
如果你要标准的模板元编程的话:
给你举个例子哈!:

template<unsigned n>
struct Factorial
{
enum { value = n*Factorial<n-1>::value };
};

template<>
struct Factorial<0>
{
enum {value = 1};
};
int main()
{
std::cout << Factorial<5>::value;
}


想仔细琢磨这段代码,参考effective C++ 第三版条款47.
小弟不才,刚看到这儿,还没弄怎么明白。
  • 打赏
  • 举报
回复
还有就是你设置的i的值太大了,当i=500斐波那契数就会变得很大,这个结果会产生溢出
你应该用大数存储的算法实现i=500的斐波那契数
  • 打赏
  • 举报
回复
对于你的方法二我做了一些修改,可以实现你要求的功能。你自己看看
#include<iostream>
using namespace std;
void Feb(int a,int b,int i)
{
if(i==0)
return ;
cout<<a<<","<<b<<",";
a=a+b;
b=a+b;
Feb(a,b,--i);
}
int main()
{
int a0=1,b0=1,i=5;
Feb(a0,b0,i);
return 0;
}

你注意一下我if语句和你自己的if语句,你的if语句是if(i=0),这样会导致死循环,运行时就会出现你说的那种情况。if语句应该是if(i==0)你试试看
liuyanghejerry 2010-04-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 canyingwushang 的回复:]
Unless you are a hard-core template junkie,you need not worry about the TMP.The rules for TMP rarely interact with the mainstream C++ programming
[/Quote]

1.We still need to know how to use it,though.
2.How about the 2nd method? The window disappeared while ended.But I've added "getchar()".What's the problem?
xxphddz 2010-04-09
  • 打赏
  • 举报
回复
up LZ
canyingwushang 2010-04-09
  • 打赏
  • 举报
回复
Unless you are a hard-core template junkie,you need not worry about the TMP.The rules for TMP rarely interact with the mainstream C++ programming
liuyanghejerry 2010-04-09
  • 打赏
  • 举报
回复
汗死了,带上code标签居然直接不显示OTZ

//方法一
#include <iostream>
using namespace std;

/*
int main()
{
int a=0,b=1,i=0;
while(i<500)
{
cout<<a<<","<<b<<",";
a=a+b;
b=a+b;
i++;
}
getchar();
}
*/

#include <iostream>
using namespace std;
//方法二
void Feb(int a=0,int b=1,int i=500)
{
if(i=0) return;
cout<<a<<","<<b<<",";
a=a+b;
b=a+b;
i--;
Feb(a,b,i);
}
int main()
{
Feb();
getchar();
}



//方法三
#include <iostream>
using namespace std;

template <int i>
class Feblar
{
public:
static void Feb(int a=0,int b=1)
{
cout<<a<<","<<b<<",";
Feblar<i-1>::Feb(a,b);
}
};

template<>
class Feblar<0>
{
public:
static void Feb(int a,int b)
{
}
};

int main()
{
Feblar<500>::Feb();
getchar();
}

hrjhrj12345 2010-04-09
  • 打赏
  • 举报
回复
没代码,怎么解?

64,281

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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