Boost的字符串算法库(boost.range)


转载自:http://blog.csdn.net/alsm168/article/details/6478000


查找

    一些泛型算法的实现,大量的使用了

boost.range

:


find_first()    // 查找第一次出现 ifind_first() find_last()  // 查找最后一次出现 ifind_last() find_nth()   // 查找第 N 次出现,从 0 次开始 ifind_nth() find_head()  // head 和 tail 跟 substr 类似 find_tail() find_token() // 查找满足条件的字符位置 find_regex() // 查找匹配正则表达式字串 find()  // 普通版 例子:
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <boost/algorithm/string.hpp>
  5.  
  6. using namespace std;
  7.  
  8. #define Print( exp ) cout<< #exp <<"="<< exp << endl
  9.  
  10. using namespace boost;
  11.  
  12. int main()
  13.     string str( "Hello World!" );
  14.     Print( str );
  15.     iterator_range< string::iterator > range = 
  16.         algorithm::find_last( str,   "or" );
  17.     algorithm::to_upper( range );
  18.     Print( str );
  19.  
  20.     copy( range.begin(), range.end(),ostream_iterator<char>( cout," " ) );
  21.  
  22.     algorithm::to_lower( range );
  23.  
  24.     str += str + str;
  25.  
  26.     range = algorithm::find_nth( str,  "Wor" ,2 );
  27.  
  28.     Print( str );
  29.     cout<< str.size() << endl;
  30.     cout<< distance( str.begin(),range.begin() )<< endl;
  31.  
  32.     range = algorithm::find_tail( str, 3 );
  33.  
  34.     Print( string( range.begin(),range.end() ) );
  35.  
  36.     range = algorithm::find_token( str, algorithm::is_punct() );
  37.  
  38.     cout<< range.size() << endl;
  39.  
  40.     Print( string( range.begin(),range.end() ) );
  41.  
  42.     cout<< distance( str.begin(), range.begin() ) << endl;
  43.  
  44.     return 0;
  45. }

输出: str=Hello World! str=Hello WORld! O R str=Hello World!Hello World!Hello World! 36 30 string( range.begin(),range.end() )=ld! 1 string( range.begin(),range.end() )=! 11 请按任意键继续. . . 
删除和替换 算法名称 描述 函数
replace/erase_first替换/删除第一次出现在被查找串中的子串replace_first() 
replace_first_copy() 
ireplace_first() 
ireplace_first_copy() 
erase_first() 
erase_first_copy() 
ierase_first() 
ierase_first_copy()
replace/erase_last替换/删除最后一次出现在被查找串中的子串replace_last() 
replace_last_copy() 
ireplace_last() 
ireplace_last_copy() 
erase_last() 
erase_last_copy() 
ierase_last() 
ierase_last_copy()
replace/erase_nth替换/删除第n次出现在被查找串中的子串replace_nth() 
replace_nth_copy() 
ireplace_nth() 
ireplace_nth_copy() 
erase_nth() 
erase_nth_copy() 
ierase_nth() 
ierase_nth_copy()
replace/erase_all替换/删除所有的子串replace_all() 
replace_all_copy() 
ireplace_all() 
ireplace_all_copy() 
erase_all() 
erase_all_copy() 
ierase_all() 
ierase_all_copy()
replace/erase_head替换/删除头部几个字符replace_head() 
replace_head_copy() 
erase_head() 
erase_head_copy()
replace/erase_tail替换/删除尾部几个字符replace_tail() 
replace_tail_copy() 
erase_tail() 
erase_tail_copy()
replace/erase_regex替换/删除一个匹配正则表达式的子串replace_regex() 
replace_regex_copy() 
erase_regex() 
erase_regex_copy()
replace/erase_regex_all替换/删除所有匹配正则表达式的子串replace_all_regex() 
replace_all_regex_copy() 
erase_all_regex() 
erase_all_regex_copy()
find_format通用替换算法find_format() 
find_format_copy() 
find_format_all() 
find_format_all_copy()()

 
一个简单例子:
  1. #include <boost/algorithm/string.hpp>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     string res( "Hello World!_Hello World!" );
  9.     string de( "Wor" );
  10.     string rep( "1wor1" );
  11.  
  12.     // 把第一次出现的Wor替换成wor1!!
  13.     boost::algorithm::replace_first( res,de,rep );
  14.  
  15.     cout<< res << endl;
  16.     // 忽略大小写的删除所有的wor
  17.     boost::algorithm::ierase_all( res,de ); 
  18.     cout<< res << endl;
  19.  
  20.     return 0;
  21. }
    其它的都类似,这里就不多写了,由此可见,boost 下面的字符串算法库真的很好很强大!同时也可以看到这个部分的内容是非常多的。

分割和组合

templatetypename SequenceSequenceT, typename RangeT, typename PredicateT >

inline SequenceSequenceT& split(

SequenceSequenceT& Result,

RangeT& Input,

PredicateT Pred,

token_compress_mode_type eCompress=token_compress_off )

    把指定的字符串按照要求分开。需要自己提供断言。

templatetypename SequenceSequenceT, typename Range1T>

inline typename range_value<SequenceSequenceT>::type 

join(

const SequenceSequenceT& Input,

const Range1T& Separator)

 

template<  typename SequenceSequenceT, typename Range1T,    typename PredicateT>

inline typename range_value<SequenceSequenceT>::type 

join_if(

const SequenceSequenceT& Input,

const Range1T& Separator,

PredicateT Pred)

    结合字符串,后者组合的字符串必须通过断言认证才能被组合,否则会被忽略。
一个简单例子:
  1. #include <iostream>
  2. #include <vector>
  3. #include <cassert>
  4. #include <boost/algorithm/string.hpp>
  5.  
  6. using namespace std;
  7.  
  8. bool isEvenFirst( const string & str )
  9. {
  10.     return str[0] == '2' || str[0] == '4'||
  11.        str[0] == '6' || str[0] == '8';
  12. }
  13.  
  14. int main()
  15. {
  16.     string ss( "HelloWorld!He.lloWorld!he" );
  17.  
  18.     vector<string> tmp;
  19.     // 以标点符号分开!
  20.     vector<string>& tt = 
  21.         boost::algorithm::split( tmp, ss, boost::algorithm::is_punct() );
  22.  
  23.     assert( boost::addressof(tmp) == boost::addressof(tt) );
  24.     copy( tt.begin(),tt.end(),ostream_iterator< string >( cout," " ) );
  25.  
  26.     tmp.clear();
  27.     int i = 10;
  28.     char sz[3];
  29.     while( i )
  30.     {
  31.         sprintf_s(sz,"%d",i-- );
  32.         tmp.push_back( sz );
  33.     }
  34.  
  35.     cout<< boost::algorithm::join( tmp,    "%x%" )<<endl;
  36.  
  37.     cout<< boost::algorithm::join_if( tmp, "%x%",isEvenFirst )<<endl;
  38.  
  39.     return 0;
  40. }
输出: HelloWorld He lloWorld he 10%x%9%x%8%x%7%x%6%x%5%x%4%x%3%x%2%x%1 8%x%6%x%4%x%2 请按任意键继续. . . 
其它     其它还有一些查找迭代子、分割迭代子等等内容,此处就不说了,因为这并不是非常重要,我感觉,呵呵。
    下面列举一些 boost 提供的判断式:
判断式名称 描述 生成器
is_classified基于分类的通用 ctype 掩码is_classified()
is_space识别空格is_space()
is_alnum识别字母和数字字符is_alnum()
is_alpha识别字母is_alpha()
is_cntrl识别控制字符is_cntrl()
is_digit识别十进制数字is_digit()
is_graph识别图形字符is_graph()
is_lower识别小写字符is_lower()
is_print识别可打印字符is_print()
is_punct识别标点符号字符is_punct()
is_upper识别大写字符is_upper()
is_xdigit识别十六进制数字is_xdigit()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值