predicate_example.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Boost string_algo library example file ---------------------------------//
  2. // Copyright Pavol Droba 2002-2003. Use, modification and
  3. // distribution is subject to the Boost Software License, Version
  4. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org for updates, documentation, and revision history.
  7. #include <string>
  8. #include <iostream>
  9. #include <functional>
  10. #include <boost/algorithm/string/predicate.hpp>
  11. #include <boost/algorithm/string/classification.hpp>
  12. #include <boost/bind.hpp>
  13. using namespace std;
  14. using namespace boost;
  15. int main()
  16. {
  17. cout << "* Predicate Example *" << endl << endl;
  18. string str1("123xxx321");
  19. string str2("abc");
  20. // Check if str1 starts with '123'
  21. cout << "str1 starts with \"123\": " <<
  22. (starts_with( str1, string("123") )?"true":"false") << endl;
  23. // Check if str1 ends with '123'
  24. cout << "str1 ends with \"123\": " <<
  25. (ends_with( str1, string("123") )?"true":"false") << endl;
  26. // Check if str1 contains 'xxx'
  27. cout << "str1 contains \"xxx\": " <<
  28. (contains( str1, string("xxx") )?"true":"false") << endl;
  29. // Check if str2 equals to 'abc'
  30. cout << "str2 equals \"abc\": " <<
  31. (equals( str2, string("abc") )?"true":"false") << endl;
  32. // Classification functors and all predicate
  33. if ( all(";.,", is_punct() ) )
  34. {
  35. cout << "\";.,\" are all punctuation characters" << endl;
  36. }
  37. // Classification predicates can be combined
  38. if ( all("abcxxx", is_any_of("xabc") && !is_space() ) )
  39. {
  40. cout << "true" << endl;
  41. }
  42. cout << endl;
  43. return 0;
  44. }