【发布时间】:2014-01-17 20:35:02
【问题描述】:
嗯,标题说明了一切。
我有这段代码:
std::list<vector<Plane>> list;
std::list<vector<Plane>>::iterator possible_planes_it;
...
for(possible_planes_it = list.begin(); possible_planes_it !=
list.end(); possible_planes_it++)
{
if(static_cast<float>(good_matches.size()) >= static_cast<float>((matches.size())*0.8))
{
if(possible_planes_it->back().getTimestamp() < count) // Means that there has not been a match found this round
{
possible_planes_it->push_back(Plane(area, *center_it, keypoint, descriptor, count));
possible_planes_it->back().setNumberOfGoodMatches(good_matches.size());
}
else
{
if(possible_planes_it->back().getNumberOfGoodMatches() < good_matches.size())
{
possible_planes_it->pop_back(); // If a better match has been found, remove the last vector entry and push the new one
possible_planes_it->push_back(Plane(area, *center_it, keypoint, descriptor, count));
possible_planes_it->back().setNumberOfGoodMatches(good_matches.size());
}
else
{
list.push_back(vector<Plane>());
list.back().push_back(Plane(area, *center_it, keypoint, descriptor, count));
}
}
}
else
{
list.push_back(vector<Plane>());
list.back().push_back(Plane(area, *center_it, keypoint, descriptor, count));
}
}
在这个 for 循环中的某个地方,我的迭代器变得无效,因此它陷入了无限循环。但是如何保持迭代器有效?
【问题讨论】:
-
标题并没有说明一切。我们需要知道它是什么类型的容器。
-
抱歉,更新了代码。它是一个向量列表。
-
哪个迭代器失效了?看起来像列表中的迭代器?
-
怎么知道迭代器无效?
-
你真的必须发布所有代码来表达你的观点吗?