字符串分割函数

实现字符串按特定分隔符(, )进行分割,并将分割的后的字符串存在到vector中,用于处理txt、csv等格式的文件。

void split_string(const string & str, vector<string> & str_vec, char delimiter)
{
	if (str.empty())
		return;
	std::string::size_type pos = 0;
	std::string::size_type pre_pos = 0;
	while((pos = str.find_first_of(delimiter,pos)) != std::string::npos)
	{
		string tmp_str = str.substr(pre_pos,pos - pre_pos);
		str_vec.push_back(tmp_str);
		pre_pos = ++pos;
	}
	string tmp_str;
	if (pre_pos < str.size())
	{
		tmp_str = string(&str[pre_pos]);
	}
	str_vec.push_back(tmp_str);
}
原文地址:https://www.cnblogs.com/cmranger/p/4109283.html