| 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 <iostream> |
| 12 | #include <functional> |
| 13 | #include <boost/algorithm/string/predicate.hpp> |
| 14 | #include <boost/algorithm/string/classification.hpp> |
| 15 | |
| 16 | using namespace std; |
| 17 | using namespace boost; |
| 18 | |
| 19 | int main() |
| 20 | { |
| 21 | cout << "* Predicate Example *" << endl << endl; |
| 22 | |
| 23 | string str1("123xxx321" ); |
| 24 | string str2("abc" ); |
| 25 | |
| 26 | // Check if str1 starts with '123' |
| 27 | cout << "str1 starts with \"123\": " << |
| 28 | (starts_with( Input: str1, Test: string("123" ) )?"true" :"false" ) << endl; |
| 29 | |
| 30 | // Check if str1 ends with '123' |
| 31 | cout << "str1 ends with \"123\": " << |
| 32 | (ends_with( Input: str1, Test: string("123" ) )?"true" :"false" ) << endl; |
| 33 | |
| 34 | // Check if str1 contains 'xxx' |
| 35 | cout << "str1 contains \"xxx\": " << |
| 36 | (contains( Input: str1, Test: string("xxx" ) )?"true" :"false" ) << endl; |
| 37 | |
| 38 | |
| 39 | // Check if str2 equals to 'abc' |
| 40 | cout << "str2 equals \"abc\": " << |
| 41 | (equals( Input: str2, Test: string("abc" ) )?"true" :"false" ) << endl; |
| 42 | |
| 43 | |
| 44 | // Classification functors and all predicate |
| 45 | if ( all(Input: ";.," , Pred: is_punct() ) ) |
| 46 | { |
| 47 | cout << "\";.,\" are all punctuation characters" << endl; |
| 48 | } |
| 49 | |
| 50 | // Classification predicates can be combined |
| 51 | if ( all(Input: "abcxxx" , Pred: is_any_of(Set: "xabc" ) && !is_space() ) ) |
| 52 | { |
| 53 | cout << "true" << endl; |
| 54 | } |
| 55 | |
| 56 | cout << endl; |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |