search_example.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. Copyright (c) Marshall Clow 2010-2012.
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. For more information, see http://www.boost.org
  6. */
  7. #include <string>
  8. #include <iostream> // for cout, etc.
  9. #include <boost/algorithm/searching/boyer_moore.hpp>
  10. #include <boost/algorithm/searching/boyer_moore_horspool.hpp>
  11. #include <boost/algorithm/searching/knuth_morris_pratt.hpp>
  12. namespace ba = boost::algorithm;
  13. const std::string haystack ( "ABACAB is it everywhere!" );
  14. const std::string needle1 ( "ACAB" );
  15. const std::string needle2 ( "not ABA" );
  16. int main ( int /*argc*/, char * /*argv*/ [] ) {
  17. // In search.hpp, there are generic implementations of three classic sequence search
  18. // algorithms. They all have the same (dual) interface.
  19. // There is a procedural interface, based on std::search:
  20. if ( ba::boyer_moore_search ( haystack.begin (), haystack.end (), needle1.begin (), needle1.end ()) != std::make_pair(haystack.end(), haystack.end()))
  21. std::cout << "Found '" << needle1 << "' in '" << haystack << "' (boyer-moore 1)" << std::endl;
  22. else
  23. std::cout << "Did NOT find '" << needle1 << "' in '" << haystack << "' (boyer-moore 1)" << std::endl;
  24. // If you plan on searching for the same pattern in several different data sets,
  25. // you can create a search object and use that over and over again - amortizing the setup
  26. // costs across several searches
  27. ba::boyer_moore<std::string::const_iterator> search1 ( needle1.begin (), needle1.end ());
  28. if ( search1 ( haystack.begin (), haystack.end ()) != std::make_pair(haystack.end(), haystack.end()))
  29. std::cout << "Found '" << needle1 << "' in '" << haystack << "' (boyer-moore 2)" << std::endl;
  30. else
  31. std::cout << "Did NOT find '" << needle1 << "' in '" << haystack << "' (boyer-moore 2)" << std::endl;
  32. // There is also an implementation of boyer-moore-horspool searching
  33. if ( ba::boyer_moore_horspool_search ( haystack.begin (), haystack.end (), needle1.begin (), needle1.end ()) != std::make_pair(haystack.end(), haystack.end()))
  34. std::cout << "Found '" << needle1 << "' in '" << haystack << "' (boyer-moore-horspool)" << std::endl;
  35. else
  36. std::cout << "Did NOT find '" << needle1 << "' in '" << haystack << "' (boyer-moore-horspool)" << std::endl;
  37. // And also the knuth-pratt-morris search algorithm
  38. if ( ba::knuth_morris_pratt_search ( haystack.begin (), haystack.end (), needle1.begin (), needle1.end ()) != std::make_pair(haystack.end(), haystack.end()))
  39. std::cout << "Found '" << needle1 << "' in '" << haystack << "' (knuth_morris_pratt)" << std::endl;
  40. else
  41. std::cout << "Did NOT find '" << needle1 << "' in '" << haystack << "' (knuth_morris_pratt)" << std::endl;
  42. return 0;
  43. }