这个程序变量未定义,为什么没错误?

yuyishiwo 2011-03-13 12:34:58
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector< int > temp;
int word;
while(cin>>word)
{
temp.push_back(word);
}
/***********判断输入是否为空******************/
if(temp.size() == 0)
{
cout << "NO DATA INPUT!" <<endl;
return -1;
}
/***********首尾数据相加**********************/
for(vector< int >::size_type first=0,last = temp.size()-1;first<last;++first,--last)
{
cout<<temp[first] + temp[last]<<endl;
}
/************考虑奇数个vector< int > 对象************/
if(first == last)
{
cout<<endl
<<"The center element is not been summed"
<<"and its value is:"
<<temp[first]<<endl;
}
return 0;
}
上面的程序中, for(vector< int >::size_type first=0,last = temp.size()-1;first<last;++first,--last)
这句代码中的last的变量怎么定义的呢?
...全文
127 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuyishiwo 2011-03-13
  • 打赏
  • 举报
回复
晕,CSDN不能上传本地图片啊
yuyishiwo 2011-03-13
  • 打赏
  • 举报
回复
我用的是VC++6.0 我运行好几次都没问题,并且编译也没错误,我给你发截图。
intelycc 2011-03-13
  • 打赏
  • 举报
回复
运行没问题?我刚用vs2008跑了一下,都提示我未定义变量了~~~肯定有问题的
yuyishiwo 2011-03-13
  • 打赏
  • 举报
回复
现在是看懂如何定义那变量的了。你说的也对,但是这个程序是C++ Primer给出的答案,奇怪的是程序运行起来还真没问题!!!!!!!!!!!!![Quote=引用 1 楼 intelycc 的回复:]
楼主。。。你这个通的过编译吗。。。。first,last应该是局部变量吧,出了for循环就被释放空间了,你后面
if(first == last)会提示first,last未定义吧
[/Quote]
jinyi821 2011-03-13
  • 打赏
  • 举报
回复
你好! 请加我吧!
飞天御剑流 2011-03-13
  • 打赏
  • 举报
回复
vector< int >::size_type first=0,last = temp.size()-1;

这里定义了两个变量,一个是first,另一个是last,last并初始化为temp.size()-1。
「已注销」 2011-03-13
  • 打赏
  • 举报
回复
vector< int >::size_type first=0,last = temp.size()-1
这句不是定义了first和last两个变量么?????
intelycc 2011-03-13
  • 打赏
  • 举报
回复
楼主。。。你这个通的过编译吗。。。。first,last应该是局部变量吧,出了for循环就被释放空间了,你后面
if(first == last)会提示first,last未定义吧
碎碎念 2011-03-13
  • 打赏
  • 举报
回复
vc6.0---98年的软件了,该退役了...lz换个编译环境吧,,,,
qq120848369 2011-03-13
  • 打赏
  • 举报
回复
#include<iostream>
#include<vector>
using namespace std;

int main()
{
vector< int > temp;
int word;

while(cin>>word)
{
temp.push_back(word);
}

cin.clear();

/***********判断输入是否为空******************/
if(temp.size() == 0)
{
cout << "NO DATA INPUT!" <<endl;
return -1;
}

vector< int >::size_type first=0,last = temp.size()-1;

/***********首尾数据相加**********************/

for(;first<last;++first,--last)
{
cout<<temp[first] + temp[last]<<endl;
}

/************考虑奇数个vector< int > 对象************/
if(first == last)
{
cout<<endl
<<"The center element is not been summed"
<<"and its value is:"
<<temp[first]<<endl;
}

return 0;
}
愤怒的熊猫007 2011-03-13
  • 打赏
  • 举报
回复
vector< int >::size_type first=0,last = temp.size()-1;first<last;
first和last中间是逗号!!所以是一起定义的~~要注意~
zicheng_lin 2011-03-13
  • 打赏
  • 举报
回复
建议楼主还是用 VS2008吧,至少比6.0要标准很多哦!~ VS2010都出来了嘛,还用那么古老的东西....
harleypang 2011-03-13
  • 打赏
  • 举报
回复
看来vc6.0和2008还是有很大的区别的哦
yuyishiwo 2011-03-13
  • 打赏
  • 举报
回复
哦,这样啊,谢谢LS的。那我是不是该换用VS2008呢?呵呵!
zhangsongcui 2011-03-13
  • 打赏
  • 举报
回复
VC6对于for内部定义变量(指for语句中而非循环体内)的作用范围设定不规范,其作用范围一直延续到循环体外。而VS2008默认不会(有编译选项调节)
intelycc 2011-03-13
  • 打赏
  • 举报
回复
楼主,VC6.0我跑过了,的确编译没出错,可是VS2008下还是会报错的
VS2008对标准的支持比较好,我想程序本身那个问题应该是有的。至于为何vc6.0可以通过,就等待高人解答吧

64,637

社区成员

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

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