来自 std::_Rb_tree_const_iterator::operator++ 的分段错误

发布于 2024-08-23 11:52:30 字数 445 浏览 4 评论 0原文

迭代集合时出现分段错误。堆栈跟踪指向

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苍景流年 2024-08-30 11:52:30

您不想像这样迭代返回值。每次迭代都会重新评估中间终止条件,因此您的 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() and end() from that.

双马尾 2024-08-30 11:52:30

是否删除循环内的元素?在这种情况下,您需要获取擦除函数的返回值(该函数将迭代器提供给删除后的第一个元素),并且您不得在该轮之后执行 ++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++).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文