c++自己实现一个string类
时间: 2023-10-09 15:10:30 浏览: 77
为了实现一个string类,我们需要了解以下几个关键点:
1. 字符串的存储方式:字符串可以用字符数组或者指针来存储,我们可以选择其中一个来实现。
2. 字符串的基本操作:字符串的基本操作包括赋值、拷贝、比较、连接、截取等,我们需要实现这些操作。
3. 内存管理:字符串的存储需要动态分配内存,我们需要实现内存的申请、释放等操作。
下面是一个简单的string类的实现:
```c++
#include <iostream>
#include <cstring>
class String {
public:
String(); // 默认构造函数
String(const char* str); // 带参构造函数
String(const String& other); // 拷贝构造函数
~String(); // 析构函数
String& operator=(const String& rhs); // 赋值运算符
String operator+(const String& rhs); // 连接运算符
bool operator==(const String& rhs); // 比较运算符
char operator[](int index); // 下标运算符
const char* c_str() const; // 返回字符串指针
int length() const; // 返回字符串长度
private:
char* m_data; // 存储字符串的指针
int m_length; // 字符串长度
};
// 默认构造函数
String::String() {
m_length = 0;
m_data = new char[1];
*m_data = '\0';
}
// 带参构造函数
String::String(const char* str) {
m_length = strlen(str);
m_data = new char[m_length + 1];
strcpy(m_data, str);
}
// 拷贝构造函数
String::String(const String& other) {
m_length = other.m_length;
m_data = new char[m_length + 1];
strcpy(m_data, other.m_data);
}
// 析构函数
String::~String() {
delete[] m_data;
}
// 赋值运算符
String& String::operator=(const String& rhs) {
if (this == &rhs) {
return *this;
}
delete[] m_data;
m_length = rhs.m_length;
m_data = new char[m_length + 1];
strcpy(m_data, rhs.m_data);
return *this;
}
// 连接运算符
String String::operator+(const String& rhs) {
int newLength = m_length + rhs.m_length;
char* newData = new char[newLength + 1];
strcpy(newData, m_data);
strcat(newData, rhs.m_data);
String result(newData);
delete[] newData;
return result;
}
// 比较运算符
bool String::operator==(const String& rhs) {
return strcmp(m_data, rhs.m_data) == 0;
}
// 下标运算符
char String::operator[](int index) {
if (index >= 0 && index < m_length) {
return m_data[index];
}
return '\0';
}
// 返回字符串指针
const char* String::c_str() const {
return m_data;
}
// 返回字符串长度
int String::length() const {
return m_length;
}
int main() {
String s1("hello");
String s2("world");
String s3 = s1 + s2;
std::cout << s3.c_str() << std::endl; // 输出helloworld
std::cout << (s1 == s2) << std::endl; // 输出0
std::cout << (s1 == String("hello")) << std::endl; // 输出1
std::cout << s3[1] << std::endl; // 输出e
return 0;
}
```
在这个实现中,我们使用了字符指针来存储字符串,并实现了赋值、拷贝、比较、连接、下标等基本操作,同时也实现了内存管理。当然,这只是一个简单的实现,实际上还有很多细节需要考虑,例如异常处理、性能优化等。
相关推荐
![h](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)