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&520分钟是多少小时gt; 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 v前赤壁赋原文及翻译ector<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; } ret深度近视urn 0;}
#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.era( 0, npos+1 ); npos = strdata.find( "," ); }vecdata.push_b联山湾ack( strdata ); return vecdata;}
到此这篇关于c++11实现字符串分割的示例的文章就介绍到这了,更多相关c++11 字符串分割内容请搜索www.887551.comrelationship可数吗以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 18:17:49,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/0cc8913657fd7ddc126948149487b712.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:C++11实现字符串分割的示例.doc
本文 PDF 下载地址:C++11实现字符串分割的示例.pdf
留言与评论(共有 0 条评论) |