C++ split 分割函数

split函数是一个非常常用的函数,在Java等高级语言中都很常见,但是在标准C++中却没有,这不禁让人有些遗憾,虽然像boost等第三方库或者所谓的准标准库中已经实现了相关相关函数,但是为了这么简单的功能把庞大的函数库部署好,则是没必要的。这里提供split的一个简单实现,分string和wstring

string:

void split(vector<string> &Result, string &Input,const char* Regex)
{
	int pos = 0;
	int npos = 0;
	int regexlen = strlen(Regex);
	while((npos=Input.find(Regex, pos))!=-1)
	{
		string tmp = Input.substr(pos,npos-pos);
		Result.push_back(tmp);
		pos = npos+regexlen;
	}
	Result.push_back(Input.substr(pos,Input.length()-pos));
}

wstring

void split(vector<wstring> &Result, wstring &Input,const wchar_t* Regex)
{
	int pos = 0;
	int npos = 0;
	int regexlen = wcslen(Regex);
	while((npos=Input.find(Regex, pos))!=-1)
	{
		wstring tmp = Input.substr(pos,npos-pos);
		Result.push_back(tmp);
		pos = npos+regexlen;
	}
	Result.push_back(Input.substr(pos,Input.length()-pos));
}

这用用到了标准库的string wstring 同时还要使用命名空间std

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值