新版:
/* Copyright (c) 2005-2020 sdragonx (mail:sdragonx@foxmail.com) split.hpp 2006-06-16 00:43:16 2018-07-28 19:42:00 2023-04-11 23:03:46 update 2024-12-13 21:42:04 原来不清空容器,现在清空容器 functions: size_t split(container, const char_type*, size_t, char_type, bool) size_t split(container, const char_type*, size_t, const char_type*, size_t, bool) 这是个模板函数,支持标准库里面的各种容器,和标准字符串。 这次更新,为了平台兼容性,削减到只剩两个核心函数。 测试了三款编译器,编译通过 (gcc7.2.0、c++builder6.0、C++Builder2010)。 例如: std::string str = "abc,,123"; std::vector<std::string> ls; split(ls, str.c_str(), str.length(), ',', true); 如果 repeat == true,str 就被分割成为 3 个字符串,第二个字符串是空的; 如果 repeat == false,str 就被分割成 2 个字符串。 也支持其他容器,例如: std::list<std::string> ls; split(ls, str.c_str(), str.size(), ',', true); std::deque<std::string> ls; split(ls, str.c_str(), str.size(), ',', true); 基础的两个函数,是以C字符串格式写的,也支持其他类型的字符串, 只要这个字符串有构造函数String(const char*, size_t length)就行 const char* str = "a,b,,c"; std::vector<std::string> ls; split(ls, str, strlen(str), ',', true); */ #ifndef STRING_SPLIT_HPP_200606161656 #define STRING_SPLIT_HPP_200606161656 #include <algorithm> namespace cgl { // size_t split(container, begin, end, delimiter, repeat) template<typename iterator, typename delimiter_type, class container_type> size_t split( container_type& container, iterator begin, iterator end, delimiter_type delimiter, bool repeat = true ) { typedef typename container_type::value_type element_type; iterator i; container.clear(); for (; begin < end;) { i = std::find<iterator>(begin, end, delimiter); if (begin == i) { if (repeat) container.push_back(element_type()); } else { container.push_back(element_type(begin, i)); } begin = i + 1; } if (repeat) { if (i == end - 1) { container.push_back(element_type()); } } return container.size(); } // size_t split(container, str, length, delimiter, repeat) template<typename char_type, typename delimiter_type, class container_type> size_t split( container_type container, const char_type* str, size_t size, delimiter_type delimiter, bool repeat = true ) { return split(container, str, str + size, delimiter, repeat); } // size_t split(container, begin, end, delimiterBegin, delimiterEnd, repert) template<typename iterator, typename container_type> size_t split( container_type& container, iterator begin, iterator end, iterator delimiterBegin, iterator delimiterEnd, bool repeat = true ) { typedef typename container_type::value_type element_type; iterator i; container.clear(); for (; begin < end;) { i = std::find_first_of<iterator>(begin, end, delimiterBegin, delimiterEnd); if (begin == i) { if (repeat) container.push_back(element_type()); } else { container.push_back(element_type(begin, i - begin)); } begin = i + 1; } if (repeat) { if (i == end - 1) { container.push_back(element_type()); } } return container.size(); } // size_t split(container, str, size, delimiter, delimiterSize, repert) template<typename char_type, typename container_type> size_t split( container_type& container, const char_type* str, size_t size, const char_type* delimiter, size_t delimiterSize, bool repeat = true ) { return split(container, str, str + size, delimiter, delimiter + delimiterSize, repeat); } // // std::basic_string // template< typename _char_t, class _Tx, class _Ax, template<typename, class, class> class basic_string, typename delimiter_type, class container_type> size_t split( container_type& container, const basic_string<_char_t, _Tx, _Ax>& str, delimiter_type delimiter, bool repeat = true ) { return split(container, str.begin(), str.end(), delimiter, repeat); } template< typename _char_t, class _Tx, class _Ax, template<typename, class, class> class basic_string, class container_type > size_t split( container_type& container, const basic_string<_char_t, _Tx, _Ax>& str, const basic_string<_char_t, _Tx, _Ax>& delimiter, bool repeat = true) { return split(container, str.begin(), str.end(), delimiter.begin(), delimiter.end(), repeat); } } // namespace cgl #endif // STRING_SPLIT_HPP_200606161656
以前的旧版:
/* 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
文章发布于 于 2017-02-04 07:36:37 CSDN,现转博客园。
sdragonx https://github.com/sdragonx-com