使用自定义访问者时,如何停止使用 Boost Graph Library 进行广度优先搜索?

发布于 2024-08-05 20:24:02 字数 31 浏览 4 评论 0原文

假设我找到了符合我的条件的节点,我需要停止搜索。

Say I found the node that meets my criteria and I need to stop the search.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

愿与i 2024-08-12 20:24:02

解决方案是抛出已知类型的异常 - 然后在调用方捕获它。来自常见问题解答

如何提前退出 BFS 等算法?

创建一个访问者,当您想要中断搜索时抛出异常,然后将对 breadth_first_search 的调用放在适当的 try/catch 块内。这让许多程序员认为这是对异常的滥用,但是,经过深思熟虑,才决定让异常具有提前退出的首选方式。有关更多详细信息,请参阅增强电子邮件讨论。

The solution is to throw an exception of your known type - then catch it on the calling side. From the FAQ:

How do I perform an early exit from an algorithm such as BFS?

Create a visitor that throws an exception when you want to cut off the search, then put your call to breadth_first_search inside of an appropriate try/catch block. This strikes many programmers as a misuse of exceptions, however, much thought was put into the decision to have exceptions has the preferred way to exit early. See boost email discussions for more details.

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