函数的部分原型
- template<typename SequenceT, typename PredicateT>
- void trim_if(SequenceT & Input, PredicateT IsSpace);
- template<typename OutputIteratorT, typename RangeT, typename PredicateT>
- OutputIteratorT
- trim_copy_if(OutputIteratorT Output, const RangeT & Input,
- PredicateT IsSpace);
- template<typename SequenceT, typename PredicateT>
- SequenceT
- trim_copy_if(const SequenceT & Input, PredicateT IsSpace);
- template<typename SequenceT, typename PredicateT>
- void trim_left_if(SequenceT & Input, PredicateT IsSpace);
- template<typename OutputIteratorT, typename RangeT, typename PredicateT>
- OutputIteratorT
- trim_left_copy_if(OutputIteratorT Output, const RangeT & Input,
- PredicateT IsSpace);
- template<typename SequenceT, typename PredicateT>
- SequenceT
- trim_left_copy_if(const SequenceT & Input, PredicateT IsSpace);
例子
- void test_string_trim_if()
- {
- std::string str = "abcd!@#$%^efghi1234xyz";
- std::string str1 = boost::trim_left_copy_if(str, boost::is_alnum());
- assert(str1 == "!@#$%^efghi1234xyz");
- std::string str2 = boost::trim_right_copy_if(str, boost::is_alnum());
- assert(str2 == "abcd!@#$%^");
- std::string str3 = boost::trim_copy_if(str, boost::is_alnum());
- assert(str3 == "!@#$%^");
- boost::trim_left_if(str, boost::is_alnum());
- assert(str == "!@#$%^efghi1234xyz");
- boost::trim_right_if(str, boost::is_alnum());
- assert(str == "!@#$%^");
- // 如果是二元函数,可以做如下处理
- std::string str2nd("###GoodBye ChongQing!######");
- boost::trim_if(str2nd, bind2nd(std::equal_to<char>(), '#'));
- assert(str2nd == "GoodBye ChongQing!");
- // 当然用下面的方法也可以实现如上的功能
- std::string strAny("###Hello Fuzhou!######");
- boost::trim_if(strAny, boost::is_any_of("#"));
- assert(strAny == "Hello Fuzhou!");
- }