vector::erase returns incompatible iterator in debug build
关于std::vector中erase的用法http://www.cplusplus.com/reference/vector/vector/erase/
#include <vector> struct WordCoordinate { int x; int y; int z; }; struct Mypoint { int x; int y; }; int main() { std::vector<WordCoordinate> m_worldCoorPoint; std::vector<Mypoint> m_screenPoint; Mypoint point; for (int i=1;i<=4;i++) { WordCoordinate m_w; m_w.x=1+i; m_w.y=2+i; m_w.z=3+i; m_worldCoorPoint.push_back(m_w); point.x = i+1; point.y = i+2; m_screenPoint.push_back(point); point.x = 10*i+1; point.y = 10*i+2; m_screenPoint.push_back(point); } int thershold =5; // if distance low than thershold delete the point std::vector<WordCoordinate>::iterator itw=m_worldCoorPoint.begin(); std::vector<Mypoint>::iterator it=m_screenPoint.begin(); int idex = 1; while(it!=m_screenPoint.end()) { printf("delete point index %d\n",idex); printf("cur point (%d %d)",(*it).x,(*it).y); if( idex==2) { it=m_screenPoint.erase(it,it+2); itw=m_worldCoorPoint.erase(itw); } idex++; } system("pause"); return 0; }
当程序运行到if( idex==2)后再返回while就会出错,这个是微软官方bug http://connect.microsoft.com/VisualStudio/feedback/details/545013
解决方法安装sp1
在这个版本下会有错:时间有限来不及详细写,有空补充bug原因