C++实现split函数(来自网上)

vector<string> split(string source, string sep)
{
	vector<string> result;
    
	int sepLen = sep.length();
	int lastPosition = 0;
	int index = -1;

	while (-1 != (index = source.find(sep, lastPosition)))
	{
		result.push_back(source.substr(lastPosition, index - lastPosition));
		lastPosition = index + sepLen;
	}

	string lastString = source.substr(lastPosition);

	if (!lastString.empty())
	{
		result.push_back(lastString);
	}
	
	return result;
}


你可能感兴趣的:(C++实现split函数(来自网上))