| 1 | // Boost string_algo library example file ---------------------------------// |
| 2 | |
| 3 | // Copyright Pavol Droba 2002-2003. Use, modification and |
| 4 | // distribution is subject to the Boost Software License, Version |
| 5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 6 | // http://www.boost.org/LICENSE_1_0.txt) |
| 7 | |
| 8 | // See http://www.boost.org for updates, documentation, and revision history. |
| 9 | |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | #include <iostream> |
| 13 | #include <iterator> |
| 14 | #include <functional> |
| 15 | #include <boost/algorithm/string/classification.hpp> |
| 16 | #include <boost/algorithm/string/split.hpp> |
| 17 | #include <boost/algorithm/string/find_iterator.hpp> |
| 18 | |
| 19 | using namespace std; |
| 20 | using namespace boost; |
| 21 | |
| 22 | int main() |
| 23 | { |
| 24 | cout << "* Split Example *" << endl << endl; |
| 25 | |
| 26 | string str1("abc-*-ABC-*-aBc" ); |
| 27 | |
| 28 | cout << "Before: " << str1 << endl; |
| 29 | |
| 30 | // Find all 'abc' substrings (ignoring the case) |
| 31 | // Create a find_iterator |
| 32 | typedef find_iterator<string::iterator> string_find_iterator; |
| 33 | for(string_find_iterator It= |
| 34 | make_find_iterator(Collection&: str1, Finder: first_finder(Search: "abc" , Comp: is_iequal())); |
| 35 | It!=string_find_iterator(); |
| 36 | ++It) |
| 37 | { |
| 38 | cout << copy_range<std::string>(r: *It) << endl; |
| 39 | // shift all chars in the match by one |
| 40 | transform( |
| 41 | first: It->begin(), last: It->end(), |
| 42 | result: It->begin(), |
| 43 | unary_op: bind2nd( fn: plus<char>(), x: 1 ) ); |
| 44 | } |
| 45 | |
| 46 | // Print the string now |
| 47 | cout << "After: " << str1 << endl; |
| 48 | |
| 49 | // Split the string into tokens ( use '-' and '*' as delimiters ) |
| 50 | // We need copies of the input only, and adjacent tokens are compressed |
| 51 | vector<std::string> ResultCopy; |
| 52 | split(Result&: ResultCopy, Input&: str1, Pred: is_any_of(Set: "-*" ), eCompress: token_compress_on); |
| 53 | |
| 54 | for(unsigned int nIndex=0; nIndex<ResultCopy.size(); nIndex++) |
| 55 | { |
| 56 | cout << nIndex << ":" << ResultCopy[nIndex] << endl; |
| 57 | }; |
| 58 | |
| 59 | cout << endl; |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |