string类中各成员函数的实现
一、string类的定义
class MyString
{
public:
MyString();
MyString(char *sz);
MyString(const MyString &other);
MyString& operator=(const MyString &rhs);
MyString& operator=(const char *str);
char& operator[](unsigned int index);
const char& operator[](unsigned int index) const;
friend MyString operator+(const MyString &s1, const MyString &s2);
friend ostream& operator<<(ostream &os, const MyString &str);
friend istream& operator>>(istream &is, MyString &str);
bool isEmpty();
~MyString();
int assign(const char *obj);
void display() const;
int replace(const char *from, const char *to);
int append(const char *obj);
unsigned int getSize() const;
private:
char *m_data;
unsigned int m_len;
};
二、类成员函数的实现
MyString::MyString()
{
m_data = NULL;
m_len = 0;
}
MyString::MyString(char *sz)
{
m_len = strlen(sz);
m_data = new char[m_len + 1];
strcpy(m_data, sz);
}
MyString::MyString(const MyString &other)
{
m_len = other.m_len;
m_data = new char[m_len];
strcpy(m_data, other.m_data);
}
MyString& MyString::operator=(const MyString &rhs)
{
if(this == &rhs)
{
return *this;
}
if(m_data != NULL)
{
delete []m_data;
}
m_len = rhs.m_len;
m_data = new char[m_len + 1];
strcpy(m_data, rhs.m_data);
return *this;
}
MyString& MyString::operator=(const char *str)
{
if(m_data != NULL)
{
delete []m_data;
}
m_len = strlen(str);
m_data = new char[m_len + 1];
strcpy(m_data, str);
return *this;
}
char& MyString::operator[](unsigned int index)
{
cout << "char& MyString::operator[](unsigned int index)" << endl;
return *(m_data + index); //m_data[index]
}
const char& MyString::operator[](unsigned int index) const
{
cout << "const char& MyString::operator[](unsigned int index) const" << endl;
return *(m_data + index);
}
bool MyString::isEmpty()
{
return m_len == 0; //m_len
}
MyString::~MyString()
{
if(m_data != NULL)
{
delete []m_data;
}
}
int MyString::assign(const char *obj)
{
if(m_data != NULL)
{
delete []m_data;
}
m_len = strlen(obj);
m_data = new char[m_len + 1];
strcpy(m_data, obj);
}
void MyString::display() const
{
if(m_data == NULL)
{
cout << "str is NULL" <<endl;
}
cout << m_data << endl;
}
int MyString::replace(const char *from, const char *to)
{
char *q,*tmp,*cur;
tmp = strstr(m_data, from);
if(strlen(from) < strlen(to) || tmp == NULL)
{
return -1;
}
char *p = new char[m_len];
q = p;
cur = m_data;
for(int i = 0; i < (tmp - m_data); i++)
{
*q = *cur;
q++;
cur++;
}
*q = '\0';
strcat(p, to);
tmp = tmp + strlen(from);
strcat(p,tmp);
m_data = p;
return 0;
}
int MyString::append(const char *obj)
{
m_len += strlen(obj);
char *tmp = new char[m_len + 1];
char *p = tmp, *q = m_data;
while(*q != '\0')
{
*p = *q;
p++;
q++;
}
if(m_data != NULL)
{
delete []m_data;
m_data = NULL;
}
strcat(tmp,obj);
m_data = tmp;
return 0;
}
unsigned int MyString::getSize() const
{
return m_len;
}
三、友元函数的实现
MyString operator+(const MyString &s1, const MyString &s2)
{
int len = strlen(s1.m_data) + strlen(s2.m_data);
MyString tmp;
tmp.m_data = new char[len + 1];
tmp.m_len = len;
strcat(tmp.m_data,s1.m_data);
strcat(tmp.m_data,s2.m_data);
return tmp;
}
ostream& operator<<(ostream &os, const MyString &str)
{
os << str.m_data;
return os;
}
istream& operator>>(istream &is, MyString &str)
{
if(str.m_data != NULL)
{
delete []str.m_data;
str.m_data = NULL;
str.m_len = 0;
}
char tmp[1024] = {0};
cin >> tmp;
str.m_len = strlen(tmp);
str.m_data = new char[str.m_len + 1];
strcpy(str.m_data,tmp);
return is;
}