浅尝boost之String algorithms library

 很多时候我都想在自已的代码中全部使用std::string代替MS的CString来保证我的程序在未来易于移植,但老实说CString比std::string好用很多,每每还是被诱惑了;再看看C#的string,用起来感觉更好。不过有了这个库 我可以基本抵制住诱惑了...

#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.";
  vector
<string> splitVec;
  split( splitVec, words, is_any_of(
" ,.:") ); 
  
for(int i=0; i<(int)splitVec.size(); i++)
  
{
   
if(trim_copy(splitVec[i]) != "")// 注意去掉空格,文档中的例子没做说明
    cout << splitVec[i] << endl;
  }


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

  string words = "this is a list of words, with: a bit of punctuation.";
  vector
<string> splitVec;
  boost::array
<char4>separator = {' '',''.'':'};
  
//char separator[4] = {' ', ',', '.', ':'}; 也可
  split( splitVec, words, is_any_of(separator) ); 
  
for(vector<string>::iterator iter=splitVec.begin(); iter!=splitVec.end(); ++iter)
  
{
       
if(!all(*iter, is_space())) 
        cout 
<< *iter << endl;
  }

  


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

原文地址:https://www.cnblogs.com/lzjsky/p/1934769.html