免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 986 | 回复: 0
打印 上一主题 下一主题

boost字符串 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-28 15:36 |只看该作者 |倒序浏览
      

#include boost/algorithm/string.hpp>

         很多时候我都想在自已的代码中全部使用std::string代替MS的CString来保证我的程序在未来易于移植,但老实说CString比std::string好用很多,每每还是被诱惑了;再看看C#的string,用起来感觉更好。不过有了这个库 我可以基本抵制住诱惑了 :D 。
         
          来看看有哪些不错的东西,文档上列出了下面的目录(我改变了一下顺序,有些基本的,大家都能猜到是什么,这里就不多说了)
          1.Trimming  
           2.Case conversion  
          3.Replace Algorithms  
          4.Find Iterator
          5.Find algorithms  
          6.Predicates and Classification  
          7.Split  
      对6.Predicates and Classification和7.Split我是比较感兴趣的,先看看函数的列表(函数名有前缀i的是指对大 小写不敏感)。
   
          6.1.Predicates
              starts_with  // 'Starts with' predicate
              istarts_with // 'Starts with' predicate ( case insensitive )
              ends_with
              iends_with
              contains
              icontains
              equals
              iequals
              all  
          6.2.Classification
          类属断言被加入到库中主要是为了在使用算法trim()和all()可以有一些便利 :-),其实差不多形式的函数STL都有,但都是对字符而言。
              is_space // 空格
              is_alnum // 字母和数字
              is_alpha // 字母
              is_cntrl // 控制字符
              is_digit // 数字
              is_graph // 可打印字符(不含空格)
              is_lower // 小写
              is_print // 可打印字符(含空格)
              is_punct // 标点
              is_upper // 大写
              is_xdigit // 16进制数字
              is_any_of //
              ...
   
  例:
  

string str = "boost_1_32_0.rar";

  cout  str  

    (boost::algorithm::ends_with(str, ".rar")  ? "是": "不是")  "rar文件"  endl;
   
  例:
  

char text[]="hello world! ";

      cout

       text  

       (all( text, is_lower() )? "is": "is not")

       " written in the lower case"  

       endl;  

         // prints "hello world! is not written in the lower case"
         
          7.Split
              find_all
              ifind_all
              split
   
          split是一个我很喜欢的功能,C#中的string就有这个功能,下面是一段msdn中C#的代码
  

string words = "this is a list of words, with: a bit of punctuation.";


            string [] split = words.Split(new Char []

{' ', ',', '.', ':'});

            foreach (string s in split)  


            

{

                if (s.Trim() != "")

                    Console.WriteLine(s);

            }
      用起来感觉不错的。  现在有了这个库我们也可以这样使用

  string words = "this is a list of words, with: a bit of punctuation.";

  vectorstring> splitVec;

  split( splitVec, words, is_any_of(" ,.:") );  

  for(int i=0; i(int)splitVec.size(); i++)


  

{

   if(trim_copy(splitVec) != "")// 注意去掉空格,文档中的例子没做说明

    cout  splitVec  endl;

  }
      感觉与上面的差不多吧,呵,当样,这样写也不错

  string words = "this is a list of words, with: a bit of punctuation.";

  vectorstring> splitVec;


  boost::arraychar, 4>separator =

{' ', ',', '.', ':'};

  //char separator[4] = {' ', ',', '.', ':'}; 也可

  split( splitVec, words, is_any_of(separator) );  

  for(vectorstring>::iterator iter=splitVec.begin(); iter!=splitVec.end(); ++iter)


  

{

       if(!all(*iter, is_space()))  

        cout  *iter  endl;

  }

   
      到这里,可以说基本的操作都有了,也比较好用。只是由于不是跟std::string放在一起,使用上稍稍感到麻烦,不过用名字空间,现在的IDE都能有效的帮助你,如果你是用VI那就另说了 :-)
      前面大家看到不少功能,但还有一个重要的string操作大家没看到,就是CString中常用的format方法 boost也提供了一个,但感觉上是比较变态的,下篇文章会做一个介绍。


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/62281/showart_1671026.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP