C ++ STL向量迭代器不兼容

编程入门 行业动态 更新时间:2024-10-23 03:30:52
本文介绍了C ++ STL向量迭代器不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 // Erase the missing items vector<AlignedFDRData>::size_type StandardNum = FDRFreq.at(0).fData.size(); vector<AlignedFDRData>::iterator iter = FDRFreq.begin(); while (iter != FDRFreq.end()){ if( iter->fData.size() < StandardNum){ FDRFreq.erase(iter); } else{ ++iter; } }

此部分用于擦除FDRFreq矢量项,其中数据长度小于标准数,但调试断言失败:向量迭代器不兼容。

This part is used to erase the FDRFreq vector item, in which the data length is smaller than the standard number, but the debug assertion failed: vector iterators incompatible. I am a green hand in C++ STL, thanks for your kindly help.

推荐答案

您的代码需要成为

while (iter != FDRFreq.end()){ if( iter->fData.size() < StandardNum){ iter = FDRFreq.erase(iter); } else{ ++iter; } }

向量迭代器不兼容重新使用已被无效 - 也就是说,不能保证它指向的元素仍然存在于该存储器位置。向量元素的擦除使该位置之后的迭代器无效。 .erase 返回一个新的,有效的迭代器,您可以改用。

"vector iterators incompatible" means that the iterator you're using has been invalidated - that is to say, there is no guarantee that the elements it points to still exist at that memory location. An erase of a vector element invalidates the iterators following that location. .erase returns a new, valid iterator you can use instead.

如果你刚接触STL,强烈建议您阅读Scott Myer的有效STL (以及 Effective C ++ )

If you're new to STL, I highly recommend you read Scott Myer's Effective STL (and Effective C++, while you're at it)

更多推荐

C ++ STL向量迭代器不兼容

本文发布于:2023-07-30 00:30:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1244970.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:向量   不兼容   迭代   STL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!