Essential C++ 中的一段代码,编译通不过,帮我看一下

Asus 2001-10-25 09:10:52
下面这段是书中习题2-6的代码,我在VC6 ENTERPRISE edition 中无法通过
BCB5 中也通不过,哪位大虾看一下啦,(用VC编译标准C++好象总出奇怪的问题,
比如for(int i=0;i<10;i++)这种句子在一个函数里面i好象不会释放)
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

template <typename Type>
inline Type max(Type t1,Type t2)
{ return t1 > t2? t1: t2; }

template <typename elemType>
inline elemType max(const vector<elemType> &vec)
{ return *max_element(vec.begin(),vec.end() ); }

template <typename arrayType>
inline arrayeType max(const arrayType *parray,int size)
{ return *max_element(parray,parray+size); }

int main()
{
string sarray[]={"we","were","her","pride","of","ten"};
vector <string> svec(sarray,sarray+6);

int iarray[]={12,70,2,169,1,5,29};
vector <int> ivec(iarray,iarray+7);

float farray[]={2.5,24.8,12.1,3.4,122.23}
vector <float> fvec(farray,farray+5);

int imax=max( max(ivec),max(iarray,7) );
float fmax=max( max(fvec),max(farray,5) );
string smax=max( max(svec),max(sarray,6) );

cout<<"imax"<<imax<<endl;
cout<<"fmax"<<fmax<<endl;
cout<<"smax"<<smax<<endl;

return 0;
}
...全文
76 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
richielee 2001-11-28
  • 打赏
  • 举报
回复
这是VC不符合C++标准的原因
标准中定义
for(int i=0;i<n;i++)
{
}
变量i的作用域应该在 {} 之间

听说可以在程序最前面加上
#define for if 0 else for
这样就没有问题了

不过在有很多if else嵌套的时候,这样作不好
kernel1_0 2001-11-28
  • 打赏
  • 举报
回复
对啊,,vc6的局部变量好象是释放不了。。我也奇怪呢。 。
magicblue 2001-10-25
  • 打赏
  • 举报
回复
i会被释放
weakest 2001-10-25
  • 打赏
  • 举报
回复
这是VC Compiler的BUG?
G++对于这种情况没有问题
Asus 2001-10-25
  • 打赏
  • 举报
回复
thanks ,不好意思
VC里for(int i=0;i<10;i++)这种句子在一个函数里面i好象不会释放,是这样吗
weakest 2001-10-25
  • 打赏
  • 举报
回复
inline arrayeType max(const arrayType *parray,int size)
^敲错了,应该是arrayType
float farray[]={2.5,24.8,12.1,3.4,122.23}
^少分号
Asus 2001-10-25
  • 打赏
  • 举报
回复
please do me a favor

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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