vector<string> split(string str, string pattern)
{
string::size_type pos;
vector<string> result;
str += pattern;//在最後加上分割類型,擴展字符串以方便操做
int size = str.size();
for (int i = 0; i<size; i++)
{
pos = str.find(pattern, i);
if (pos<size)
{
string s = str.substr(i, pos - i);
result.push_back(s);
i = pos + pattern.size() - 1;
}
}
return result;
}ci
int _tmain(int argc, _TCHAR* argv[])
{
string str;
getline(cin,str);
string pattern;
getline(cin, pattern);//用於獲取含空格的字符串
vector<string> result = split(str, pattern);
cout << "The result:" << endl;
for (int i = 0; i<result.size(); i++)
{
cout << result[i] << endl;
}
return 0;
}字符串