C++ string类的模拟(详细代码实现)
发布人:shili8
发布时间:2024-12-29 09:30
阅读次数:0
**C++ String 类的模拟**
在 C++ 中,`std::string` 是一个非常重要的类,它提供了字符串操作的基本功能。然而,在某些情况下,我们可能需要自己实现一个 `String` 类来模拟 `std::string` 的行为。这篇文章将详细介绍如何实现一个 C++ String 类的模拟。
**1. 类定义**
首先,我们需要定义我们的 `String` 类:
cppclass String {
public:
// 构造函数 String();
String(const char* str);
String(const String& other);
// 赋值运算符 String& operator=(const String& other);
// 拷贝构造函数 String(const String&& other) noexcept;
// 移动赋值运算符 String& operator=(String&& other) noexcept;
// 析构函数 ~String();
// 获取长度 size_t length() const;
// 比较两个字符串 bool equals(const char* str) const;
bool equals(const String& other) const;
// 拼接两个字符串 String operator+(const char* str) const;
String operator+(const String& other) const;
// 获取子串 String substr(size_t pos, size_t len = -1) const;
// 转换为 C 风格字符串 const char* c_str() const;
private:
// 内存分配函数 void allocate(size_t capacity);
// 内存释放函数 void deallocate();
// 字符串缓冲区 char* buffer_;
size_t capacity_; // 缓冲区容量};
**2. 构造函数**
我们的 `String` 类有三个构造函数:一个无参构造函数、一个从 C 风格字符串构造函数和一个拷贝构造函数。
cpp// 无参构造函数String::String() : capacity_(0), buffer_(nullptr) {}
// 从 C 风格字符串构造函数String::String(const char* str) {
allocate(strlen(str));
strcpy(buffer_, str);
}
// 拷贝构造函数String::String(const String& other) : capacity_(other.capacity_), buffer_(new char[capacity_]) {
strcpy(buffer_, other.buffer_);
}
**3. 赋值运算符**
我们需要实现赋值运算符来支持 `=` 操作。
cpp// 赋值运算符String& String::operator=(const String& other) {
if (this != &other) { // 防止自赋值 deallocate();
capacity_ = other.capacity_;
buffer_ = new char[capacity_];
strcpy(buffer_, other.buffer_);
}
return *this;
}
**4. 移动赋值运算符**
我们还需要实现移动赋值运算符来支持 `=` 操作。
cpp// 移动赋值运算符String& String::operator=(String&& other) noexcept {
if (this != &other) { // 防止自赋值 deallocate();
capacity_ = other.capacity_;
buffer_ = other.buffer_;
other.buffer_ = nullptr;
other.capacity_ =0;
}
return *this;
}
**5. 析构函数**
我们的 `String` 类需要一个析构函数来释放内存。
cpp// 析构函数String::~String() {
deallocate();
}
**6. 获取长度**
我们可以实现一个 `length()` 函数来获取字符串的长度。
cpp// 获取长度size_t String::length() const {
return capacity_;
}
**7. 比较两个字符串**
我们可以实现一个 `equals()` 函数来比较两个字符串是否相等。
cpp// 比较两个字符串bool String::equals(const char* str) const {
return strcmp(buffer_, str) ==0;
}
// 比较两个String对象bool String::equals(const String& other) const {
return strcmp(buffer_, other.buffer_) ==0;
}
**8. 拼接两个字符串**
我们可以实现一个 `operator+()` 函数来拼接两个字符串。
cpp// 拼接两个字符串String String::operator+(const char* str) const {
size_t len = strlen(buffer_);
size_t new_len = len + strlen(str);
allocate(new_len);
strcat(buffer_, str);
return *this;
}
// 拼接两个String对象String String::operator+(const String& other) const {
size_t len = strlen(buffer_);
size_t new_len = len + other.length();
allocate(new_len);
strcat(buffer_, other.buffer_);
return *this;
}
**9. 获取子串**
我们可以实现一个 `substr()` 函数来获取子串。
cpp// 获取子串String String::substr(size_t pos, size_t len) const {
if (pos >= capacity_) {
return String();
}
size_t new_len = std::min(len, capacity_ - pos);
allocate(new_len);
memmove(buffer_, buffer_ + pos, new_len);
return *this;
}
**10. 转换为 C 风格字符串**
我们可以实现一个 `c_str()` 函数来转换为 C 风格字符串。
cpp// 转换为 C 风格字符串const char* String::c_str() const {
return buffer_;
}
以上就是我们的 `String` 类的模拟。这个类提供了基本的字符串操作功能,如构造函数、赋值运算符、析构函数等。我们还实现了一些额外的功能,如比较两个字符串、拼接两个字符串和获取子串等。
**示例代码**
cppint main() {
String str1 = "Hello";
String str2 = str1 + ", World!";
std::cout << str2.c_str() << std::endl; // 输出: Hello, World!
String str3 = str2.substr(0,5);
std::cout << str3.c_str() << std::endl; // 输出: Hello return0;
}
以上就是我们的示例代码。我们首先创建两个 `String` 对象 `str1` 和 `str2`,然后使用 `+` 操作符将它们拼接起来。接着,我们使用 `substr()` 函数获取子串,并输出结果。
**注释**
* 我们的 `String` 类提供了基本的字符串操作功能,如构造函数、赋值运算符、析构函数等。
* 我们还实现了一些额外的功能,如比较两个字符串、拼接两个字符串和获取子串等。
* 示例代码展示了如何使用我们的 `String` 类进行基本的字符串操作。
以上就是我们的 `String` 类的模拟。这个类提供了基本的字符串操作功能,并且可以用于各种场景中。

