您的位置:首页 > 编程语言 > C语言/C++

C++ 分割字符串-Split

2016-09-01 21:17 309 查看
通过stringstream的getline 分割string

#include "stdafx.h"
#include <string>
#include <vector>
#include <sstream>
using namespace std;

vector<string> Split(string str, char de[])
{
vector<string> vec = {str};
vector<string> desVec;
size_t DeSize = sizeof(de) / sizeof(de[0]);
for (size_t i = 0; i < DeSize; i++)
{
desVec.clear();
for (size_t j = 0; j < vec.size(); j++)
{
stringstream ss(vec[j]);
string sub_str;
while (getline(ss, sub_str, de[i]))
{
desVec.push_back(sub_str);
}
}
vec = desVec;
}
return desVec;
}

int main()
{

char de[] = {'1', '2', '3', '4'};
auto str = Split("1f22d42r", de);
for (size_t i = 0; i < str.size(); i++)
{
printf_s("%s\n", str.at(i).c_str());
}
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息