且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我不能删除向量的最后一个元素

更新时间:2023-11-09 23:30:34

if(imageDataVector[i] < threshold)
        imageDataVector.erase(imageDataVector.end()-j);

应该是:

if(imageDataVector[j] < threshold)
        imageDataVector.erase(imageDataVector.begin()+j);

为完整起见,使用擦除删除方式和迭代器方式:

for completeness, the erase-remove way and the iterator way:

imageDataVector.erase(std::remove_if(imageDataVector.begin(), imageDataVector.end(), std::bind2nd(std::less<vector_data_type>(), threshold)), imageDataVector.end());

vector<type>::iterator it = imageDataVector.begin();
while (it != imageDataVector.end()) {
  if (*it < threshold)
    it = imageDataVector.erase(it);
  else
    ++it;
}