C++自定义字符串类MyString实现与用法详解
C++自定义字符串类MyString实现与用法详解
在C++编程中,字符串是必不可少的数据类型。虽然C++提供了标准字符串类std::string,但在某些情况下,我们可能需要自定义字符串类来满足特定需求,例如学习底层实现机制、优化性能等。
本文将介绍如何使用char*实现一个自定义的C++字符串类MyString,包含以下功能:
- 构造函数
- 拷贝构造函数
- 析构函数
- 赋值函数
- 返回字符串长度
- 返回字符串首地址
- 比较大小
- 合并字符串
- 返回某个字符
- 输出字符串
MyString类定义
#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;
};
MyString类实现
// 构造函数
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;
}
boool MyString::operator==(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) == 0;
}
boool MyString::operator>(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) > 0;
}
boool MyString::operator<=(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) <= 0;
}
boool MyString::operator>=(const MyString& str) const {
return strcmp(m_pstr, str.m_pstr) >= 0;
}
boool 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;
}
代码说明
-
构造函数、拷贝构造函数和析构函数:
- 构造函数用于初始化
MyString对象,包括默认构造函数和使用C字符串初始化的构造函数。 - 拷贝构造函数用于创建一个已有
MyString对象的副本,实现深拷贝,避免内存泄漏和重复释放。 - 析构函数用于释放
MyString对象占用的内存空间。
- 构造函数用于初始化
-
赋值函数:
- 使用了拷贝-交换语义,避免了重复代码,并保证了异常安全性。
-
内存管理:
- 在构造函数和赋值函数中,使用
new动态分配内存,并在析构函数中使用delete[]释放内存,避免了内存泄漏。
- 在构造函数和赋值函数中,使用
-
const关键字的使用:
size()和c_str()函数声明为const成员函数,因为它们不应该修改成员变量。- 参数传递使用
const引用,避免不必要的拷贝,提高效率。
总结
本文介绍了如何使用char*实现一个自定义的C++字符串类MyString,并详细解释了内存管理和const关键字的使用。通过自定义字符串类,我们可以更好地理解字符串的底层实现机制,并根据实际需求进行优化。
原文地址: http://www.cveoy.top/t/topic/jvDV 著作权归作者所有。请勿转载和采集!