C++ String类的实现
#include <iostream> using namespace std; class mystring { public: mystring(); ~mystring(){} mystring(const char *str); mystring(mystring &str); mystring& operator =(const mystring& str); mystring& operator =(const char *c_str); char operator [](const unsigned int index); mystring strcat (const mystring& str); mystring strcat (const char *c_str); mystring substring(int i,int j);//截取子链 mystring substring(int i); bool operator >(const mystring& str); bool operator >(const char *c_str); bool operator >=(const mystring& str); bool operator >=(const char *c_str); bool operator <(const mystring& str); bool operator <(const char *c_str); bool operator <=(const mystring& str); bool operator <=(const char *c_str); mystring strcopy(mystring src); mystring strcopy(char *str); char *getstring(); int length(); void clear(); private: char *m_string; unsigned int m_size; public: friend ostream& operator <<(ostream& ostr,mystring& str) { ostr<<str.getstring(); return ostr; } friend istream& operator >>(istream& istr,mystring& str) { char temp[255]; istr>>temp; str=temp; return istr; } }; mystring::mystring() { m_string=NULL; m_size=0; } mystring::mystring(const char *str) { int i=0; char *strings; m_size=0; while (str[m_size]!='\0') { m_size++; } m_string=new char[m_size+1]; strings=m_string; while(str[i]!='\0') { *strings=str[i]; strings++; i++; } *strings='\0'; } mystring::mystring(mystring &str) { char *strings,*strings1; strings1=str.m_string; m_size=str.m_size; strings=new char[m_size]; m_string=strings; while(*strings1!='\0') { *strings=*strings1; strings1++; strings++; } *strings='\0'; } char *mystring::getstring() { return m_string; } int mystring::length() { return m_size; } void mystring::clear() { if( !m_string ) { delete []m_string; m_string = NULL; } m_size = 0; } mystring& mystring::operator =(const mystring& str) { int i=0; char *strings; clear(); m_size=str.m_size; strings=new char[m_size]; m_string=strings; while(str.m_string[i]!='\0') { *strings=str.m_string[i++]; strings++; } *strings='\0'; return *this; } mystring& mystring::operator =(const char *c_str) { int i=0; char *mm_string; cout<<c_str; while (c_str[i]!='\0') i++; clear(); m_string=new char[i]; mm_string=m_string; while (c_str[i]!='\0') { *mm_string=c_str[i]; i++; mm_string++; } *mm_string='\0'; return *this; } char mystring::operator [](const unsigned int index) { return(m_string[index]); } mystring mystring::strcat (const mystring& str) { int i,j=0; char *strings1,*strings2; i=str.m_size+m_size; strings1=new char[i+1]; strings2=strings1; while (m_string[j]!='\0') { *strings1=m_string[j]; strings1++; j++; } j=0; while(str.m_string[j]!='\0') { *strings1=str.m_string[j]; strings1++; j++; } *strings1='\0'; return mystring(strings2); } mystring mystring::strcat (const char *c_str) { int i=0,j=0; char *strings1,*strings2; while(c_str[i++]!='\0'); i+=m_size; strings1=new char[i+1]; strings2=strings1; while (m_string[j]!='\0') { *strings1=m_string[j]; strings1++; j++; } j=0; while(c_str[j]!='\0') { *strings1=c_str[j]; strings1++; j++; } *strings1='\0'; return mystring(strings2); } mystring mystring::substring(int i,int j) { char *strings=new char[j]; int k=0; while (k<=j-1) strings[k++]=m_string[i]; strings[k]='\0'; return mystring(strings); } mystring mystring::substring(int i) { return substring(i,m_size-i+1); } bool mystring::operator >(const char *c_str) { unsigned int i=0; bool flag; while(m_string[i]!='\0'&&c_str[i]!='\0') { if (m_string[i]==c_str[i]) { i++; continue; } else if (m_string[i]>c_str[i]) { flag=true; break; } else { flag=false; break; } } if (i>=m_size) flag=false; else flag=m_string[i]>c_str[i]; return flag; } bool mystring::operator >(const mystring& str) { mystring a(m_string); return (a>str.m_string); } bool mystring::operator >=(const char *c_str) { unsigned int i=0; bool flag; while(m_string[i]!='\0'&&c_str[i]!='\0') { if (m_string[i]==c_str[i]) { i++; continue; } else if (m_string[i]>c_str[i]) { flag=true; break; } else { flag=false; break; } } if (m_string[i]=='\0'&&c_str[i]=='\0') flag=true; else flag=m_string[i]>c_str[i]; return flag; } bool mystring::operator >=(const mystring& str) { mystring a(m_string); return (a>=str.m_string); } bool mystring::operator <(const char *c_str) { mystring a(m_string); return !(a>=c_str); } bool mystring::operator <(const mystring& str) { mystring a(m_string); return !(a>=str.m_string); } bool mystring::operator <=(const mystring& str) { mystring a(m_string); return !(a>str.m_string); } bool mystring::operator <=(const char *c_str) { mystring a(m_string); return !(a>c_str); } mystring mystring::strcopy(char *str) { clear(); int i=0; while (str[i]!='\0') { m_string[i]=str[i]; i++; } m_string[i]='\0'; m_size=i; return *this; } mystring mystring::strcopy(mystring src) { clear(); int i=0; while (src.m_string[i]!='\0') { m_string[i]=src.m_string[i]; i++; } m_string[i]='\0'; m_size=i; return *this; } int main() { char asds[]="mmmm",bsd[]="mmmmmmmmm"; mystring a(asds); mystring b(bsd); a.strcopy(b); cout<<a; return 0; }