c++string类的实现
时间: 2024-05-07 16:21:52 浏览: 22
C++中的string类是一个非常常用的字符串类,其实现原理比较复杂,需要涉及到动态内存分配、指针、操作符重载等多个知识点。下面是一个简单的string类实现:
```c++
class String {
private:
char* data; // 存储字符串的字符数组
int length; // 字符串的长度
public:
String(); // 默认构造函数
String(const char* str); // 带参数构造函数
String(const String& str); // 复制构造函数
~String(); // 析构函数
int size() const; // 获取字符串长度
char& operator[](int index); // 重载下标操作符
const char& operator[](int index) const; // 重载下标操作符(常量版本)
String& operator=(const char* str); // 重载赋值操作符
String& operator=(const String& str); // 重载赋值操作符
String operator+(const char* str) const; // 重载加法操作符
String operator+(const String& str) const; // 重载加法操作符
bool operator==(const char* str) const; // 重载等于操作符
bool operator==(const String& str) const; // 重载等于操作符
friend ostream& operator<<(ostream& os, const String& str); // 重载输出流操作符
};
String::String() {
data = new char[1];
data[0] = '\0';
length = 0;
}
String::String(const char* str) {
if (str == nullptr) {
data = new char[1];
data[0] = '\0';
length = 0;
} else {
length = strlen(str);
data = new char[length + 1];
strcpy(data, str);
}
}
String::String(const String& str) {
length = str.length;
data = new char[length + 1];
strcpy(data, str.data);
}
String::~String() {
delete[] data;
}
int String::size() const {
return length;
}
char& String::operator[](int index) {
return data[index];
}
const char& String::operator[](int index) const {
return data[index];
}
String& String::operator=(const char* str) {
if (data != str) {
delete[] data;
if (str == nullptr) {
data = new char[1];
data[0] = '\0';
length = 0;
} else {
length = strlen(str);
data = new char[length + 1];
strcpy(data, str);
}
}
return *this;
}
String& String::operator=(const String& str) {
if (&str != this) {
delete[] data;
length = str.length;
data = new char[length + 1];
strcpy(data, str.data);
}
return *this;
}
String String::operator+(const char* str) const {
String newStr;
newStr.length = length + strlen(str);
newStr.data = new char[newStr.length + 1];
strcpy(newStr.data, data);
strcat(newStr.data, str);
return newStr;
}
String String::operator+(const String& str) const {
String newStr;
newStr.length = length + str.length;
newStr.data = new char[newStr.length + 1];
strcpy(newStr.data, data);
strcat(newStr.data, str.data);
return newStr;
}
bool String::operator==(const char* str) const {
if (strcmp(data, str) == 0) {
return true;
} else {
return false;
}
}
bool String::operator==(const String& str) const {
if (strcmp(data, str.data) == 0) {
return true;
} else {
return false;
}
}
ostream& operator<<(ostream& os, const String& str) {
os << str.data;
return os;
}
```
上述实现中,我们使用了动态内存分配来存储字符串的字符数组,同时重载了常用的操作符,如赋值操作符、加法操作符、等于操作符和输出流操作符等。需要注意的是,在使用重载操作符时,我们需要保证其语义与原始操作符一致,这样才能保证程序的正确性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)