来自 std::_Rb_tree_const_iterator::operator++ 的分段错误
迭代集合时出现分段错误。堆栈跟踪指向
std::_Rb_tree_const_iterator<Type>::operator++
std::_Rb_tree_increment()
但我没有得到更多信息。迭代器位于函数返回的集合上,
for (FactSet::factset_iterator fact_it = (*binSet_it).getDependencyGraph().getExtentionalFactSet().begin();
fact_it != (*binSet_it).getDependencyGraph().getExtentionalFactSet().end();
++fact_it) {...}
我看不到问题。 提前致谢。
I get a segmentation fault when iterating over a set. The stack trace points to
std::_Rb_tree_const_iterator<Type>::operator++
std::_Rb_tree_increment()
but I get nothing more informative. The iterator is over a set returned by a function
for (FactSet::factset_iterator fact_it = (*binSet_it).getDependencyGraph().getExtentionalFactSet().begin();
fact_it != (*binSet_it).getDependencyGraph().getExtentionalFactSet().end();
++fact_it) {...}
I cannot see the issue.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不想像这样迭代返回值。每次迭代都会重新评估中间终止条件,因此您的
end()
每次都会针对不同的集合,这意味着您的迭代器永远不会到达它。将集合缓存在局部变量中,然后使用其中的
begin()
和end()
。You don't want to be iterating over the return value like that. The middle termination condition is re-evaluated every iteration, so your
end()
will be for a different set every time, which means your iterator will never reach it.Cache the set in a local variable and then use the
begin()
andend()
from that.是否删除循环内的元素?在这种情况下,您需要获取擦除函数的返回值(该函数将迭代器提供给删除后的第一个元素),并且您不得在该轮之后执行 ++it (这将跳过下一个元素,或者如果它已经在末尾,可能会导致运算符++中的段错误)。
Do you erase elements within the loop? In that case you need to take the return value of the erase function (that gives iterator to the first element after the one(s) deleted) and you must not perform ++it after that round (that would skip the next element, or if it was already at the end, possibly cause a segfault in operator++).