CINZY-LAB

分享免费、实用软件 | 分享编程经验、例程
04

一个string类的基类,相比stl里面的string和MFC的CString体积精简得多,但是功能较少,可完成基本的+ +=字符串连结操作,其他功能的在代码里发觉吧

文件名cy_basic_string.h;

可以使用typedef string cy_basic_string<char>;重定义

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  1. /*****************************<br>filename:cy_basic_string.h<br>by:cinzy<br>usage:cy_basic_string<char> string1;<br>*****************************/<br>#ifndef __CY_BASIC_STRING_H<br>#define __CY_BASIC_STRING_H<br>#include <windows.h><br>template<class charT>class cy_basic_string{<br> charT *pStr;<br> unsigned int nSize;<br> unsigned int nLength;
  2. <br> unsigned int cy_strlen(const charT *s){if(s==NULL)return 0;int i=0;while(s[i])i++;return i;}<br> void cy_init(const charT *str){<br> pStr=NULL;nSize=0;nLength=0;<br> cy_strcpy(str);<br> }<br> cy_basic_string &cy_strcpy(const charT *str)<br> {<br> if(str==NULL)return *this;<br> nLength = cy_strlen(str);<br> if(nLength>=nSize){<br> nSize = (nLength/256+1)*256;<br> if(pStr)<br> delete[] pStr;<br> pStr = new charT[nSize];<br> }<br> if(pStr==NULL||str==NULL)return *this;<br> charT* temp=pStr;<br> while(*temp++=*str++);<br> return *this;<br> }<br> cy_basic_string &cy_strcat(const charT *str)<br> {<br> if(pStr==NULL||str==NULL)return *this;<br> int len=cy_strlen(str);<br> nLength+=len;<br> if(nLength>=nSize){<br> nSize = (nLength/256+1)*256;<br> charT* temppStr=pStr;<br> pStr = new charT[nSize];<br> cy_strcpy(temppStr);<br> delete[]temppStr;<br> }<br> charT* temp=pStr;//+len-2;<br> while(*temp)temp++;<br> while(*temp++=*str++);<br> //*temp++=0;<br> return *this;<br> }<br>public:<br> cy_basic_string(){pStr=NULL;nSize=0;nLength=0;}<br> cy_basic_string(charT* str){cy_init(str);}<br> cy_basic_string(const cy_basic_string<charT> &copy){cy_init(copy.pStr);}
  3. <br> ~cy_basic_string(){if(pStr)delete[] pStr;}
  4. <br> operator charT* (){return pStr;}
  5. <br> const cy_basic_string& operator= (const charT * s) {return cy_strcpy(s);}<br> const cy_basic_string& operator+=(const charT * s) {return cy_strcat(s);}<br> const cy_basic_string operator+ (const charT * s) const{cy_basic_string newString(*this);newString.cy_strcat(s);return newString;}
  6. <br> const cy_basic_string& operator= (const cy_basic_string &right){if(&right!=this)return cy_strcpy(right.pStr);}<br> const cy_basic_string operator+ (const cy_basic_string &right)const{cy_basic_string newString(*this);newString.cy_strcat(right.pStr);return newString;}<br> const cy_basic_string& operator+=(const cy_basic_string &right){return cy_strcat(right.pStr);}
  7. <br> charT & operator[](int nIndex){{if(nIndex>=0&&nIndex<=nSize)return pStr[nIndex];else return (charT)0;}}<br>};
  8. <br>#endif
推荐(0)
收藏

相关日志

网友评论:

  1. process 说:

    strcpy函数都被重写了呵呵

您必须登录 才能进行评论。