(原)使用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后就可以了。

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

posted on 2015-12-23 17:37  darkknightzh  阅读(734)  评论(0编辑  收藏  举报

导航