(原)使用vectot的.end()报错:iterators incompatible

转载请注明出处:

http://www.cnblogs.com/darkknightzh/p/5070672.html

参考网址:

http://blog.csdn.net/yxnyxnyxnyxnyxn/article/details/17610899

 

之前用vector一直没有问题,前几天遍历时,

for (auto it = var.sta.begin(); it != var.sta.end();)

运行时直接报:iterators incompatible

网上搜了一下,很多是说使用erase之类的时候的问题,但是自己的程序还没有跑到那里去。。。后来在上面参考网址里面看到那哥们检查到的原因,在程序初始化时,用了ZeroMemory,回想自己是类似的。在程序中有如下简化的代码:

 1 struct a 
 2 {
 3     int a1;
 4     char a2[64];
 5 };
 6 struct example
 7 {
 8     int ina;
 9     vector<a> sta;
10 };
11 
12 void temp()
13 {
14     example var;
15     memset(&var, 0, sizeof(example));
16 
17     // ... some code
18 
19     for (auto it = var.sta.begin(); it != var.sta.end();)
20     {
21         // ... other code
22     }
23 }

崩溃的位置是:

for (auto it = var.sta.begin(); it != var.sta.end();)

这行。主要原因就是

memset(&var, 0, sizeof(example));

这句。去掉这句之后,程序就没有崩溃了。

参考网页中说的很详细,使用memset将_Myproxy清零了。去掉memset后就可以了。

当然,可能该问题还有其他多种原因,这里只是我目前碰到的原因。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值