转载自: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() // 普通版 例子:
- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <boost/algorithm/string.hpp>
- using namespace std;
- #define Print( exp ) cout<< #exp <<"="<< exp << endl
- using namespace boost;
- int main()
- {
- string str( "Hello World!" );
- Print( str );
- iterator_range< string::iterator > range =
- algorithm::find_last( str, "or" );
- algorithm::to_upper( range );
- Print( str );
- copy( range.begin(), range.end(),ostream_iterator<char>( cout," " ) );
- algorithm::to_lower( range );
- str += str + str;
- range = algorithm::find_nth( str, "Wor" ,2 );
- Print( str );
- cout<< str.size() << endl;
- cout<< distance( str.begin(),range.begin() )<< endl;
- range = algorithm::find_tail( str, 3 );
- Print( string( range.begin(),range.end() ) );
- range = algorithm::find_token( str, algorithm::is_punct() );
- cout<< range.size() << endl;
- Print( string( range.begin(),range.end() ) );
- cout<< distance( str.begin(), range.begin() ) << endl;
- return 0;
- }
输出: 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 请按任意键继续. . .
- #include <boost/algorithm/string.hpp>
- #include <iostream>
- using namespace std;
- int main()
- {
- string res( "Hello World!_Hello World!" );
- string de( "Wor" );
- string rep( "1wor1" );
- // 把第一次出现的Wor替换成wor1!!
- boost::algorithm::replace_first( res,de,rep );
- cout<< res << endl;
- // 忽略大小写的删除所有的wor
- boost::algorithm::ierase_all( res,de );
- cout<< res << endl;
- return 0;
- }
分割和组合
template< typename SequenceSequenceT, typename RangeT, typename PredicateT >
inline SequenceSequenceT& split(
SequenceSequenceT& Result,
RangeT& Input,
PredicateT Pred,
token_compress_mode_type eCompress=token_compress_off )
把指定的字符串按照要求分开。需要自己提供断言。template< typename 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)
结合字符串,后者组合的字符串必须通过断言认证才能被组合,否则会被忽略。一个简单例子:
- #include <iostream>
- #include <vector>
- #include <cassert>
- #include <boost/algorithm/string.hpp>
- using namespace std;
- bool isEvenFirst( const string & str )
- {
- return str[0] == '2' || str[0] == '4'||
- str[0] == '6' || str[0] == '8';
- }
- int main()
- {
- string ss( "HelloWorld!He.lloWorld!he" );
- vector<string> tmp;
- // 以标点符号分开!
- vector<string>& tt =
- boost::algorithm::split( tmp, ss, boost::algorithm::is_punct() );
- assert( boost::addressof(tmp) == boost::addressof(tt) );
- copy( tt.begin(),tt.end(),ostream_iterator< string >( cout," " ) );
- tmp.clear();
- int i = 10;
- char sz[3];
- while( i )
- {
- sprintf_s(sz,"%d",i-- );
- tmp.push_back( sz );
- }
- cout<< boost::algorithm::join( tmp, "%x%" )<<endl;
- cout<< boost::algorithm::join_if( tmp, "%x%",isEvenFirst )<<endl;
- return 0;
- }
其它 其它还有一些查找迭代子、分割迭代子等等内容,此处就不说了,因为这并不是非常重要,我感觉,呵呵。
下面列举一些 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() |