首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

有关容器的迭代器的有关问题

2012-08-29 
有关容器的迭代器的问题for(vectorBook*::iterator itborrowedbook.begin()it!borrowedbook.end()++

有关容器的迭代器的问题
for(vector<Book*>::iterator it=borrowedbook.begin();it!=borrowedbook.end();++it)
{
......
}
我的问题是,如果在循环中有erase的操作,例如borrowedbook.erase(it,it+1);那么,it会不会自动指向下一个元素???

[解决办法]

探讨
for(vector<Book*>::iterator it=borrowedbook.begin();it!=borrowedbook.end();++it)
{
......
}
我的问题是,如果在循环中有erase的操作,例如borrowedbook.erase(it,it+1);那么,it会不会自动指向下一个元素???

[解决办法]
erase的返回值:
A random access iterator pointing to the new location of the element that followed the last element erased by the function call, which is the vector end if the operation erased the last element in the sequence.

参考:
http://www.cplusplus.com/reference/stl/vector/erase/
[解决办法]
楼主的写法要修改下,需要用返回值才行。
C/C++ code
for(vector<Book*>::iterator it=borrowedbook.begin();it!=borrowedbook.end();){   if(需要删除)      it = borrowedbook.erase(it,it+1);   else      ++it;} 

热点排行