c++自定义string类,根据声明实现功能并测试

题目要求如下:
根据给定的MyString类的声明,实现每一项功能并进行功能测试,具体代码如下:


```c++
class MyString
 {
private :   
      unsigned buflen;           //串的当前长度
      char *buffer;             //串的存储数组
 public:
      MyString (int x=1024);     
      MyString ( char );  
       MyString ( const char * );
      MyString ( const MyString& );
      ~MyString ( );
      unsigned length() const;
      //求字符串长度

      int compare (const MyString &) const;
      //字符串比较 

      bool operator < (const MyString& )const;
      void  operator += (const MyString &);
      //将串right连接到当前串*this之后

     MyString& operator = (const MyString &);
     //将串right赋给当前串*this

     int find(const MyString& );
    //字符串查找,返回主串中匹配字符串的第1个字符的位置

     void insert(const MyString&,int x=0);
     //字符串插入函数,在指定位置插入指定字符串

     void remove(const MyString& );
     //删除字符串中指定的字符串

    operator const char*() const;
     //到普通C字符串的转换

    friend istream &operator>>(istream &in,  MyString & str);
    friend ostream &operator<<(ostream &out,  const MyString & str);
     char& operator [] ( int index );
     //取当前串*this的第 index 个字符   
};


以下是我代码:

```c++
#pragma once
#include
using namespace std;
class MyString 
{
private:
    unsigned buflen;           //串的当前长度
    char *buffer;             //串的存储数组
public:
    MyString(int x = 1024);
    MyString(char);
    MyString(const char*);
    MyString(const MyString&);
    ~MyString();
    unsigned length() const;//求字符串长度
    int compare(const MyString&) const;//字符串比较 
    bool operator<(const MyString&);
    void operator+=(const MyString&);//将串right连接到当前串*this之后
    MyString& operator = (const MyString&);//将串right赋给当前串*this
    int find(const MyString&);//字符串查找,返回主串中匹配字符串的第1个字符的位置
    void insert(const MyString&, int x = 0);//字符串插入函数,在指定位置插入指定字符串
    void remove(const MyString&);//删除字符串中指定的字符串
    operator const char* ();//到普通C字符串的转换
    friend istream& operator>>(istream& in, MyString& str);
    friend ostream& operator<<(ostream& out, const MyString& str);
    char& operator [] (int index);//取当前串*this的第 index 个字符   
};
MyString::MyString(const char* other)
{
    if (other != NULL)
    {
        int len = strlen(other);
        buffer = new char[len + 1];
        strcpy(buffer, other);
    }
    else
    {
        int lengh = 0;
        buffer = new char[lengh + 1];
        strcpy(buffer, "");
    }
}
MyString::MyString(const MyString& initstr)
{
    buflen = 1 + initstr.length();
    buffer = new char[buflen];
    //for (unsigned i = 0; i < buflen - 1; i++)
    //    buffer[i] = initstr.buffer[i];
    //buffer[i] = '\0';
    strcpy(buffer,initstr.buffer);
}
MyString::~MyString()
{
    delete[]buffer;
    buflen = 0;
}
unsigned MyString::length() const
{
    return buflen;
}
int MyString::compare(const MyString& str) const
{
    return strcmp(buffer, str.buffer);
}
bool MyString::operator<(const MyString& str)
{
    for (int i = 0; i < (buflen <= str.buflen ? buflen : str.buflen); i++)
    {
        if (buflen[i] < str.buflen[i])
        {
            return true;
        }
        else if (buflen[i] > str.buflen[i])
        {
            return false;
        }
    }
    if (buflen < str.buflen)
    {
        return true;
    }
    else
    {
        return false;
    }
}
void MyString::operator+=(const MyString& str)
{
    char* pTemp = new char[buflen + str.buflen + 1];
    strcpy(pTemp, buffer);
    strcpy(pTemp + buflen, str.buffer);
    delete[]buffer;
    buffer = pTemp;
    buflen += str.buflen;
}
MyString& MyString::operator=(const MyString& right)
{
    const unsigned rightLength = right.length();
    if (right.length() >= buflen)
    {
        delete[]buffer;
        buflen = 1 + rightLength;
        buffer = new char[buflen];
    }
    //for (unsigned i = 0; i < rightLength; i++)
    //{
    //    buffer[i] = right.buffer[i];
    //}
    //buffer[i] = '\0';
    strcpy(buffer, right.buffer);
}
int MyString::find(const MyString& t)//const
{
    int i = 0, j = 0;
    while (i < strlen(buffer) && (unsigned)j < t.length())
    {
        if (buffer[i] == t[j])
        {
            ++i;
            ++j;
        }
        else
        {
            i = i - j + 1;
            j = 0;
        }
    }
    if (j > t.length() - 1)
        return(i - t.length());
    else
        return -1;
}
istream& operator>>(istream& in, MyString& str)
{
    in >> str.buffer;
    return in;
}
ostream& operator<<(ostream& out, const MyString& str)
{
    out << str.buffer << endl;
    return out;
}
char& MyString::operator[](int index)
{
    return buffer[index];
}

其中存在几处错误,我不知道该怎么修改,还有一部分没有弄明白,可能我的思路有偏差错误,感谢指正。
非常感谢!

#include<iostream>
#include<cstring>

using namespace std;

class MyString
{
private:
    unsigned buflen;           //串的当前长度
    char *buffer;             //串的存储数组

public:
    MyString(int x=1024);
    MyString(char);
    MyString(const char *);
    MyString(const MyString &);
    ~MyString();
    unsigned length() const;
    int compare(const MyString &) const;
    bool operator < (const MyString &)const;
    void operator += (const MyString &);
    MyString& operator = (const MyString &);
    int find(const MyString &);
    void insert(const MyString&, int x=0);
    void remove(const MyString &);
    operator const char*() const;
    friend istream &operator>>(istream &in, MyString & str);
    friend ostream &operator<<(ostream &out, const MyString & str);
    char& operator [] (int index);
};

MyString::MyString(int x)
{
    buflen = 0;
    buffer = new char[x];
    buffer[0] = '\0';
}

MyString::MyString(char c)
{
    buflen = 1;
    buffer = new char[2];
    buffer[0] = c;
    buffer[1] = '\0';
}

MyString::MyString(const char *s)
{
    buflen = strlen(s);
    buffer = new char[buflen + 1];
    strcpy(buffer, s);
}

MyString::MyString(const MyString &s)
{
    buflen = s.buflen;
    buffer = new char[buflen + 1];
    strcpy(buffer, s.buffer);
}

MyString::~MyString()
{
    delete [] buffer;
}

unsigned MyString::length() const
{
    return buflen;
}

int MyString::compare(const MyString &s) const
{
    return strcmp(buffer, s.buffer);
}

bool MyString::operator < (const MyString &s) const
{
    return strcmp(buffer, s.buffer) < 0;
}

void MyString::operator += (const MyString &s)
{
    buflen += s.buflen;
    char *temp = new char[buflen + 1];
    strcpy(temp, buffer);
    strcat(temp, s.buffer);
    delete [] buffer;
    buffer = temp;
}

MyString& MyString::operator = (const MyString &s)
{
    if (this != &s) {
        delete [] buffer;
        buflen = s.buflen;
        buffer = new char[buflen + 1];
        strcpy(buffer, s.buffer);
    }
    return *this;
}

int MyString::find(const MyString &s)
{
    char *p = strstr(buffer, s.buffer);
    if (p != NULL)
        return p - buffer;
    else
        return -1;
}

void MyString::insert(const MyString &s, int x)
{
    buflen += s.buflen;
    char *temp = new char[buflen + 1];
    strncpy(temp, buffer, x);
    temp[x] = '\0';
    strcat(temp, s.buffer);
    strcat(temp, buffer + x);
    delete [] buffer;
    buffer = temp;
}

void MyString::remove(const MyString &s) {
    int pos = find(s);
    while (pos != -1) {
        int len = s.length();
        for (int i = pos; i < buflen - len + 1; i++) {
            buffer[i] = buffer[i + len];
        }
        buflen -= len;
        buffer[buflen] = '\0';
        pos = find(s);
    }
}

引用chatGPT作答,以下是修改后的代码:

#pragma once
#include<iostream>
using namespace std;

class MyString
{
private:
    unsigned buflen;   //串的当前长度
    char *buffer;      //串的存储数组
public:
    MyString(int x = 1024);
    MyString(char);
    MyString(const char*);
    MyString(const MyString&);
    ~MyString();
    unsigned length() const;   //求字符串长度
    int compare(const MyString&) const;  //字符串比较 
    bool operator<(const MyString&) const;  //修改成const,否则无法比较const对象
    void operator+=(const MyString&);  //将串right连接到当前串*this之后
    MyString& operator=(const MyString&);  //将串right赋给当前串*this
    int find(const MyString&) const;  //字符串查找,返回主串中匹配字符串的第1个字符的位置
    void insert(const MyString&, int x = 0);  //字符串插入函数,在指定位置插入指定字符串
    void remove(const MyString&);  //删除字符串中指定的字符串
    operator const char*() const;  //到普通C字符串的转换
    friend istream& operator>>(istream& in, MyString& str);
    friend ostream& operator<<(ostream& out, const MyString& str);
    char& operator[](int index);  //取当前串*this的第 index 个字符   
};

MyString::MyString(int x)
{
    buflen = 0;
    buffer = new char[x + 1];
    buffer[0] = '\0';
}

MyString::MyString(char c)
{
    buflen = 1;
    buffer = new char[2];
    buffer[0] = c;
    buffer[1] = '\0';
}

MyString::MyString(const char* other)
{
    if (other != NULL)
    {
        buflen = strlen(other);
        buffer = new char[buflen + 1];
        strcpy(buffer, other);
    }
    else
    {
        buflen = 0;
        buffer = new char[1];
        buffer[0] = '\0';
    }
}

MyString::MyString(const MyString& initstr)
{
    buflen = initstr.buflen;
    buffer = new char[buflen + 1];
    strcpy(buffer, initstr.buffer);
}

MyString::~MyString()
{
    delete[] buffer;
}

unsigned MyString::length() const
{
    return buflen;
}

int MyString::compare(const MyString& str) const
{
    return strcmp(buffer, str.buffer);
}

bool MyString::operator<(const MyString& str) const
{
    return strcmp(buffer, str.buffer) < 0;
}

void MyString::operator+=(const MyString& str)
{
    buflen += str.buflen;
    char* newBuffer = new char[buflen + 1];
    strcpy(newBuffer, buffer);
    strcat(newBuffer, str.buffer);
    delete[] buffer;
    buffer = newBuffer;
}

MyString& MyString::operator=(const MyString& right)
{
    if (this != &right) // 检查自赋值
    {
        delete[] buffer;
        buflen = right.buflen;
        buffer = new char[buflen + 1];
        strcpy(buffer, right.buffer);
    }
    return *this;
}

int MyString::find(const My
//for (unsigned i...
strcpy(buffer, right.buffer);
return *this;
}
int MyString::find(const MyString& str)
{
if (buflen == 0 || buflen < str.buflen)
{
return -1;
}
for (int i = 0; i <= buflen - str.buflen; i++)
{
if (buffer[i] == str.buffer[0])
{
unsigned j = 1;
for (; j < str.buflen && buffer[i + j] == str.buffer[j]; j++)
{
// Match continues
}
if (j == str.buflen)
{
return i;
}
}
}
return -1;
}
void MyString::insert(const MyString& str, int x)
{
if (x < 0 || x >= buflen)
{
return;
}
char* pTemp = new char[buflen + str.buflen + 1];
// Copy first part
strncpy(pTemp, buffer, x);
// Insert
strcpy(pTemp + x, str.buffer);
// Copy rest
strcpy(pTemp + x + str.buflen, buffer + x);
delete[]buffer;
buffer = pTemp;
buflen += str.buflen;
}
void MyString::remove(const MyString& str)
{
const int index = find(str);
if (index >= 0)
{
// Remove by copying rest of the string
char* pTemp = new char[buflen - str.buflen + 1];
strncpy(pTemp, buffer, index);
strcpy(pTemp + index, buffer + index + str.buflen);
delete[]buffer;
buffer = pTemp;
buflen -= str.buflen;
}
}
MyString::operator const char* ()
{
return buffer;
}
istream& operator>>(istream& in, MyString& str)
{
// Get rid of leading whitespace
char c;
while (in.get(c) && isspace(c))
{
// Skip character
}
if (in)
{
str = "";
do {
str += c;
} while (in.get(c) && !isspace(c));
// Put whitespace character back into the stream
if (in)
{
in.unget();
}
}
return in;
}
ostream& operator<<(ostream& out, const MyString& str)
{
out << str.buffer;
return out;
}
char& MyString::operator[](int index)
{
if (index < 0 || index >= buflen)
{
throw "Invalid index";
}
return buffer[index];
}
return *this;
}
int MyString::find(const MyString& str)
{
int i = 0, j = 0;
while (i < buflen && j < str.buflen)
{
if (buffer[i] == str.buffer[j])
{
i++;
j++;
}
else
{
i = i - j + 1;
j = 0;
}
}
if (j >= str.buflen)
{
return i - j;
}
else
{
return -1;
}
}
void MyString::insert(const MyString& str, int x)
{
if (x < 0 || x > buflen)
{
cout << "插入位置错误" << endl;
return;
}
int newlen = buflen + str.buflen;
char* temp = new char[newlen + 1];
memcpy(temp, buffer, x);
memcpy(temp + x, str.buffer, str.buflen);
memcpy(temp + x + str.buflen, buffer + x, buflen - x + 1);
delete[]buffer;
buffer = temp;
buflen = newlen;
}
void MyString::remove(const MyString& str)
{
int i = find(str);
if (i >= 0)
{
int j = i + str.buflen;
while (j <= buflen)
{
buffer[i] = buffer[j];
i++;
j++;
}
buflen -= str.buflen;
}
}
MyString::operator const char* ()
{
return buffer;
}
istream& operator>>(istream& in, MyString& str)
{
char buffer[1024];
in >> buffer;
str = buffer;
return in;
}
ostream& operator<<(ostream& out, const MyString& str)
{
out << str.buffer;
return out;
}
char& MyString::operator[](int index)
{
if (index < 0 || index >= buflen)
{
cout << "越界" << endl;
return buffer[0];
}
else
{
return buffer[index];
}
}