C++拆分字符串

#include 
#include 
#include 
using namespace std;

const vector explode(const string& s, const char& c)
{
	string buff{""};
	vector v;
	
	for(auto n:s)
	{
		if(n != c) buff+=n; else
		if(n == c && buff != "") { v.push_back(buff); buff = ""; }
	}
	if(buff != "") v.push_back(buff);
	
	return v;
}

int main()
{
	string str{"您好,欢迎开始,我们开始测试,了"};
	vector v{explode(str, ',')};
	for(auto n:v)
	{
		cout << n << endl;
	} 
	
	return 0;
}

C++拆分字符串_第1张图片

你可能感兴趣的:(C/C++)