请设计实现自己的字符串类(不要直接用系统 的string) 自行设计补充 类中列出的方法的返回值,参数等自行设计实 现。

时间: 2023-05-28 21:04:46 浏览: 51
以下是一个简单的字符串类的设计: ```c++ class MyString { private: char* str; // 字符串的字符数组 int len; // 字符串的长度 public: MyString(); // 默认构造函数 MyString(const char* s); // 带参数的构造函数 MyString(const MyString& other); // 拷贝构造函数 ~MyString(); // 析构函数 MyString& operator=(const MyString& other); // 赋值运算符 MyString operator+(const MyString& other) const; // 加法运算符 bool operator==(const MyString& other) const; // 等于运算符 bool operator!=(const MyString& other) const; // 不等于运算符 char& operator[](int index); // 下标运算符,返回引用 const char& operator[](int index) const; // 下标运算符,返回常量引用 int length() const; // 返回字符串长度 bool empty() const; // 判断字符串是否为空 void clear(); // 清空字符串 void append(const char* s); // 在字符串末尾添加一个字符串 void insert(int pos, const char* s); // 在指定位置插入一个字符串 void erase(int pos, int len); // 删除指定位置的字符子串 const char* c_str() const; // 返回C风格的字符串指针 }; ``` 其中,构造函数、拷贝构造函数、析构函数、赋值运算符和加法运算符的实现如下: ```c++ MyString::MyString() { str = new char[1]; str[0] = '\0'; len = 0; } MyString::MyString(const char* s) { len = strlen(s); str = new char[len+1]; strcpy(str, s); } MyString::MyString(const MyString& other) { len = other.len; str = new char[len+1]; strcpy(str, other.str); } MyString::~MyString() { delete[] str; } MyString& MyString::operator=(const MyString& other) { if (this != &other) { delete[] str; len = other.len; str = new char[len+1]; strcpy(str, other.str); } return *this; } MyString MyString::operator+(const MyString& other) const { MyString newStr; newStr.len = len + other.len; newStr.str = new char[newStr.len+1]; strcpy(newStr.str, str); strcat(newStr.str, other.str); return newStr; } ``` 等于运算符和不等于运算符的实现如下: ```c++ bool MyString::operator==(const MyString& other) const { return strcmp(str, other.str) == 0; } bool MyString::operator!=(const MyString& other) const { return !(*this == other); } ``` 下标运算符的实现如下: ```c++ char& MyString::operator[](int index) { return str[index]; } const char& MyString::operator[](int index) const { return str[index]; } ``` 字符串长度和是否为空的实现如下: ```c++ int MyString::length() const { return len; } bool MyString::empty() const { return len == 0; } ``` 清空字符串的实现如下: ```c++ void MyString::clear() { delete[] str; str = new char[1]; str[0] = '\0'; len = 0; } ``` 在字符串末尾添加一个字符串的实现如下: ```c++ void MyString::append(const char* s) { int newLen = len + strlen(s); char* newStr = new char[newLen+1]; strcpy(newStr, str); strcat(newStr, s); delete[] str; str = newStr; len = newLen; } ``` 在指定位置插入一个字符串的实现如下: ```c++ void MyString::insert(int pos, const char* s) { int newLen = len + strlen(s); char* newStr = new char[newLen+1]; strncpy(newStr, str, pos); newStr[pos] = '\0'; strcat(newStr, s); strcat(newStr, str+pos); delete[] str; str = newStr; len = newLen; } ``` 删除指定位置的字符子串的实现如下: ```c++ void MyString::erase(int pos, int len) { int newLen = this->len - len; char* newStr = new char[newLen+1]; strncpy(newStr, str, pos); newStr[pos] = '\0'; strcat(newStr, str+pos+len); delete[] str; str = newStr; this->len = newLen; } ``` 返回C风格的字符串指针的实现如下: ```c++ const char* MyString::c_str() const { return str; } ```

相关推荐

最新推荐

recommend-type

Java面试笔试资料大全

19、我们在web应用开发过程中经常遇到输出某种编码的字符,如iso8859-1等,如何输出一个某种编码的字符串? 90 20.现在输入n个数字,以逗号,分开;然后可选择升或者降序排序;按提交键就在另一页面显示按什么排序...
recommend-type

java基础题 很全面

2. 我们在web应用开发过程中经常遇到输出某种编码的字符,如iso8859-1等,如何输出一个某种编码的字符串? 30 3. 请问输出什么,为什么? 30 4. 编程题: 写一个Singleton出来。 31 5. 设计4个线程,其中两个线程每次对j...
recommend-type

十天学会ASP.net--我认为ASP.NET比ASP难很多,希望大家做好准备

我要申明一点,操作系统就用2000,如果说要学习一个网络语言还不肯多装一个操作系统的话,那我看也不要学习了。 先到网上下载一个MDAC(要是2.7以上的版本才行),然后安装,安装以后或许会重新启动一次。 然后再...
recommend-type

六首页数字藏品NFT交易网React NextJS网站模板 六首页数字藏品nft交易网反应NextJS网站模板

六首页数字藏品NFT交易网React NextJS网站模板 六首页数字藏品nft交易网反应NextJS网站模板
recommend-type

wireshark安装教程入门

wireshark安装教程入门
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。