鱼C论坛

 找回密码
 立即注册
查看: 474|回复: 2

使用C++实现一个简易的string类

[复制链接]
发表于 2024-10-10 19:14:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
bug如下:
    // 为什么无法显示str6?
    str6 = str5 + "hello";
    std::cout << str6 << std::endl;

字符串类.zip

20.76 KB, 下载次数: 1

售价: 50 鱼币  [记录]  [购买]

C++的源文件和头文件

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-10-10 19:19:30 | 显示全部楼层
要实现一个简易的 `string` 类并支持字符串相加,需要重载运算符。你提到的问题可能是由于字符串连接操作没有正确实现导致的。以下是一个基本的 `String` 类实现,支持简单的字符串相加操作。

  1. #include <iostream>
  2. #include <cstring>

  3. class String {
  4. private:
  5.     char* data;
  6.     size_t length;

  7. public:
  8.     // 默认构造函数
  9.     String() : data(nullptr), length(0) {}

  10.     // 构造函数
  11.     String(const char* s) {
  12.         if (s) {
  13.             length = std::strlen(s);
  14.             data = new char[length + 1];
  15.             std::strcpy(data, s);
  16.         } else {
  17.             length = 0;
  18.             data = nullptr;
  19.         }
  20.     }

  21.     // 拷贝构造函数
  22.     String(const String& other) {
  23.         length = other.length;
  24.         if (length > 0) {
  25.             data = new char[length + 1];
  26.             std::strcpy(data, other.data);
  27.         } else {
  28.             data = nullptr;
  29.         }
  30.     }

  31.     // 移动构造函数
  32.     String(String&& other) noexcept : data(other.data), length(other.length) {
  33.         other.data = nullptr;
  34.         other.length = 0;
  35.     }

  36.     // 析构函数
  37.     ~String() {
  38.         delete[] data;
  39.     }

  40.     // 赋值运算符
  41.     String& operator=(const String& other) {
  42.         if (this != &other) {
  43.             delete[] data;

  44.             length = other.length;
  45.             if (length > 0) {
  46.                 data = new char[length + 1];
  47.                 std::strcpy(data, other.data);
  48.             } else {
  49.                 data = nullptr;
  50.             }
  51.         }
  52.         return *this;
  53.     }

  54.     // 移动赋值运算符
  55.     String& operator=(String&& other) noexcept {
  56.         if (this != &other) {
  57.             delete[] data;

  58.             data = other.data;
  59.             length = other.length;

  60.             other.data = nullptr;
  61.             other.length = 0;
  62.         }
  63.         return *this;
  64.     }

  65.     // 重载加法运算符用于字符串连接
  66.     String operator+(const char* s) const {
  67.         size_t newLength = length + std::strlen(s);
  68.         char* newData = new char[newLength + 1];

  69.         std::strcpy(newData, data);
  70.         std::strcat(newData, s);

  71.         String newString(newData);
  72.         delete[] newData;
  73.         
  74.         return newString;
  75.     }

  76.     // 重载输出运算符
  77.     friend std::ostream& operator<<(std::ostream& os, const String& str) {
  78.         if (str.data) {
  79.             os << str.data;
  80.         }
  81.         return os;
  82.     }
  83. };

  84. int main() {
  85.     String str5("This is ");
  86.     String str6;
  87.     str6 = str5 + "hello";
  88.     std::cout << str6 << std::endl;  // 应该输出 "This is hello"

  89.     return 0;
  90. }
复制代码


这个示例代码定义了一个 `String` 类,其中支持:
- 字符数组到 `String` 的构造。
- 重载了`+`运算符用于将字符串与字符数组拼接。
- 使用移动语义提高性能。
- 重载了输出运算符 `<<` 用于打印字符串。

在使用中,确保 `str5` 和 `"hello"` 是可连接的字符串,调用 `str5 + "hello"` 后返回一个新 `String` 对象,再赋值给 `str6`。这样就能正确显示字符串。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-10-10 20:32:49 | 显示全部楼层
本帖最后由 人造人 于 2024-10-10 20:34 编辑

str.zip (84.88 KB, 下载次数: 0)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-10 07:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表