C++-string类的模拟实现
发布人:shili8
发布时间:2025-01-07 12:49
阅读次数:0
**C++ string 类的模拟实现**
在 C++ 中,`std::string` 是一个非常重要的类,它提供了对字符串的基本操作,如查找、替换、插入等。然而,在某些情况下,我们可能需要自己实现一个 `string` 类来满足特定的需求。在本文中,我们将模拟实现 C++ 的 `string` 类。
###1. 概述我们的 `String` 类将提供以下功能:
* 构造函数:支持多种构造方式,如从 `const char*`、`char`、`std::string` 等创建。
* 赋值运算符:支持赋值给其他 `String` 对象或 `const char*`。
* 常用操作函数:
* `size()`:返回字符串长度。
* `empty()`:判断是否为空。
* `find()`:查找子串。
* `replace()`:替换子串。
* `insert()`:插入子串。
* 其他功能,如 `substr()、erase()` 等。
###2. 构造函数
cpp// 构造函数模拟实现String::String(const char* str) {
if (str == nullptr) {
// 处理空指针 len =0;
data = new char[1];
*data = '0';
} else {
len = strlen(str);
data = new char[len +1];
strcpy(data, str);
}
}
String::String(char c) {
len =1;
data = new char[2];
data[0] = c;
data[1] = '0';
}
String::String(const String& other) {
len = other.len;
data = new char[len +1];
strcpy(data, other.data);
}
###3. 赋值运算符
cpp// 赋值运算符模拟实现String& String::operator=(const String& other) {
if (this != &other) {
delete[] data;
len = other.len;
data = new char[len +1];
strcpy(data, other.data);
}
return *this;
}
String& String::operator=(const char* str) {
if (str == nullptr) {
// 处理空指针 delete[] data;
len =0;
data = new char[1];
*data = '0';
} else {
delete[] data;
len = strlen(str);
data = new char[len +1];
strcpy(data, str);
}
return *this;
}
###4. 常用操作函数
cpp// size() 模拟实现size_t String::size() const {
return len;
}
// empty() 模拟实现bool String::empty() const {
return len ==0;
}
// find() 模拟实现size_t String::find(const char* str) const {
if (str == nullptr || strlen(str) > len) {
return -1; // 未找到或超出长度 }
int i =0;
while (i <= len - strlen(str)) {
bool match = true;
for (int j =0; j < strlen(str); ++j) {
if (data[i + j] != str[j]) {
match = false;
break;
}
}
if (match) return i;
++i;
}
return -1; // 未找到}
// replace() 模拟实现String& String::replace(const char* oldStr, const char* newStr) {
size_t oldLen = strlen(oldStr);
size_t newLen = strlen(newStr);
if (oldLen > len || newLen >= len - oldLen +1) {
// 处理长度不符或新字符串过长 delete[] data;
len -= oldLen;
strcpy(data, this->data);
strcat(data, newStr);
return *this;
}
int i = find(oldStr);
if (i == -1) return *this; // 未找到 size_t j =0;
while (j < len && j <= i + oldLen) {
data[j] = this->data[j];
++j;
}
for (; j < len; ++j) {
if (i <= j - newLen) {
data[j - newLen] = newStr[j - i];
} else {
data[j] = this->data[j];
}
}
return *this;
}
// insert() 模拟实现String& String::insert(size_t pos, const char* str) {
size_t oldLen = strlen(str);
if (pos > len || pos + oldLen >= len) {
// 处理位置不符或新字符串过长 delete[] data;
len += oldLen;
strcpy(data, this->data);
strcat(data, str);
return *this;
}
int i =0;
while (i < pos) {
data[i + oldLen] = this->data[i];
++i;
}
for (; i <= pos; ++i) {
data[i] = str[i - pos];
}
len += oldLen;
return *this;
}
###5. 其他功能
cpp// substr() 模拟实现String String::substr(size_t pos, size_t count) const {
if (pos >= len || count > len - pos) {
// 处理位置不符或长度不符 return String();
}
int i =0;
while (i < pos) {
data[i] = this->data[i];
++i;
}
for (; i < pos + count; ++i) {
data[i - pos] = this->data[i];
}
len = count;
return *this;
}
// erase() 模拟实现String& String::erase(size_t pos, size_t count) {
if (pos >= len || count > len - pos) {
// 处理位置不符或长度不符 delete[] data;
len -= count;
strcpy(data, this->data + pos);
return *this;
}
int i =0;
while (i < pos) {
data[i] = this->data[i];
++i;
}
for (; i <= pos + count; ++i) {
data[i - count] = this->data[i];
}
len -= count;
return *this;
}
以上就是 C++ string 类的模拟实现。这个类提供了基本的字符串操作功能,如查找、替换、插入等。

