split_example.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 <vector>
  9. #include <iostream>
  10. #include <iterator>
  11. #include <functional>
  12. #include <boost/algorithm/string/classification.hpp>
  13. #include <boost/algorithm/string/split.hpp>
  14. #include <boost/algorithm/string/find_iterator.hpp>
  15. using namespace std;
  16. using namespace boost;
  17. int main()
  18. {
  19. cout << "* Split Example *" << endl << endl;
  20. string str1("abc-*-ABC-*-aBc");
  21. cout << "Before: " << str1 << endl;
  22. // Find all 'abc' substrings (ignoring the case)
  23. // Create a find_iterator
  24. typedef find_iterator<string::iterator> string_find_iterator;
  25. for(string_find_iterator It=
  26. make_find_iterator(str1, first_finder("abc", is_iequal()));
  27. It!=string_find_iterator();
  28. ++It)
  29. {
  30. cout << copy_range<std::string>(*It) << endl;
  31. // shift all chars in the match by one
  32. transform(
  33. It->begin(), It->end(),
  34. It->begin(),
  35. bind2nd( plus<char>(), 1 ) );
  36. }
  37. // Print the string now
  38. cout << "After: " << str1 << endl;
  39. // Split the string into tokens ( use '-' and '*' as delimiters )
  40. // We need copies of the input only, and adjacent tokens are compressed
  41. vector<std::string> ResultCopy;
  42. split(ResultCopy, str1, is_any_of("-*"), token_compress_on);
  43. for(unsigned int nIndex=0; nIndex<ResultCopy.size(); nIndex++)
  44. {
  45. cout << nIndex << ":" << ResultCopy[nIndex] << endl;
  46. };
  47. cout << endl;
  48. return 0;
  49. }