向量迭代器不可增量 [英] vector iterator not incrementable
问题描述
* 编辑:我移除了else部分。迭代器已重复两次*
运行时出错:
文件:c:\program files(x86)\microsoft visual studio 10.0 \vc\include\vector
行:99
表达式:向量迭代器不可递增
文件:c:\program files(x86)\microsoft visual studio
10.0 \vc\include\vector行:100
表达式:标准C ++库超出范围&& 0
我的代码必须在目录中找到新图片时发送警报。但是由于我不能自己删除文件,我不得不骗。在发现图像时发送报警之前,我将其与已发送的图像(sentPictures)进行比较。在sentPictures中,我删除了在目录中删除的图像,因为如果我必须比较每一次在过去发现的每一个图像,该程序可能有点重xD
(它在一个无限循环) / p>
这是我得到的错误。要找到存储的元素是否仍然在目录中,我将旧元素vector(sentPictures)与新的元素(refreshVector)进行比较。对于每个旧元素(sentPictures),如果他们没有在refreshVector中找到,我删除它们一个一个。在我的情况下,我得到的错误,即使没有做。只是走过这个循环崩溃。在我的情况下,我有9个元素在目录中,它打印5和崩溃。我检查,有作为nuch元素在sentPictures然后在refreshVector。我想我不能很好地处理迭代器的使用。这是代码片。
for(std :: vector< Picture> :: iterator it = sentPictures.begin ; it!= sentPictures.end(); it ++){
std :: cout< 1<< std :: endl;
if(std :: find(refreshVector.begin(),refreshVector.end(),* it)== refreshVector.end()){
tcout< * it<< std :: endl;
//如果在刷新向量中找不到图片,则删除它
// delete * it;
// it = refreshVector.erase(it);
} else {
std :: cout<< 2<<< std :: endl;
++ it;
}
}
PS:指令删除* ;不编译。 VS说表达式必须有指针或句柄类型,基础*符号。我将搜索是否不是关于事实it用于if条件。
指令delete * it;不会编译。
您正在尝试删除一个对象,delete需要一个指针。你也不应该删除/删除[]任何东西没有分配使用新/新[],这似乎你不是,因为你有一个对象(不是指针)的向量。
如果你想从一个容器中删除sth,你应该使用它相应的函数 vector< T> :: erase()
。 delete
仅适用于在堆上分配的对象。
使用迭代器可以安全地使用索引。
for(int i = 0; i!= sentPictures.size ; i ++)
{
sentPictures [i]; //
// etc
}
一个迭代器。
另一个注意事项,不要后递增迭代器,它会做一个冗余副本,预先递增它,虽然不知道编译器将会优化。
现在擦除问题似乎被裁决,我需要通过这个
代码,直到它结束。它崩溃在lopp的某个地方,但不知道
为什么。
它崩溃,因为你正在擦除循环;在删除元素时会更改数组大小,并且迭代器无效后删除操作。从向量中删除元素效率低下,您可能想使用std :: set。
*EDIT : I removed the else section. The iterator was iterated twice *
Errors when running :
File: c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector Line: 99
Expression: vector iterator not incrementable
File: c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector Line: 100
Expression: "Standard C++ Libraries Out of Range" && 0
My code has to send alarms when it finds a new picture in a directory. But as I cannot delete the files by myself I had to trick. Before sending an alarm when I find an image, I compare it with pictures already sent(sentPictures). And in sentPictures I delete the images erased in the directory, because if I have to compare each time with every image that were found in the past, the program might be a bit heavy xD (it's in an infinite loop)
This is were I get the error. To find if the stored elements are still in the directory, I compare the old elements vector (sentPictures) with the new one (refreshVector). For every old element (sentPictures), if they are not found in the refreshVector, I delete them one by one. In my case I get the error even if nothing is done. Just walking through this loop crashes. In my case I have 9 elements in the directory, it prints 5 and crashes. I checked and there are as nuch elements in sentPictures then in refreshVector. I imagine I do not handle well the use of iterators. Here is the piece of code.
for(std::vector<Picture>::iterator it = sentPictures.begin(); it != sentPictures.end(); it++){
std::cout << "1" << std::endl;
if(std::find(refreshVector.begin(), refreshVector.end(), *it) == refreshVector.end()){
tcout << *it <<std::endl;
//if picture not found in refresh vector, then delete it
//delete *it;
//it = refreshVector.erase(it);
}else{
std::cout<< "2" <<std::endl;
++it;
}
}
PS : the instruction delete * it; doesn't compile. VS says "expression must have pointer or handle type", underlying "*" symbol. I'm gonna search if it isn't about the fact "it" is used in the if condition.
The instruction delete * it; doesn't compile.
You are trying to delete an object, delete takes a pointer. You also should NEVER delete/delete[] anything was not allocated using new/new[], which seems you are not since you have a vector with objects ( not pointers).
If you want to remove sth from a container you should use its appropriate function vector<T>::erase()
. delete
is only for objects allocated on the heap.
Also, in case of a vector you don't have to use an iterator you can safely use an index.
for(int i=0; i != sentPictures.size(); i++)
{
sentPictures[i]; //
//etc
}
Though a find function will still return an iterator.
Another note, don't post-increment an iterator, it will do a redundant copy, pre-increment it instead, though am not sure if the compiler will optimize that.
Now the erase problem seems to be ruled, I need to go through this code until its end. It crashes somewhere in the lopp but don't know why.
It crashes because you're erasing inside the loop; the array size is changed when removing an element, and the iterator will be invalid after the remove operation. Removing elements from a vector is inefficient, you may want to use a std::set.
这篇关于向量迭代器不可增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!