以字符串mark作为分隔符,对字符串s进行分割

int main()
{
	string s = "How are you?";
	string mark = " ";
	string tmp;
	int cur = 0, first = 0;
	//找到第一个标记
	while ((cur = s.find_first_of(mark, cur)) != string::npos)
	{
		//获取第一个标记前的子串
		tmp = s.substr(first, cur - first);
		cout << tmp << " ";
		//下一段有效字符
		first = ++cur;
	}
	//最后一段有效字符
	if (first != s.size())
	{
		tmp = s.substr(first, cur - first);
		cout << tmp << " ";
	}
	cout << endl;
	return 0;
}

你可能感兴趣的:(C家家精品好题,c++,算法,开发语言)