贴一个分割字符串的类

template <class TString, class TVector>
void Split(
	const TString &str,
	const TString &match,
	TVector &res,
	bool fullMatch = false
	)
{
	typedef TString::size_type (TString::*find_t)(
		const TString&, 
		TString::size_type ) const;

	TString::size_type start = 0, skip = 1;
	find_t pfind = &TString::find_first_of;

	if ( fullMatch )
	{
		// use the whole match string as a key
		// instead of individual characters
		// skip might be 0. see search loop comments
		skip = match.length();
		pfind = &TString::find;
	}

	while ( start != TString::npos )
	{
		// get a complete range [start..end)
		TString::size_type end = (str.*pfind)(match, start);

		// null strings always match in string::find, but
		// a skip of 0 causes infinite loops. pretend that
		// no tokens were found and extract the whole string
		if (skip == 0) end = TString::npos;

		TString token = str.substr(start, end - start);

		// extract the token and add it to the result list
		res.push_back(token);
		
		// start the next range
		if ( (start = end) != TString::npos ) start += skip;
	}
}
找到了一个贴代码的插件,很好很强大,贴一个很精巧的字符串分割函数
说明下,这个不是我原创的,是从网上找到的。我修改了下,加入泛型支持,TString可以是string/wstring;
TVector 可以是vector,list或者你自己的实现了push_back方法的类
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值