• 【转】 一个十分有用的标准C++写的split(string) 函数


    http://doserver.net/post/standard_template_library_stl_split_function_cpp.php?page=2&part=1

     这段时间在Linux开发防火墙还有查找ARP攻击的源的软件,需要

    用到这个功能,本来自己也写了一个,感觉有点Bug,就在CSDN

    搜索了一下,找到了这个,不错!



    标准C++字符串string以及MFC6.0字符串CString的tokenize和

    split函数。

    标准串的:

    1. /******************************************** 
    2.  
    3.   the tokenize function for std::string 
    4.  
    5. *********************************************/  
    6. #include <string>  
    7. #include <vector>  
    8. #include <iostream>  
    9. using namespace std;  
    10.   
    11. typedef basic_string<char>::size_type S_T;  
    12. static const S_T npos = -1;  
    13.   
    14. ////trim指示是否保留空串,默认为保留。  
    15. vector<string> tokenize(const string& src, string tok,    
    16.   
    17. bool trim=false, string null_subst="")  
    18. {  
    19.  if( src.empty() || tok.empty() ) throw "tokenize: empty  
    20.  
    21. string\0";  
    22.    
    23.  vector<string> v;  
    24.  S_T pre_index = 0, index = 0, len = 0;  
    25.  while( (index = src.find_first_of(tok, pre_index)) !=   
    26.   
    27. npos )  
    28.  {  
    29.   if( (len = index-pre_index)!=0 )  
    30.    v.push_back(src.substr(pre_index, len));  
    31.   else if(trim==false)  
    32.    v.push_back(null_subst);  
    33.   pre_index = index+1;  
    34.  }  
    35.  string endstr = src.substr(pre_index);  
    36.  if( trim==false ) v.push_back( endstr.empty()?   
    37.   
    38. null_subst:endstr );  
    39.  else if( !endstr.empty() ) v.push_back(endstr);  
    40.  return v;  
    41. }  


    ////使用一个完整的串delimit(而不是其中的

    某个字符)来分割src串,没有trim选项,即严格分割。

    1. vector<string> split(const string& src, string delimit,   
    2.   
    3. string null_subst="")  
    4. {  
    5.  if( src.empty() || delimit.empty() ) throw "split:  
    6.  
    7. empty string\0";  
    8.   
    9.  vector<string> v;  
    10.  S_T deli_len = delimit.size();  
    11.  long index = npos, last_search_position = 0;  
    12.  while( (index=src.find(delimit,   
    13.   
    14. last_search_position))!=npos )  
    15.  {  
    16.   if(index==last_search_position)  
    17.    v.push_back(null_subst);  
    18.   else  
    19.    v.push_back( src.substr(last_search_position, index-  
    20.   
    21. last_search_position) );  
    22.   last_search_position = index + deli_len;  
    23.  }  
    24.  string last_one = src.substr(last_search_position);  
    25.  v.push_back( last_one.empty()? null_subst:last_one );  
    26.  return v;  
    27. }  



    // test

    1. int main(void)  
    2. {  
    3.  string src = ",ab,cde;,,fg,," ;  
    4.  string tok = ",;" ;  
    5.   
    6.  vector<string> v1 = tokenize(src, tok ,true);  
    7.  vector<string> v2 = tokenize(src, tok ,false,   
    8.   
    9. "<null>");  
    10.   
    11.  cout<<"-------------v1:"<<endl;  
    12.  for(int i=0; i<v1.size();i++)  
    13.  {  
    14.   cout<<v1[i].c_str()<<endl;  
    15.  }  
    16.    
    17.  cout<<"-------------v2:"<<endl;  
    18.  for(int j=0; j<v2.size();j++)  
    19.  {  
    20.   cout<<v2[j].c_str()<<endl;  
    21.  }  
    22.   
    23.  try{  
    24.    
    25.   string s = "######123#4###56########789###";  
    26.   string del = "";//"###";  
    27.   vector<string> v3 = split(s, del, "<null>");  
    28.   cout<<"-------------v3:"<<endl;  
    29.   for(int k=0; k<v3.size();k++)  
    30.   {  
    31.    cout<<v3[k].c_str()<<endl;  
    32.   }  
    33.  }  
    34.  catch (char *s) {  
    35.   cout<<s<<endl;  
    36.  }  
    37.   
    38.  return 0;  

  • 相关阅读:
    完美解决 向UILable 文字最后插入N张图片,支持向限制行数的UILable 最后一行插入,多余文字显示...
    构建自己的NSZombie
    如何以代码形式插入断点
    根据坐标点显示地图显示范围(高德地图)
    ios7 UITableView 分割线在 使用selectedBackgroundView 选中时有些不显示
    runtime MethodSwizzle 实践之 奇怪crash : [UIKeyboardLayoutStar release]: message sent to deallocated instance
    Jmeter(十一)
    Jmeter(十)
    Jmeter(九)
    Jmeter(八)
  • 原文地址:https://www.cnblogs.com/fzzl/p/1521766.html
Copyright © 2020-2023  润新知