用C++自定义String类

一、头文件

#pragma once
#include <iostream>/*** 自定义String类*/
class String 
{
public:String();String(const char *pstr);String(const String &str);~String();String &operator= (const String &str);String &operator= (const char *pstr);String &operator+= (const String &str);String &operator+= (const char *pstr);char &operator[] (std::size_t index);const char &operator[] (std::size_t index) const;std::size_t size() const;const char* c_str() const;friend bool operator== (const String &leftStr, const String &rightStr);friend bool operator!= (const String &leftStr, const String &rightStr);friend bool operator< (const String &leftStr, const String &rightStr);friend bool operator> (const String &leftStr, const String &rightStr);friend bool operator<= (const String &leftStr, const String &rightStr);friend bool operator>= (const String &leftStr, const String &rightStr);friend std::ostream &operator<< (std::ostream &os, const String &str);friend std::istream &operator>> (std::istream &is, String &str);private:char * _pstr;
};String operator+(const String &thisStr, const String &otherStr);
String operator+(const String &thisStr, const char *otherpStr);
String operator+(const char *thisPstr, const String &otherStr);

二、String实现

#include <cstring>
#include "08_MyString.hh"using namespace std;/*** 无参构造函数
*/
String::String()
: _pstr(new char[1]{0}) 
{strcpy(_pstr, "");	
}/*** 有参构造函数
*/
String::String(const char *pstr) 
: _pstr(new char[strlen(pstr) + 1]{0})
{strcpy(_pstr, pstr);
}/*** 拷贝构造函数
*/
String::String(const String &str)
: _pstr(new char[strlen(str._pstr) + 1]{0})
{strcpy(_pstr, str._pstr);
}/*** 析构函数
*/
String::~String() {delete [] _pstr;
}/*** 重载赋值运算符函数
*/
String &String::operator=(const String &str) {if (this != &str) { // 跳过自赋值delete [] _pstr;_pstr = new char[strlen(str._pstr) + 1]{0};strcpy(_pstr, str._pstr);} return *this; 
}
String &String::operator=(const char *pstr) {delete [] _pstr;_pstr = new char[strlen(pstr) + 1]{0};strcpy(_pstr, pstr);return *this;
}/*** 重载+=
*/
String &String::operator+= (const String &str) {size_t newLen = strlen(_pstr) + strlen(str._pstr) + 1;char *newStr = new char[newLen]{0};strcat(newStr, _pstr);strcat(newStr, str._pstr);delete [] _pstr;_pstr = newStr;return *this;
}
String &String::operator+= (const char *pstr) {size_t newLen = strlen(_pstr) + strlen(pstr) + 1;char *newStr = new char[newLen]{0};strcat(newStr, _pstr);strcat(newStr, pstr);delete [] _pstr;_pstr = newStr;return *this;
}/*** 重载下标运算符
*/
char &String::operator[] (size_t index) {if (index < strlen(_pstr)) { // 未越界return _pstr[index];}else {static char nullchar = '\0';return nullchar;}	
}
const char &String::operator[] (size_t index) const {if (index < strlen(_pstr)) { // 未越界return _pstr[index];}else {static char nullchar = '\0';return nullchar;}	
}/*** 获取字符串长度
*/
size_t String::size() const {return strlen(_pstr);
}/*** 返回C风格字符串
*/
const char *String::c_str() const {return _pstr;
}/*** 重载==
*/
bool operator==(const String &leftStr, const String &rightStr) {return strcmp(leftStr._pstr, rightStr._pstr) == 0 ? true : false;
}
/*** 重载!=
*/
bool operator!=(const String &leftStr, const String &rightStr) {return strcmp(leftStr._pstr, rightStr._pstr) != 0 ? true : false;
}
/*** 重载<
*/
bool operator<(const String &leftStr, const String &rightStr) {return strcmp(leftStr._pstr, rightStr._pstr) < 0 ? true : false;
}
/*** 重载>
*/
bool operator>(const String &leftStr, const String &rightStr) {return strcmp(leftStr._pstr, rightStr._pstr) > 0 ? true : false;
}
/*** 重载<=
*/
bool operator<=(const String &leftStr, const String &rightStr) {int res = strcmp(leftStr._pstr, rightStr._pstr); return res > 0 ? false : true;
}
/*** 重载>=
*/
bool operator>=(const String &leftStr, const String &rightStr) {int res = strcmp(leftStr._pstr, rightStr._pstr); return res < 0 ? false : true;
}/*** 重载输出流函数
*/
ostream &operator<< (ostream &os, const String &str) {os << str._pstr;return os;
}/*** 重载输入流函数
*/
istream &operator>> (istream &is, String &str) {char buf[4096];is.getline(buf, sizeof(buf));delete [] str._pstr;str._pstr = new char[strlen(buf) + 1];strcpy(str._pstr, buf);return is;
}/*** 重载+
*/
String operator+(const String &thisStr, const String &otherStr) {String temp(thisStr.c_str());temp+=otherStr.c_str();return temp;
}
String operator+(const String &thisStr, const char *otherPstr) {String temp(thisStr.c_str());temp+=otherPstr;return temp;
}
String operator+(const char *thisPstr, const String &otherStr) {String temp(thisPstr);temp+=otherStr.c_str();return temp;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/746261.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java:继承

目录 1.继承1.1为什么要使用继承&#xff1f;1.2继承的概念1.3对继承的理解1.4子类怎么访问父类的成员变量1.4.1不同名怎么访问&#xff1f;1.4.2同名怎么访问&#xff1f;(关键字&#xff1a;super) 1.5子类中访问父类的成员方法1.5.1不同名怎么访问&#xff1f;1.5.2同名怎么…

Apache DolphinScheduler-3.2.0集群部署教程

集群部署方案(2 Master 3 Worker) Apache DolphinScheduler官网&#xff1a;https://dolphinscheduler.apache.org/zh-cnApache DolphinScheduler使用文档&#xff1a;https://dolphinscheduler.apache.org/zh-cn/docs/3.2.0截止2024-01-19&#xff0c;最新版本&#xff1a;3…

Linux - 安装 Jenkins(详细教程)

目录 前言一、简介二、安装前准备三、下载与安装四、配置镜像地址五、启动与关闭六、常用插件的安装 前言 虽然说网上有很多关于 Jenkins 安装的教程&#xff0c;但是大部分都不够详细&#xff0c;或者是需要搭配 docker 或者 k8s 等进行安装&#xff0c;对于新手小白而已&…

BUGKU-WEB shell

题目描述 题目截图如下&#xff1a; 描述&#xff1a; $poc "a#s#s#e#r#t";$poc_1 explode("#", $poc);$poc_2 $poc_1[0].$poc_1[1].$poc_1[2].$poc_1[3].$poc_1[4].$poc_1[5];$poc_2($_GET[s])进入场景看看&#xff1a;是一个空白的界面 解题思路 …

NCP1380BDR2G芯片中文资料规格书PDF数据手册引脚图图片参数功能价格

产品描述&#xff1a; NCP1380 是一款高性能器件&#xff0c;旨在为准谐振转换器供电。该控制器基于专属的谷锁闭系统&#xff0c;可以在功率负载变轻时进行切换并降低开关频率。这样将产生稳定的运行&#xff0c;即使在漏极-源极谷中总是触发的开关事件下也是如此。此系统可在…

安卓Java面试题 161- 170

161. 使用new Message()和obtainMessage两种方式得到Message对象有什么区别 ?我们在平常使用Handler sendMessage方法的时候都要传递Message参数进去,通常创建Message对象有两种方式,一种就是常用的通过构造函数的方式创建对象, 一种就是通过Handler的obtainMessage了,既然…

让数据在业务间高效流转,镜舟科技与NineData完成产品兼容互认

近日&#xff0c;镜舟科技与NineData完成产品兼容测试。在经过联合测试后&#xff0c;镜舟科技旗下产品与NineData云原生智能数据管理平台完全兼容&#xff0c;整体运行高效稳定。 镜舟科技致力于帮助中国企业构建卓越的数据分析系统&#xff0c;打造独具竞争力的“数据护城河”…

faust勒索病毒:最新变种[nicetomeetyou@onionmail.org].faust袭击了您的计算机?

导言&#xff1a; 在当今数字时代&#xff0c;网络安全已经成为每个人都需要关注的重要问题。然而&#xff0c;恶意软件的威胁却从未停止过。最近出现的[nicetomeetyouonionmail.org].faust勒索病毒再次提醒我们&#xff0c;安全意识的重要性绝不可忽视。本文将深入介绍.faust…

探索 Spring 框架:企业级应用开发的强大工具

CSDN-个人主页&#xff1a;17_Kevin-CSDN博客 收录专栏&#xff1a;《Java》 目录 一、引言 二、Spring 框架的历史 三、Spring 框架的核心模块 四、Spring 框架的优势 五、Spring 框架的应用场景 六、结论 一、引言 在当今数字化时代&#xff0c;企业级应用开发的需求日…

代码随想录刷题笔记 Day 51 | 单词拆分 No.139 | 多重背包理论基础

文章目录 Day 5101. 单词拆分&#xff08;No. 139&#xff09;<1> 题目<2> 笔记<3> 代码 02. 多重背包理论基础2.1 解题思路2.2 携带矿石资源&#xff08;卡码网No.56&#xff09;<1> 题目<2> 笔记<3> 代码 Day 51 01. 单词拆分&#xff…

python-0002-linux安装pycharm

下载软件包 下载地址&#xff1a;https://download.csdn.net/download/qq_41833259/88944791 安装 # 解压 tar -zxvf 你的软件包 # 进入软件解压后的路径&#xff0c;如解压到了/home/soft/pycharm cd /home/soft/pycharm cd bin # 执行启动命令 sh pycharm.sh # 等待软件启…

基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的手写数字和符号识别(深度学习训练+UI界面+训练数据集)

摘要&#xff1a;开发手写数字和符号识别对于智能交互系统具有关键作用。本篇博客详细介绍了如何运用深度学习构建一个手写数字和符号识别&#xff0c;并提供了完整的实现代码。该系统基于强大的YOLOv8算法&#xff0c;并对比了YOLOv7、YOLOv6、YOLOv5&#xff0c;展示了不同模…

STM32F103 CubeMX 定时器输出PWM控制呼吸灯

STM32F103 CubeMX 定时器输出PWM控制呼吸灯 1. 生成代码1. 1 配置外部时钟&#xff0c;SWD口1.2 配置定时器31.3 配置定时器2 2. 代码编写的部分 使用的cubmx 软件的版本&#xff1a;6.2.0 最新版本 6.10&#xff08;2024年3月11日&#xff09; 1. 生成代码 1. 1 配置外部时钟…

C++ 拷贝构造函数和运算符重载

目录 一. 拷贝构造函数 1. 引入 2. 拷贝构造的概念 3. 浅拷贝 4. 深拷贝 二. C运算符重载 1. 概念 2. 注意事项 3.举例 一. 拷贝构造函数 1. 引入 我们在创建对象时&#xff0c;能不能创建一个与原先对象一模一样的新对象呢&#xff1f;为了解决这个问题&#x…

通过Python pypdf库轻松拆分大型PDF文件

pypdf的历史 pypdf最早可以追溯到2005年开源发布,最早名称是"pyPdf",中间的P是大写的,是一个纯python库,这个库一直持续到2010年的pyPdf1.13最后一个版本! 开源其实是一件非常吃力不讨好的事情,在没有商业化的手段,以及没有额外费用的支持下,很难一直靠爱发…

高性能计算——汇编语言(终章)

汇编语言(终章) 文章目录 汇编语言(终章)机器码布局(Machine Code Layout)CPU前端(CPU Front-End)代码对齐指令缓存不均等分支(Unequal Branches)机器码布局(Machine Code Layout) 计算机工程师喜欢在将CPU的流水线划分为两部分:前端和后端,前段指的是指令从内存…

关于PolarDB粗浅认识

PolarDB简介 目前&#xff08;20240314&#xff09;&#xff0c;PolarDB有两个版本&#xff1a; PolarDB-PG PolarDB PostgreSQL 版&#xff08;PolarDB for PostgreSQL&#xff0c;简称“PolarDB-PG”&#xff09;是阿里云自主研发的云原生关系型数据库产品&#xff0c;100%…

主板维修一般多少钱?电脑主板常见维修方法交换法

修主板一般要多少钱&#xff1f; 下面就让我们一起来了解一下吧。 电脑主板维修价格根据损坏程度不同而不同 1、电容器最容易损坏。 如果只是更换电容的话&#xff0c;大约需要50元左右。 2、如果主板上的电路损坏&#xff0c;面积越大&#xff0c;价格就越贵&#xff0c;可…

css动画和js动画的区别?

CSS 动画和 JavaScript 动画是网页开发中常用的两种动画实现方式&#xff0c;它们各有特点和适用场景&#xff0c;下面是它们之间的一些主要区别&#xff1a; CSS 动画&#xff1a; 声明式&#xff1a;CSS 动画是一种声明式的动画实现方式&#xff0c;通过定义样式规则和过渡效…

一学就会 | ChatGPT提示词-[简历指令库]-有爱AI实战教程(七)

演示站点&#xff1a; https://ai.uaai.cn 对话模块 官方论坛&#xff1a; www.jingyuai.com 京娱AI 一、导读&#xff1a; 在使用 ChatGPT 时&#xff0c;当你给的指令越精确&#xff0c;它的回答会越到位&#xff0c;举例来说&#xff0c;假如你要请它帮忙写文案&#x…