第117讲boostalgorithmstring之分割与合并_第1页
第117讲boostalgorithmstring之分割与合并_第2页
第117讲boostalgorithmstring之分割与合并_第3页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第 117 讲 boostalgorithmstring 之分割与合并在 boost 的字符串算法中,关于分割的函数应该是有 2 个的,还记得我们在 115 讲里面说到的 find_all 吗?从某些 角度来说这也算是一种分割方式,只是这比较特殊一些,所 有的结果都是一样的而已,那么这一讲我们来说一个一般的 分割,其实关于分割大家都比较清楚的了,简单点说就是将 一个字符串按照某种方式将他分成 N 多段,这种对字符串的 操作通常很有用,在实际项目中对字符串的处理可以说是随 处可见,而分割字符串更是家常便饭了,那么说了这么多废 话,我们来点实际的东西。 /= template inline Seq

2、uenceSequenceT& split( SequenceSequenceT& Result, RangeT& Input, PredicateT Pred, token_compress_mode_type eCompress=token_compress_off ) /=这就是split的函数声明,SequenceSequenceT可以是vector, deque, list 等标准容器,对 Input 的要求也没那么多,通常 于我们来说只要是 string 或者是 boost:iterator_range 即可, 当然我们通常都是对 string 进行处理的,所以这我们就不关 心了,

3、 PredicateT 是一个判断式,可以理解为一个函数对象, 因为这里是当以当前字符为参数时返回 true 的话那么当前点 就是一个分割点, 我们不用纠结这东西, boost 本身就提供有 一个已经很完备的判断式了 :is_any_of, 我们只需要将我们要 分割的东西给他就行,看下面的例子就知道。在结果中分割 字符串是要被丢弃的。 token_compress_mode_type 这个参数 的目的简单点说就是这种形式 : ,这里是当作两个 :号来处 理呢还是就是 :号来处理?嗯, 这句话说得不清不楚, 还好在 程序有时候比语言的表述来更直接一些: /=string str(Hello :

4、World);vector result;split(result,str,is_any_of( :); / 使用空格和 : 来进行分割 / 这里会得到结果是 Hello World result.clear();split(result, str, is_any_of( :),token_compress_on); / 使用空格和 :进行分割 / result : Hellow World /= 还是代码来得实在一些,简单两句代码就将一些于我来说 很难用语言来描述的现象给解释清楚了,从上面的函数原型 里面我们看到 split 的最后一个参数是有默认值的,那就是 token_compress_

5、off ,这就是不压缩处理,而我们开启压缩处 理后,那些 : 都被当成一个 :,有因为空格和 :都是连在一起 的,而空格也是被定义为分隔符,所以也都被压缩在一起进 行处理了。于是乎我们就得到了想要的 Hellow World 了。 和 split 相对的是 join ,顾名思义,这就是将字符串连接起 来,所以他的用法很简单,不过他有两个版本,一个带有 _if 后缀的版本,这自然是需要一个判断式,只有满足判断式的 字符串才会被添加到链接队列之中,我们来看看他们的用 法: /= std:string str(Hello,World,Ni,Hao,A);std:vector v_str;boost:

6、algorithm:split(v_str, str, boost:algorithm:is_any_of(,),boost:algorithm:token_compres s_on); / 将字符串按照,号进行分割 std:string _str = boost:algorithm:join(v_str, -); / 再将字 符串重新连接起来 / Hello-World-Ni-Hao-A std:string _str2 = boost:algorithm:join_if(v_str, -, &(const std:string& s) return boost:algorithm:cont

7、ains(s, l); ); / 只连接包含 l 字符的字符串 / 结果为 Hello-World /= 不论是 split 还是 join ,他们都是对字符串进行处理,那么 很多时候我们可能想要的不是字符串,有可能是其他的东 西,比如是数字等等,为了解决这个问题,我们可以对 split 和 join 进行扩展(可以参考我们前面说的 MString ,他们可 以转变为任意类型的东西 )。/=templa te class Cvoid _split(C& result, const std:string& input,const std:string& spliter,bool isPress

8、= false) result.clear(); if (input.empty() return; std:vector _result; boost:algorithm:token_compress_mode_type _type; if (isPress) _type = boost:algorithm:token_compress_on; else _type = boost:algorithm:token_compress_off; boost:algorithm:split(_result, input, boost:is_any_of(spliter), _type);for (

9、auto& t : _result) try T _t = boost:lexical_cast(t); result.push_back(_t); catch (boost:bad_lexical_cast e) ; /=/ 针对 string 进 行特化 /=template class Cvoid _split(C& result, const std:string& input, const std:string& spliter, bool isPress = false) result.clear(); if (input.empty() return; std:vector _r

10、esult; boost:algorithm:token_compress_mode_type _type; if (isPress) _type = boost:algorithm:token_compress_on; else _type = boost:algorithm:token_compress_off; boost:algorithm:split(result, input, boost:is_any_of(spliter), _type);templateclass Cstd:string _join(const C& Input, const std:string& join

11、er) std:vector _v_str; for (auto& c : Input) try std:string str = boost:lexical_cast(c);_v_str.push_back(str); catch (boost:bad_lexical_cast e) ; return boost:join(_v_str, joiner); /= =/ 同样需要针对 string 进行特化 /= =templateclass Cstd:string _join(const C& Input, const std:string& joiner) return boost:joi

12、n(Input, joiner); /=现在使用起来就变得相对简单了:/= =int main() std:string str(hello,World,Ni,Hao,A); std:vector v_str; _split(v_str, str, ,);std:cout for (auto & v : v_str) std:cout std:string _str= _join(v_str, -);std:cout std:cout std:string _str2 = boost:algorithm:join_if(v_str, -, &(const std:string& s) return boost:algorithm:contains(s, l); );std:cout std:coutstr = Hello : World; v_str.clear(); boost:algorithm:split(v_str, str, boost:algorithm:is_any_of( :),boost:algorithm:token_compre ss_on);str = (123,345,981,3345,98); std:vector v_int; _split(v_int, str, ,);

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论