C++ 自定义字符串类 MyString 实现详解
C++ 自定义字符串类 MyString 实现详解
本文将使用 char* 存储字符串内容,实现一个自定义的字符串对象 MyString。我们将实现构造函数、拷贝构造函数、析构函数以及相关函数,例如比较大小、合并字符串、访问字符等。
类声明
class MyString {
public:
MyString(); //构造函数
~MyString(); //析构函数
MyString(const char* pstr); //构造函数
MyString(const MyString& str); //拷贝构造函数
//赋值函数
MyString& operator=(const MyString& str);
int size() const; //返回字符串长度
const char* c_str() const; //返回字符串首地址
// 比较大小
bool operator<(const MyString& str) const;
bool operator==(const MyString& str) const;
bool operator>(const MyString& str) const;
bool operator<=(const MyString& str) const;
bool operator>=(const MyString& str) const;
bool operator!=(const MyString& str) const;
//合并字符串
MyString operator+(const MyString& str) const;
MyString& operator+=(const MyString& str);
//返回某个字符,从0开始
char operator[](int index) const;
char& operator[](int index);
friend ostream& operator<<(ostream& os, const MyString& str); //输出字符串
private:
char* m_pstr;
};
类实现
#include <cstring>
#include <iostream>
using namespace std;
class MyString {
public:
// 构造函数
MyString() : m_pstr(nullptr) {}
MyString(const char* pstr) : m_pstr(nullptr) {
if (pstr) {
m_pstr = new char[strlen(pstr) + 1];
strcpy(m_pstr, pstr);
}
}
// 拷贝构造函数
MyString(const MyString& str) : m_pstr(nullptr) {
if (str.m_pstr) {
m_pstr = new char[strlen(str.m_pstr) + 1];
strcpy(m_pstr, str.m_pstr);
}
}
// 析构函数
~MyString() {
if (m_pstr) {
delete[] m_pstr;
m_pstr = nullptr;
}
}
// 赋值函数
MyString& operator=(const MyString& str) {
if (this != &str) {
MyString temp(str);
swap(m_pstr, temp.m_pstr);
}
return *this;
}
// 返回字符串长度
int size() const {
return m_pstr ? strlen(m_pstr) : 0;
}
// 返回字符串首地址
const char* c_str() const {
return m_pstr;
}
// 比较大小
bool operator<(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) < 0;
}
bool operator==(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) == 0;
}
bool operator>(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) > 0;
}
bool operator<=(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) <= 0;
}
bool operator>=(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) >= 0;
}
bool operator!=(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) != 0;
}
// 合并字符串
MyString operator+(const MyString& str) const {
MyString temp;
if (m_pstr && str.m_pstr) {
temp.m_pstr = new char[strlen(m_pstr) + strlen(str.m_pstr) + 1];
strcpy(temp.m_pstr, m_pstr);
strcat(temp.m_pstr, str.m_pstr);
}
return temp;
}
MyString& operator+=(const MyString& str) {
if (m_pstr && str.m_pstr) {
char* temp = new char[strlen(m_pstr) + strlen(str.m_pstr) + 1];
strcpy(temp, m_pstr);
strcat(temp, str.m_pstr);
delete[] m_pstr;
m_pstr = temp;
}
return *this;
}
// 返回某个字符,从0开始
char operator[](int index) const {
if (m_pstr && index >= 0 && index < strlen(m_pstr)) {
return m_pstr[index];
}
return '\0';
}
char& operator[](int index) {
static char nullchar = '\0';
if (m_pstr && index >= 0 && index < strlen(m_pstr)) {
return m_pstr[index];
}
return nullchar;
}
// 输出字符串
friend ostream& operator<<(ostream& os, const MyString& str) {
if (str.m_pstr) {
os << str.m_pstr;
}
return os;
}
private:
char* m_pstr;
};
代码分析:
- 构造函数: 负责初始化
m_pstr指针,并根据传入的字符串指针进行内存分配和字符串复制。 - 拷贝构造函数: 负责深拷贝传入的
MyString对象,避免浅拷贝导致的内存错误。 - 析构函数: 负责释放
m_pstr指针指向的内存空间。 - 赋值函数: 负责将传入的
MyString对象的内容复制到当前对象,避免浅拷贝。 size()函数: 使用strlen函数返回字符串长度。c_str()函数: 返回m_pstr指针指向的字符串首地址。- 比较大小运算符: 使用
strcmp函数比较字符串大小。 - 合并字符串运算符: 使用
strcpy和strcat函数合并字符串。 - 访问字符运算符: 通过下标访问字符串中的字符。
- 输出运算符: 使用
friend函数重载输出运算符,方便输出字符串。
注意事项:
str函数应通过引用传递,避免不必要的拷贝操作,提高效率。c_str和size函数应该是const成员函数,因为它们不应该修改成员变量。
优化代码
bool operator<(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) < 0;
}
bool operator==(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) == 0;
}
bool operator>(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) > 0;
}
bool operator<=(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) <= 0;
}
bool operator>=(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) >= 0;
}
bool operator!=(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) != 0;
}
总结
本文详细介绍了使用 char* 存储字符串内容,实现自定义字符串类 MyString 的方法。通过该类,我们可以实现字符串的各种操作,包括比较大小、合并字符串、访问字符等。同时,我们还学习了如何通过引用传递参数以提高效率,以及 const 成员函数的使用场景。
原文地址: http://www.cveoy.top/t/topic/jvD1 著作权归作者所有。请勿转载和采集!