请设计实现自己的字符串类(不要直接用系统 的string) 自行设计补充 类中列出的方法的返回值,参数等自行设计实 现。
时间: 2023-05-28 21:04:46 浏览: 51
以下是一个简单的字符串类的设计:
```c++
class MyString {
private:
char* str; // 字符串的字符数组
int len; // 字符串的长度
public:
MyString(); // 默认构造函数
MyString(const char* s); // 带参数的构造函数
MyString(const MyString& other); // 拷贝构造函数
~MyString(); // 析构函数
MyString& operator=(const MyString& other); // 赋值运算符
MyString operator+(const MyString& other) const; // 加法运算符
bool operator==(const MyString& other) const; // 等于运算符
bool operator!=(const MyString& other) const; // 不等于运算符
char& operator[](int index); // 下标运算符,返回引用
const char& operator[](int index) const; // 下标运算符,返回常量引用
int length() const; // 返回字符串长度
bool empty() const; // 判断字符串是否为空
void clear(); // 清空字符串
void append(const char* s); // 在字符串末尾添加一个字符串
void insert(int pos, const char* s); // 在指定位置插入一个字符串
void erase(int pos, int len); // 删除指定位置的字符子串
const char* c_str() const; // 返回C风格的字符串指针
};
```
其中,构造函数、拷贝构造函数、析构函数、赋值运算符和加法运算符的实现如下:
```c++
MyString::MyString() {
str = new char[1];
str[0] = '\0';
len = 0;
}
MyString::MyString(const char* s) {
len = strlen(s);
str = new char[len+1];
strcpy(str, s);
}
MyString::MyString(const MyString& other) {
len = other.len;
str = new char[len+1];
strcpy(str, other.str);
}
MyString::~MyString() {
delete[] str;
}
MyString& MyString::operator=(const MyString& other) {
if (this != &other) {
delete[] str;
len = other.len;
str = new char[len+1];
strcpy(str, other.str);
}
return *this;
}
MyString MyString::operator+(const MyString& other) const {
MyString newStr;
newStr.len = len + other.len;
newStr.str = new char[newStr.len+1];
strcpy(newStr.str, str);
strcat(newStr.str, other.str);
return newStr;
}
```
等于运算符和不等于运算符的实现如下:
```c++
bool MyString::operator==(const MyString& other) const {
return strcmp(str, other.str) == 0;
}
bool MyString::operator!=(const MyString& other) const {
return !(*this == other);
}
```
下标运算符的实现如下:
```c++
char& MyString::operator[](int index) {
return str[index];
}
const char& MyString::operator[](int index) const {
return str[index];
}
```
字符串长度和是否为空的实现如下:
```c++
int MyString::length() const {
return len;
}
bool MyString::empty() const {
return len == 0;
}
```
清空字符串的实现如下:
```c++
void MyString::clear() {
delete[] str;
str = new char[1];
str[0] = '\0';
len = 0;
}
```
在字符串末尾添加一个字符串的实现如下:
```c++
void MyString::append(const char* s) {
int newLen = len + strlen(s);
char* newStr = new char[newLen+1];
strcpy(newStr, str);
strcat(newStr, s);
delete[] str;
str = newStr;
len = newLen;
}
```
在指定位置插入一个字符串的实现如下:
```c++
void MyString::insert(int pos, const char* s) {
int newLen = len + strlen(s);
char* newStr = new char[newLen+1];
strncpy(newStr, str, pos);
newStr[pos] = '\0';
strcat(newStr, s);
strcat(newStr, str+pos);
delete[] str;
str = newStr;
len = newLen;
}
```
删除指定位置的字符子串的实现如下:
```c++
void MyString::erase(int pos, int len) {
int newLen = this->len - len;
char* newStr = new char[newLen+1];
strncpy(newStr, str, pos);
newStr[pos] = '\0';
strcat(newStr, str+pos+len);
delete[] str;
str = newStr;
this->len = newLen;
}
```
返回C风格的字符串指针的实现如下:
```c++
const char* MyString::c_str() const {
return str;
}
```