问高手们,一个简单程序的问题

mengxiang_y 2004-08-08 08:38:33

#include <vector>
#include <iostream>
#include <string>
using namespace std;

vector<string> iter;
vector<string>::iterator ite = iter.begin();

void download(int);

int main()
{
iter.push_back("111111");
iter.push_back("222222");
vector<string>::iterator ite_end = iter.end();
int i=0;
while(ite!=ite_end)
{
download(i);
ite_end = iter.end();
ite++;
i++;
}
iter.clear();
return 1;
}

void download(int i)
{
if (i%2==0)
iter.push_back("11111111");

}

为什么一运行就死机呢?能分析下原因吗?
如何实现动态的增加容器内容,并且动态的访问呢?
...全文
179 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
ekschencn 2004-08-08
  • 打赏
  • 举报
回复
就是这样。
不过你得提防下一次的增容,如果push_back进很多数据的话
ntxs 2004-08-08
  • 打赏
  • 举报
回复
恩,经楼上这么一提点,明白了

iter.begin()改变了,但因为ite的声明太靠前,所以没来得及跟着改变。

只要ite的声明放在任何一个 push_back后 都可以


ekschencn 2004-08-08
  • 打赏
  • 举报
回复
还是增容问题
确保ite与iter.begin()同步
ntxs 2004-08-08
  • 打赏
  • 举报
回复
while(ite!=ite_end)
{
download(i);
ite_end = iter.end();
ite=iter.begin();
i++;
ite+=i;
}

这个干嘛的,我知道你的意思,但觉得,又置begin又+i , 意思跟原来的 ite++是一样啊
显得挺罗嗦的 ^^
ekschencn 2004-08-08
  • 打赏
  • 举报
回复
还是增容问题
增容后iter.begin()就会变了
ntxs 2004-08-08
  • 打赏
  • 举报
回复
放在俺放的那地方就行了 ^^
ntxs 2004-08-08
  • 打赏
  • 举报
回复
同意楼上

就只有一个问题 vector<string>::iterator ite = iter.begin();放进main()里面

其他的 一点问题没有

靠 这个小问题折磨我跟楼上那么久 。。。

但为什么不能放前面, 谁给说说?
ekschencn 2004-08-08
  • 打赏
  • 举报
回复
还有vector<string>::iterator ite = iter.begin();不是放进去了就行,位置一定要对,放在main刚开始处就不行的了
ekschencn 2004-08-08
  • 打赏
  • 举报
回复
1 不用,楼主的已经写了前置声明
2 不放也可以,只要把while写成:
while(ite!=ite_end)
{
download(i);
ite_end = iter.end();
ite=iter.begin();
i++;
ite+=i;
}
ntxs 2004-08-08
  • 打赏
  • 举报
回复
很奇怪很奇怪

关于楼主原来的代码 只要修改两个地方 就可以运行

1 将void download(int i)
{
if (i%2==0)
iter.push_back("11111111");

}
写在main的前面

2 将vector<string>::iterator ite = iter.begin();放进main 里

象这样 就OK:
#include <vector>
#include <iostream>
#include <string>
using namespace std;

vector<string> iter;


void download(int i)
{
if (i%2==0)
iter.push_back("11111111");

}
int main()
{
iter.push_back("111111");
iter.push_back("222222");
vector<string>::iterator ite = iter.begin();
vector<string>::iterator ite_end = iter.end();
int i=0;
while(ite!=ite_end)
{
download(i);
ite_end = iter.end();
ite++;
i++;
}
ite = iter.begin();
for (; ite != iter.end(); ite++)//这个是我加来做测试的
cout<< *ite<<"\t";

iter.clear();
return 0;
}
ntxs 2004-08-08
  • 打赏
  • 举报
回复
又试了一下 1 的确不是问题

但 2 绝对是问题!!!!!!!!!!!!!!!!

ntxs 2004-08-08
  • 打赏
  • 举报
回复
楼上的 不是吧。。。
难道问题出在编译器上?
如果真的象你说的 1 2 都没问题

那楼主的程序本身就没问题啊
ekschencn 2004-08-08
  • 打赏
  • 举报
回复
1 iter是可见的,它是全局对象

2 我这里倒没有问题,体内体外都可以
ntxs 2004-08-08
  • 打赏
  • 举报
回复
终于发现问题了

1 关于
void download(int i)
{
if (i%2==0)
iter.push_back("11111111");

}
这里面的iter不可见的问题 但,这不是造成死循环的主要原因

主要原因,居然是……
2 vector<string>::iterator ite = iter.begin();
这句本身并没有错,但把它放到main函数的外面,就是大错特错了。
难怪当时我把程序的断点设在main函数入口的时候就死循环了。
那为什么 这句放在外面就会造成死循环呢?

请真正的高手出来解决问题!!!!!!!!!!!!!!!!
我也很想知道@_@
ekschencn 2004-08-08
  • 打赏
  • 举报
回复
搞了一下可以了
ntxs 2004-08-08
  • 打赏
  • 举报
回复
不会吧? 我用BCB5 运行了n次了 完全没问题啊
ekschencn 2004-08-08
  • 打赏
  • 举报
回复
楼上的你的程序我运行了还是不行,我用的是vc.net
ntxs 2004-08-08
  • 打赏
  • 举报
回复
这里有个关于 iter.end()的问题
当vector动态增长的时候 这个 iter.end()也跟着自动到达 最后一个项的后面一项

所以你完全没必要写
vector<string>::iterator ite_end = iter.end();这句

如果你一定要写 ,那么修改后的,可编译的程序是这样的:
#include <vector>
#include <iostream>
#include <string>
using namespace std;
vector<string> iter;

int main(int argc, char* argv[])
{
iter.push_back("111111");
iter.push_back("222222");
int i = 0;
vector<string>::iterator ite = iter.begin();
vector<string>::iterator ite_end = iter.end();
while ( ite != ite_end)
{
if ( i % 2 == 0)
iter.push_back("11111111");
ite_end = iter.end();
i++;
ite++;

}
ite = iter.begin();
for (; ite != iter.end(); ite++)//这个是我加来做测试的
cout<< *ite<<"\t";
return 0;
}

其实更好的代码应该象这样,省略掉那个ite_end
那么是这样:
#include <vector>
#include <iostream>
#include <string>
using namespace std;
vector<string> iter;

int main(int argc, char* argv[])
{
iter.push_back("111111");
iter.push_back("222222");
int i = 0;
vector<string>::iterator ite = iter.begin();

while ( ite != iter.end())
{
if ( i % 2 == 0)
iter.push_back("11111111");

i++;
ite++;

}
ite = iter.begin();
for (; ite != iter.end(); ite++)
cout<< *ite<<"\t";
return 0;
}
foolish4 2004-08-08
  • 打赏
  • 举报
回复
gz
ekschencn 2004-08-08
  • 打赏
  • 举报
回复
楼主我分析了一下:
程序出现这种情况的原因在于动态增容。vector应该是用数组实现的,因此在动态增容时会改变了第一个元素的地址。
定义了iter以后,它的容量是0,第一次push_back()时,容量不足,进行动态增容,而使得iter.begin()的值改变的。但是ite却没有得到更新,因此push_back()以后,ite实际已经是没有意义的了。
解决办法有两个:
1、及时更新ite的值:
while(ite!=iter.end())
{
download(i);
ite=iter.begin();
i++;
ite+=i;
}
2、分配iter足够的容量:

int main()
{
iter.reserve(10);
......
}
加载更多回复(6)

33,311

社区成员

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

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