C++ 用字符串分割字符串

C++ 用字符串分割字符串,第1张

概述本文章向大家介绍C++ 用字符串分割字符串,主要包括C++ 用字符串分割字符串使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

最近学习使用C++写点工具,遇到了字符串分割的问题,上网搜索了几个例程,都是采用单个字符分割字符串,不是我想要的,于是自己动手写了一个split函数:

#include

#include

#include

using namespace std;

vector strSplit (const string &strOrig,const string &delim)

{

vector vecResult;

int intLen_d = delim.length();

if (intLen_d > 0)

{

string::size_type pos;

string strTemp;

int intLen_s = strOrig.length();

for(int i = 0; i <= intLen_s; i++)

{

pos = strOrig.find(delim,i);

if (pos > intLen_s) pos = intLen_s;

strTemp = strOrig.substr(i,pos - i);

vecResult.push_back(strTemp);

i = pos + intLen_d - 1;

}

}

else vecResult.push_back(strOrig);

return vecResult;

}

int main()

{

string t1 = "......";

vector tx;

tx = strSplitB(t1,"...");

for(int i = 0; i < tx.size(); i++)

{

cout << tx[i] << endl;

}

return 0;

}

以上代码测试用字符串“...”(3个小数点)分割字符串“......”(6个小数点)得到的向量由3个空字符组成["","",""],这就是我想要的结果。顺便提一句,这和python的split函数运行结果一致。

总结

以上是内存溢出为你收集整理的C++ 用字符串分割字符串全部内容,希望文章能够帮你解决C++ 用字符串分割字符串所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1264379.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存