1.重载赋值运算符函数:(具体见代码)
//普通做法 CMyString& CMyString::operator=(const CMyString& str) { if (this == &str) return *this; delete[] m_Pdata; m_Pdata = new char[strlen(str.m_Pdata)+1]; strcpy(m_Pdata,str.m_Pdata); return *this; } //更加安全的做法,普通做法在new内存不足情况下,已经将原值delete CMyString& CMyString::operator=(const CMyString& str) { if (this != &str) { CMyString strTemp(str); char* temp = str.m_Pdata; //通过strTemp的析构函数delete掉原值 strTemp.m_Pdata = m_Pdata; m_Pdata = temp; } return *this; }
2.替换字符串中的空白字符
瞎了,做了一遍结果还是出错。。。确实要多练练
class Solution { public: //length为数组容量 void replaceSpace(char *str,int length) { if(length<=0||str==NULL) return; int i=0,n_blank=0,n_str=0,n_strlength=0; while(str[i]!='