浅尝boost之String algorithms library#include <boost/algorithm/string.hpp>浅尝boost之String algorithms library


         很多时候我都想在自已的代码中全部使用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 //
              ...
  
  例: 

 
浅尝boost之String algorithms library string str = "boost_1_32_0.rar";
浅尝boost之String algorithms library  cout 
<< str 
浅尝boost之String algorithms library   
<< (boost::algorithm::ends_with(str, ".rar")  ? """不是"<< "rar文件" << endl;

   
  例:
 
浅尝boost之String algorithms library char text[]="hello world! ";
浅尝boost之String algorithms library      cout
浅尝boost之String algorithms library      
<< text 
浅尝boost之String algorithms library      
<< (all( text, is_lower() )? "is""is not")
浅尝boost之String algorithms library      
<< " written in the lower case" 
浅尝boost之String algorithms library      
<< endl; 
浅尝boost之String algorithms library         
// prints "hello world! is not written in the lower case"

         
          7.Split
              find_all
              ifind_all
              split
  
          split是一个我很喜欢的功能,C#中的string就有这个功能,下面是一段msdn中C#的代码
 
浅尝boost之String algorithms librarystring words = "this is a list of words, with: a bit of punctuation.";
            }
浅尝boost之String algorithms library  string words = "this is a list of words, with: a bit of punctuation.";
浅尝boost之String algorithms library  vector
<string> splitVec;
浅尝boost之String algorithms library  split( splitVec, words, is_any_of(
" ,.:") ); 
浅尝boost之String algorithms library  
for(int i=0; i<(int)splitVec.size(); i++)
  }

相关文章: