C ++ delimiter

Source: Internet
Author: User

I read a blog in csdn: http://blog.csdn.net/shuice/article/details/5618058

Is a class for string splitting. However, the Code provided by the author cannot be compiled in vc6.0, and I am not very familiar with the definition of this type of template.

template<>class SplitAssist<char>{public:static long GetLength(const char* pszStr){return strlen(pszStr);}static const char* FindString(const char * pszSource, const char * pszFlags){return strstr(pszSource, pszFlags);}};

I don't know what the role of the template <char> is to provide parameters.

So I made some modifications to his code.

// File name splitstring. h // purpose: Split the string class // call example: // vector <string> vectstr1; // vector <cstring> vectstr2; // list <wstring> liststr3; // splitstring ("Hello World", "", vectstr1); // splitstring (_ T ("Hello World"), _ T (""), vectstr2 ); // splitstring (L ("Hello World"), L (""), liststr3); # ifndef _ split_string_h _ # DEFINE _ split_string_h _ # include <string. h> class splitassist {public: static long getlength (const char * pszs Tr) {return strlen (pszstr);} static long getlength (const wchar_t * pwszstr) {return wcslen (pwszstr);} static const char * findstring (const char * pszsource, const char * pszflags) {return strstr (pszsource, pszflags);} static const wchar_t * findstring (const wchar_t * pwszsource, const wchar_t * szflags) {return wcsstr (pwszsource, pwszflags) ;}}; template <class basetype, class stringcontenttype> void splitstrin G (const basetype * pszsource, const basetype * pszflags, stringcontenttype & strreturn) {strreturn. clear (); If (pszsource = NULL) | (pszflags = NULL) {return;} size_t isourcelen = splitassist: getlength (pszsource ); size_t iflaglen = splitassist: getlength (pszflags); If (iflaglen = 0) {stringcontenttype: value_type STR (pszsource, isourcelen); strreturn. push_back (STR); return;} const basetype * pposstart = Pszsource; For (const basetype * pposfind = splitassist: findstring (pposstart, pszflags); pposfind! = NULL; pposfind = splitassist: findstring (pposstart, pszflags) {int ilenvalid = pposfind-pposstart; If (ilenvalid> 0) {stringcontenttype: value_type STR (pposstart, ilenvalid); strreturn. push_back (STR);} pposstart = pposfind + iflaglen;} int ilenvalid = isourcelen-(pposstart-pszsource); If (ilenvalid> 0) {stringcontenttype: value_type STR (pposstart, ilenvalid); strreturn. push_back (STR) ;}# endif

Welcome to the discussion!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.