10

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

dsolimano
  • 8,870
  • 3
  • 48
  • 63

1 Answers1

10

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.

Community
  • 1
  • 1
1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239