c/c++语言开发共享C++11实现字符串分割的示例

c++11 字符串分割代码示例如下,很显然, 使用了c++11 特性,代码简洁好多#include <iostream>#include <string>#include &l

c++11 字符串分割代码示例如下,很显然, 使用了c++11 特性,代码简洁好多

#include <iostream>  #include <string>  #include <vector>  #include <regex>     using namespace std;     //没有使用c++11特性  vector<string> testsplit(string srcstr, const string& delim)  {      int npos = 0;      vector<string> vec;      npos = srcstr.find(delim.c_str());      while(-1 != npos)      {          string temp = srcstr.substr(0, npos);          vec.push_back(temp);          srcstr = srcstr.substr(npos+1);          npos = srcstr.find(delim.c_str());      }      vec.push_back(srcstr);      return vec;  }     //使用c++11特性  vector<string> testsplit11(const string& in, const string& delim)  {      vector<string> ret;      try      {          regex re{delim};          return vector<string>{                  sregex_token_iterator(in.begin(), in.end(), re, -1),                  sregex_token_iterator()             };            }      catch(const std::exception& e)      {          cout<<"error:"<<e.what()<<std::endl;      }      return ret;  }     int main()  {      vector<string>ret = testsplit("how many credits ?", " ");      for(int i = 0 ; i < ret.size(); ++i)      {          cout<<ret[i]<<endl;      }            return 0;  }  

c++ 实现字符串分割函数 split

#include <iostream>  #include <vector>  using namespace std;    vector<string> split( strdata )  {  vector<string> vecdata;  int npos = strdata.find( "," );      while( npos > 0 )      {          strtmp = strline.substr( 0, npos );          vecdata.push_back( strtmp );            strline.erase( 0, npos+1 );          npos = strdata.find( "," );      }  vecdata.push_back( strdata );      return vecdata;  }

到此这篇关于c++11实现字符串分割的示例的文章就介绍到这了,更多相关c++11 字符串分割内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多c/c++开发分享C++11实现字符串分割的示例,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/1037713.html

(0)
上一篇 2022年1月25日
下一篇 2022年1月25日

精彩推荐