循环遍历std :: vector并删除元素会导致超出范围异常

时间:2017-09-19 13:21:33

标签: c++ c++11 vector

我有一个vectorArr数组(vectorArr)对象。有2个线程,1个推送对象std::vector<int>数组和其他工作。当我结束使用它们时,必须从中删除一些对象。所以我只是将这些对象索引号添加到其他DeleteListvectorArr),然后迭代它并从保存的索引中删除std::vector<int> DeleteList; int index = 0; for (auto &data : vectorArr) { if (data.frames < 150) { data.frames++; //some code here } else DeleteList.push_back(index); index++; } if (DeleteList.empty()) return; for (auto &dIndex : DeleteList) { if (dIndex < vectorArr.size()) vectorArr.erase(vectorArr.cbegin() + dIndex); // out of range exception } DeleteList.clear(); 中的对象,但有时我得到了超出范围的例外。我不明白为什么会这样。

$this->team->playerAssignment->player

0 个答案:

没有答案