find_example.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 <algorithm>
  10. #include <functional>
  11. #include <boost/algorithm/string/case_conv.hpp>
  12. #include <boost/algorithm/string/find.hpp>
  13. using namespace std;
  14. using namespace boost;
  15. int main()
  16. {
  17. cout << "* Find Example *" << endl << endl;
  18. string str1("abc___cde___efg");
  19. string str2("abc");
  20. // find "cde" substring
  21. iterator_range<string::iterator> range=find_first( str1, string("cde") );
  22. // convert a substring to upper case
  23. // note that iterator range can be directly passed to the algorithm
  24. to_upper( range );
  25. cout << "str1 with upper-cased part matching cde: " << str1 << endl;
  26. // get a head of the string
  27. iterator_range<string::iterator> head=find_head( str1, 3 );
  28. cout << "head(3) of the str1: " << string( head.begin(), head.end() ) << endl;
  29. // get the tail
  30. head=find_tail( str2, 5 );
  31. cout << "tail(5) of the str2: " << string( head.begin(), head.end() ) << endl;
  32. // char processing
  33. char text[]="hello dolly!";
  34. iterator_range<char*> crange=find_last(text,"ll");
  35. // transform the range ( add 1 )
  36. transform( crange.begin(), crange.end(), crange.begin(), bind2nd( plus<char>(), 1 ) );
  37. // uppercase the range
  38. to_upper( crange );
  39. cout << text << endl;
  40. cout << endl;
  41. return 0;
  42. }