首页 > 学院 > 开发设计 > 正文

c++ 字符串分割函数stringsplit

2019-11-14 12:01:41
字体:
来源:转载
供稿:网友
/* stringsplit.hpp sdragonx 2006-06-16 00:43:16 revise  2016.07.18 19:04*/#ifndef STRINGSPLIT_HPP_200606161656#define STRINGSPLIT_HPP_200606161656#include <algorithm>namespace cgl{template<typename char_type, template<typename> class string_type, template<typename> class container>size_t stringsplit(	container< string_type<char_type> >& ls,	typename string_type<char_type>::const_iterator begin,	typename string_type<char_type>::const_iterator end,	char_type spliter,	bool repeat = true){    if(end <= begin)	{    	return 0;	}    typename string_type<char_type>::const_iterator first = begin;    typename string_type<char_type>::const_iterator second;        for( ; first<end; )    {        second = std::find<string_type<char_type>::const_iterator>(first, end, spliter);        if(first == second){            if(repeat)ls.push_back(string_type<char_type>());        }        else{            ls.push_back(string_type<char_type>(first, second));    	}        first = second+1;    }    if(repeat)    {        if(second == end-1){        	ls.push_back(string_type<char_type>());        }    }    return ls.size();}template<typename char_type, template<typename> class string_type, template<typename> class container>size_t stringsplit(	container< string_type<char_type> >& ls,    typename string_type<char_type>::const_iterator begin,    typename string_type<char_type>::const_iterator end,    typename string_type<char_type>::const_iterator spliter_begin,    typename string_type<char_type>::const_iterator spliter_end,    bool repeat = true){    if(end <= begin || spliter_end<=spliter_begin)	{    	return 0;    }    typename string_type<char_type>::const_iterator first = begin;    typename string_type<char_type>::const_iterator second;        for( ; first<end; )    {        second = std::find_first_of<string_type<char_type>::const_iterator>(first, end, spliter_begin, spliter_end);        if(first == second){            if(repeat)ls.push_back(string_type<char_type>());        }        else{        	ls.push_back(string_type<char_type>(first, second));        }        first = second+1;    }    if(repeat)    {        if(second == end-1){        	ls.push_back(string_type<char_type>());        }    }    return ls.size();}template<typename char_type, template<typename> class string_type, template<typename> class container>size_t stringsplit(container< string_type<char_type> > &strs,	const string_type<char_type>& str, char_type spliter, bool repeat = true){    return stringsplit(strs, str.begin(), str.end(), spliter, repeat);}template<typename char_type, template<typename> class string_type, template<typename> class container>size_t stringsplit(container< string_type<char_type> > &strs,	const char_type* str, size_t length, char_type spliter, bool repeat = true){    return stringsplit(strs, str, str+length, spliter, repeat);}template<typename char_type, template<typename> class string_type, template<typename> class container>size_t stringsplit(container< string_type<char_type> > &strs,	const string_type<char_type>& str, const string_type<char_type>& spliter, bool repeat = true){    return stringsplit(strs, str.begin(), str.end(), spliter.begin(), spliter.end(), repeat);}template<typename char_type, template<typename> class string_type, template<typename> class container>size_t stringsplit(container< string_type<char_type> > &strs,	const char_type* str, size_t length, const char_type* spliter, size_t splength, bool repeat = true){    return stringsplit(strs, str, str+length, spliter, spliter+splength, repeat);}}; // end namespace cgl;#endif //STRINGSPLIT_HPP_200606161656
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表