C++自定义字符串类MyString:实现与优化
C++自定义字符串类MyString:实现与优化
本文将使用char*实现一个自定义的C++字符串类MyString,并详细介绍其构造函数、拷贝构造函数、析构函数以及其他常用函数的实现。
1. 类声明
#include <cstring>
#include <iostream>
using namespace std;
class MyString {
public:
// 构造函数
MyString();
MyString(const char* pstr);
// 拷贝构造函数
MyString(const MyString& str);
// 析构函数
~MyString();
// 赋值函数
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;
};
2. 函数实现
#include 'MyString.h'
// 构造函数
MyString::MyString() : m_pstr(nullptr) {}
MyString::MyString(const char* pstr) : m_pstr(nullptr) {
if (pstr) {
m_pstr = new char[strlen(pstr) + 1];
strcpy(m_pstr, pstr);
}
}
// 拷贝构造函数
MyString::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::~MyString() {
if (m_pstr) {
delete[] m_pstr;
m_pstr = nullptr;
}
}
// 赋值函数
MyString& MyString::operator=(const MyString& str) {
if (this != &str) {
MyString temp(str);
swap(m_pstr, temp.m_pstr);
}
return *this;
}
// 返回字符串长度
int MyString::size() const {
return m_pstr ? strlen(m_pstr) : 0;
}
// 返回字符串首地址
const char* MyString::c_str() const {
return m_pstr;
}
// 比较大小
bool MyString::operator<(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) < 0;
}
bool MyString::operator==(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) == 0;
}
bool MyString::operator>(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) > 0;
}
bool MyString::operator<=(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) <= 0;
}
bool MyString::operator>=(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) >= 0;
}
bool MyString::operator!=(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) != 0;
}
// 合并字符串
MyString 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& 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 MyString::operator[](int index) const {
if (m_pstr && index >= 0 && index < strlen(m_pstr)) {
return m_pstr[index];
}
return '�';
}
char& MyString::operator[](int index) {
static char nullchar = '�';
if (m_pstr && index >= 0 && index < strlen(m_pstr)) {
return m_pstr[index];
}
return nullchar;
}
// 输出字符串
ostream& operator<<(ostream& os, const MyString& str) {
if (str.m_pstr) {
os << str.m_pstr;
}
return os;
}
3. 代码解释
- 构造函数: 初始化
m_pstr为空指针,或根据传入的char*分配内存并复制字符串内容。 - 拷贝构造函数: 根据传入的
MyString对象复制字符串内容,避免浅拷贝问题。 - 析构函数: 释放
m_pstr指向的内存,避免内存泄漏。 - 赋值运算符: 使用深拷贝确保正确赋值,并利用 swap 函数优化性能。
- 其他函数: 实现字符串长度获取、字符串首地址获取、字符串比较、字符串拼接、字符访问等功能。
4. 优化与注意事项
- 使用深拷贝避免内存问题。
- 在析构函数中将
m_pstr设置为nullptr,防止野指针。 - 在
operator[]中进行边界检查,提高代码健壮性。 - 可以考虑添加移动构造函数和移动赋值运算符,进一步优化性能。
总结: 本文详细介绍了如何使用 char* 实现一个自定义的 C++ 字符串类 MyString,并涵盖了内存管理、代码优化等方面的注意事项。通过学习本例,读者可以更深入地理解 C++ 类和对象的概念,以及如何编写高效、安全的代码。
原文地址: https://www.cveoy.top/t/topic/jvCV 著作权归作者所有。请勿转载和采集!