#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
void addstr(const string s, int i, int j, vector<string> &result)
{//从i到分割点j部分子串加入到result,需判断子串是否全为空,有一个不为空则可以
bool allempty = true;
for (int x = i; x < j ; x++)
{//判断子串是否全为空,如果有一个字符不为空,则加入到result
if (s[x] != ' ')
{
allempty = false;
break;
}
}
if (!allempty)
result.push_back(s.substr(i, j - i));//从i开始,j-i个字符
}
vector<string> mysplit(const string s, const char delim)
{
vector<string> result;
if (s.empty()) return result;
int i = 0;
int j = 0;
while (j < s.size() && s[j] == ' ')
{//移动到第一个不为空的字符处
++i;
++j;
}
//++j;
while (j < s.size())
{//从i到分割点j 放入result中
if (s[j] == delim)
{
addstr(s, i, j, result);
i = ++j;//i和j都指向下一个字符
}
else
++j;
}
addstr(s, i, j, result);//最后一个子串
return result;
}
int main() {
string s = ",he llo boy, I am a student, come from shangdong";
vector<string> res1 = mysplit(s, ',');
for (auto a : res1) {
cout << a << endl;
}
system("pause");
return 0;
}