c++ 字符串分割写法 一般比较常用 懒得每次都写 记录下

std::vector<std::string> split(const std::string& str, const std::string& separator)
{
	std::vector<std::string> str_result;
	if (str.empty() || separator.empty()){return str_result;}
	std::string::size_type pos = str.find_first_of(separator);
	if (pos != std::string::npos)
	{
		str_result.emplace_back(str.substr(0, pos));
		while (pos != std::string::npos)
		{
			std::string::size_type next = str.find(separator, pos + separator.size());
			std::string str_temp_sub;
			if (next != std::string::npos)
			{
				str_temp_sub = str.substr(pos + separator.size(), next - (pos + separator.size()));
				str_result.emplace_back(str_temp_sub);
			}
			else
			{
				str_temp_sub = str.substr(pos + separator.size(), -1);
				str_result.emplace_back(str_temp_sub);
			}
			pos = next;
		}
	}
	return str_result;
}
int main()
{
	for (auto& v : split("285894_A789646_65489", "_"))
		std::cout << v << std::endl;
}
posted @ 2020-12-11 12:49  轻风々  阅读(125)  评论(0编辑  收藏  举报