可以用string类的find和substr函数

1. find函数

函数原型:size_t find(const string& str, size_t pos = 0) const;
功能说明:从pos位置开始查找子字符串str第一次出现的位置
参数说明:str为要查找的子字符串,pos从为初始查找位置
返回值:找到的话返回子字符串第一次出现的位置,否则返回string::npos

2. substr函数

函数原型:string substr(size_t pos = 0, size_t n = npos) const;
功能说明:获取从指定的起始位置开始,长度为n的子字符串
参数说明:pos为起始位置,n获取的1字符串长度
返回值:子字符串

// 字符串分割
vector<string> split(const string &str, const string &pattern)
{
	vector<string> res;
	if (str == "")
		return res;

	//在字符串末尾也加入分隔符,方便截取最后一段
	string strs = str + pattern;
	size_t pos = strs.find(pattern);

	while (pos != strs.npos)
	{
		string temp = strs.substr(0, pos);
		res.push_back(temp);
		//去掉已分割的字符串,在剩下的字符串中进行分割
		strs = strs.substr(pos + 1, strs.size());
		pos = strs.find(pattern);
	}

	return res;
}