MyString类 -- 面试问题总结
针对以下代码总结常见面试问题
cpp
#include<iostream>
#include<cstring>
using namespace std;
class MyString
{
private:
char* p;
public:
MyString()
{
p = new char[1];
p[0] = '\0';
}
MyString(const char* str)
{
p = new char[strlen(str) + 1];
strcpy(p, str);
}
MyString(const MyString & S)
{
p = new char[strlen(S.p) + 1];
strcpy(p, S.p);
}
~MyString()
{
delete []p;
}
MyString & operator= (const char* str)
{
if (p == str)
return *this;
delete []p;
p = new char[strlen(str) + 1];
strcpy(p, str);
return *this;
}
MyString & operator= (const MyString & S)
{
if (this == &S)
return *this;
delete []p;
p = new char[strlen(S.p) + 1];
strcpy(p, S.p);
return *this;
}
friend ostream & operator<< (ostream & os, const MyString & S)
{
os << S.p;
return os;
}
MyString operator+ (const MyString & S) const
{
MyString temp(p);
strcat(temp.p, S.p);
return temp;
}
MyString operator+ (const char* str) const
{
MyString temp(str);
return *this + temp;
}
friend MyString operator+ (const char* str, const MyString & S)
{
MyString temp(str);
return temp + S;
}
MyString & operator+= (const char* str)
{
*this = *this + str;
return *this;
}
char & operator[] (int idx)
{
return p[idx];
}
char operator[] (int idx) const
{
return p[idx];
}
MyString operator() (int start, int len)
{
int size = strlen(p);
if (start < 0 || start >= size)
return MyString{};
int actualLen = start + len - 1 < size ? len : size - start;
char* str = new char[actualLen + 1];
strncpy(str, p + start, actualLen);
str[actualLen] = '\0';
MyString temp(str);
delete []str;
return temp;
}
bool operator> (const MyString & S) const
{
return strcmp(p, S.p) > 0;
}
bool operator< (const MyString & S) const
{
return strcmp(p, S.p) < 0;
}
bool operator== (const MyString & S) const
{
return strcmp(p, S.p) == 0;
}
};
/* qsort()自定义比较函数 */
int CompareString( const void * e1, const void * e2)
{
const MyString* s1 = (const MyString*)e1;
const MyString* s2 = (const MyString*)e2;
if (*s1 > *s2)
return 1;
else if (*s1 < *s2)
return -1;
else
return 0;
}
int main()
{
MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);
MyString SArray[4] = {"big","me","about","take"};
cout << "1. " << s1 << s2 << s3<< s4<< endl;
s4 = s3;
s3 = s1 + s3;
cout << "2. " << s1 << endl;
cout << "3. " << s2 << endl;
cout << "4. " << s3 << endl;
cout << "5. " << s4 << endl;
cout << "6. " << s1[2] << endl;
s2 = s1;
s1 = "ijkl-";
s1[2] = 'A' ;
cout << "7. " << s2 << endl;
cout << "8. " << s1 << endl;
s1 += "mnop";
cout << "9. " << s1 << endl;
s4 = "qrst-" + s2;
cout << "10. " << s4 << endl;
s1 = s2 + s4 + " uvw " + "xyz";
cout << "11. " << s1 << endl;
qsort(SArray,4,sizeof(MyString),CompareString);
for( int i = 0;i < 4;i ++ )
cout << SArray[i] << endl;
//s1的从下标0开始长度为4的子串
cout << s1(0,4) << endl;
//s1的从下标5开始长度为10的子串
cout << s1(5,10) << endl;
return 0;
}
1. 你在 MyString 类中定义了私有成员 char* p ,为什么要使⽤动态内存分配?不动态分配(⽐如⽤固定数组)会有什么问题?
1)使用动态内存分配:可以灵活地根据字符串长度分配内存空间,避免了内存溢出或造成内存大量浪费
2)使用固定数组:
- 长度受限:字符串过长会造成内存溢出、程序崩溃 ;字符串过短会大量内存浪费
- 无法灵活地进行赋值/拼接:固定数组不能扩容/缩容,赋值拼接操作失效
- 对象体积臃肿:每一个对象都会携带一个固定长度的字符数组,内存占用极高