当前位置: 首页 > news >正文

c++中mystring运算符重载

#include <iostream>
#include <cstring>using namespace std;class mystring
{char* buf;
public:mystring(); //构造函数mystring(const char * str); //构造函数mystring(const mystring& str); //深拷贝函数void show(); //输出函数void setmystr(const mystring& str); //设置函数const char* getmystr() const; //获取函数void append(const mystring& str); //追加函数int isEqual(const mystring& str); // 比较函数void swap(mystring& str);//交换函数~mystring(); //析构函数
/****************************************************************************/mystring& operator+(const mystring& str);//+mystring& operator=(const mystring& str);//=mystring& operator+=(const mystring& str);//+=bool operator==(const mystring& str);//==bool operator!=(const mystring& str);// !=mystring& operator++();//前++mystring operator++(int);//后++char operator[](const int index);//[]friend ostream& operator<<(ostream& out , const mystring& str);//cout << mystringfriend istream& operator>>(istream& in , const mystring& str);//cin >> mystring
};/****************************************************************************/
//+
mystring& mystring::operator+(const mystring& str)
{this->append(str);return *this;
}
//=
mystring& mystring::operator=(const mystring& str)
{int len = strlen(str.buf);buf = new char[len+1];strcpy(buf , str.buf);return *this;
}
//+=
mystring& mystring::operator+=(const mystring& str)
{this->append(str);return *this;
}
//==
bool mystring::operator==(const mystring& str)
{if (strcmp(this->buf , str.buf) == 0 )return true;elsereturn false;
}
// !=
bool mystring::operator!=(const mystring &str)
{if(strcmp(buf , str.buf) == 0)return false;elsereturn true;
}
//++
mystring& mystring::operator++()
{char * p = buf;while (*p != '\0') {++(*p);p++;}return *this;
}
//后++
mystring mystring::operator++(int)
{mystring temp = *this;char * p = buf;while (*p != '\0') {++(*p);p++;}return temp;
}
//[]
char mystring::operator[](const int index)
{int len = strlen(buf);if(index < len){char * p = buf;return *(p+index);}return '\0';
}
//cout << mystring
ostream& operator<<(ostream& out , const mystring& str)
{out << str.buf;return out;
}
//cin >> mystring
istream& operator>>(istream& in , const mystring& str)
{in >> str.buf;return in;
}/****************************************************************************/
//构造函数
mystring::mystring()
{buf = new char[1];
}
//构造函数
mystring::mystring(const char * str)
{int len = strlen(str);buf = new char[len+1];strcpy(buf , str);
}
//深拷贝函数
mystring::mystring(const mystring& str)
{int len = strlen(str.buf);buf = new char[len+1];strcpy(buf , str.buf);
}
//打印函数
void mystring::show()
{cout << buf << endl;
}
//设置函数
void mystring::setmystr(const mystring& str)
{free(this->buf);int len = strlen(str.buf);this->buf = new char[len+1];strcpy(this->buf , str.buf);
}
//获取函数
const char* mystring::getmystr() const
{return buf;
}
//追加函数
void mystring::append(const mystring& str)
{int len1 = strlen(this->buf);char* temp = new char[len1+1];memset(temp , 0 , len1+1);strcpy(temp , this->buf);delete[] buf;int len2 = strlen(str.buf);this->buf = new char[len1+len2+1];memset(buf , 0 , len1+len2+1);strcat(this->buf , temp);strcat(this->buf , str.buf);
}
//比较函数
int mystring::isEqual(const mystring& str)
{if(strcmp(buf , str.buf) > 0){return 1;}else if(strcmp(buf , str.buf) < 0){return -1;}elsereturn 0;
}
//交换函数
void mystring::swap(mystring& str)
{char * temp = this->buf;this->buf = str.buf;str.buf = temp;
}
//析构函数
mystring::~mystring()
{delete[] buf;
}int main()
{mystring ptr;mystring str = "hello world";str.show();ptr.setmystr("hello kitty");cout << ptr.getmystr() << endl;mystring qtr;qtr = ptr + str;qtr.show();/*	ptr += str;ptr.show();  	*//*	if(ptr == str){}else{cout << "== success" << endl;} 				*//*	if(ptr != str){cout << "!= success" << endl;}  				*//*	mystring temp = str++;temp.show();str.show(); 	*///	cout << str[0] <<  str[3] << str[8] << endl;mystring ttr;cout << "请输入:";cin >> ttr;cout << ttr;cout << endl;return 0;
}

相关文章:

c++中mystring运算符重载

#include <iostream> #include <cstring>using namespace std;class mystring {char* buf; public:mystring(); //构造函数mystring(const char * str); //构造函数mystring(const mystring& str); //深拷贝函数void show(); //输出函数void setmystr(const my…...

图像处理 - 色彩空间转换

色彩空间转换的含义与原理 色彩空间转换是指将一种颜色模型或表示方式中的颜色数据映射到另一种颜色模型中的过程。色彩空间&#xff08;Color Space&#xff09;本质上是一个三维坐标系统&#xff0c;每个点都表示图像中的某种颜色。在实际应用中&#xff0c;由于不同的色彩空…...

MariaDB面试题及参考答案

什么是 MariaDB&#xff1f; MariaDB 是一个开源的关系型数据库管理系统&#xff0c;它是 MySQL 数据库的一个分支。它的主要目的是存储和管理数据&#xff0c;采用了关系模型&#xff0c;数据存储在表中&#xff0c;表之间可以通过关联建立关系。 从起源来讲&#xff0c;Maria…...

PostgreSQL常用字符串函数与示例说明

文章目录 coalesce字符串位置(position strpos)字符串长度与大小写转换去掉空格(trim ltrim rtrim)字符串连接(concat)字符串替换简单替换(replace)替换指定位置长度(overlay)正则替换(regexp_replace) 字符串匹配字符串拆分split_part(拆分数组取指定位置的值)string_to_array…...

力扣第58题:最后一个单词的长度

力扣第58题是 最后一个单词的长度&#xff0c;具体要求是给定一个字符串&#xff0c;找到其最后一个单词的长度。 题目描述 输入&#xff1a;一个由字母和空格组成的字符串 s&#xff0c;可以包含大小写字母和若干空格。 输出&#xff1a;最后一个单词的长度。 注意&#xf…...

【Maven】Nexus几个仓库的介绍

在 Nexus 仓库管理器中&#xff0c;maven-central、maven-public、maven-releases 和 maven-snapshots 是常用的 Maven 仓库类型。每个仓库都有其特定的用途和功能。以下是对这些仓库的详细介绍&#xff1a; 1. maven-central 类型&#xff1a;代理仓库&#xff08;Proxy Rep…...

SSH免密登陆

一、生成SSH密钥对 在客户端主机 ClientHost上&#xff0c;以 root用户身份生成SSH密钥对&#xff1a; ssh-keygen -t rsa -b 4096 -C "your_emailexample.com" # -t rsa&#xff1a;指定使用RSA算法 # -b 4096&#xff1a;指定密钥长度为4096位 # -C ""…...

【Linux】Namespace

一、概念 Linux Namespace 是 Linux 内核提供的一种特性&#xff0c;用于对系统资源进行隔离。通过 Namespace&#xff0c;不同的进程组可以拥有独立的系统资源视图&#xff0c;即使它们在同一台物理机器上运行。这种隔离机制使得容器技术成为可能&#xff0c;因为它允许在单个…...

SQLite 和 MySQL语法区别

SQLite 和 MySQL 在 SQL 语法上有一些差异&#xff0c;这些差异主要体现在数据类型、函数、表和索引的管理等方面。以下是一些主要的不同之处&#xff1a; 1. 数据类型 SQLite 支持的数据类型包括&#xff1a;TEXT, INTEGER, REAL, BLOB。动态类型系统&#xff0c;允许在插入…...

基于BERT的命名体识别(NER)

基于BERT的命名实体识别&#xff08;NER&#xff09; 目录 项目背景项目结构环境准备数据准备代码实现 5.1 数据预处理 (src/preprocess.py)5.2 模型训练 (src/train.py)5.3 模型评估 (src/evaluate.py)5.4 模型推理 (src/inference.py) 项目运行 6.1 一键运行脚本 (run.sh)6…...

华为云鸿蒙应用入门级开发者认证考试题库(理论题和实验题)

注意&#xff1a;考试链接地址&#xff1a;华为云鸿蒙应用入门级学习认证_华为云鸿蒙应用入门级开发者认证_华为云开发者学堂-华为云 当前认证打折之后是1元&#xff0c;之后原价700元&#xff0c;大家尽快考试&#xff01;考试题库里面答案不一定全对&#xff0c;但是可以保证…...

SpringBoot+React养老院管理系统 附带详细运行指导视频

文章目录 一、项目演示二、项目介绍三、运行截图四、主要代码1.入住合同文件上传2.添加和修改套餐的代码3.查看入住记录代码 一、项目演示 项目演示地址&#xff1a; 视频地址 二、项目介绍 项目描述&#xff1a;这是一个基于SpringBootReact框架开发的养老院管理系统。首先…...

使用element-plus el-table中使用el-image层级冲突table表格会覆盖预览的图片等问题

在日常开发项目中 使用element-plus 中表格中使用 el-image的点击图片出现图片预览 会出现以下问题 表格一行会覆盖预览的图片 鼠标滑过也会显示表格 el-image 的预览层级和表格的层级冲突导致的。 解决方法&#xff1a;有两种一种是直接使用样式穿透 第二种推荐方法 使用官网推…...

python读取Oracle库并生成API返回Json格式

一、安装必要的库 首先&#xff0c;确保已经安装了以下库&#xff1a; 有网模式 pip install flask pip install gevent pi install cx_Oracle离线模式&#xff1a; 下载地址&#xff1a;https://pypi.org/simple/flask/ # a. Flask Werkzeug-1.0.1-py2.py3-none-any.whl J…...

音视频入门基础:MPEG2-TS专题(5)——FFmpeg源码中,判断某文件是否为TS文件的实现

一、引言 通过FFmpeg命令&#xff1a; ./ffmpeg -i XXX.ts 可以判断出某个文件是否为TS文件&#xff1a; 所以FFmpeg是怎样判断出某个文件是否为TS文件呢&#xff1f;它内部其实是通过mpegts_probe函数来判断的。从《FFmpeg源码&#xff1a;av_probe_input_format3函数和AVI…...

每天10个vue面试题(九)

1、如何在组件中批量使用Vuex的getter属性&#xff1f; 使用mapGetters辅助函数, 利用对象展开运算符将getter混入computed 对象中computed:{ ...mapGetters([total,discountTotal]) } 2、vue2和vue3的区别&#xff1f; 双向数据绑定不同&#xff1a;vue2 的双向数据绑定…...

Jenkins的环境部署

day22 回顾 Jenkins 简介 官网Jenkins Jenkins Build great things at any scale The leading open source automation server, Jenkins provides hundreds of plugins to support building, deploying and automating any project. 用来构建一切 其实就是用Java写的一个项目…...

八、鸿蒙开发-网络请求、应用级状态管理

提示&#xff1a;本文根据b站尚硅谷2024最新鸿蒙开发HarmonyOS4.0鸿蒙NEXT星河版零基础教程课整理 链接指引 > 尚硅谷2024最新鸿蒙开发HarmonyOS4.0鸿蒙NEXT星河版零基础教程 文章目录 一、网络请求1.1 申请网络访问权限1.2 安装axios库1.2.1 配置环境变量1.2.2 第二步&…...

经验笔记:Git 中的远程仓库链接及上下游关系管理

Git 中的远程仓库链接及上下游关系管理 1. 远程仓库的链接信息 当你克隆一个远程仓库时&#xff0c;Git 会在本地仓库中记录远程仓库的信息。这些信息包括远程仓库的 URL、默认的远程名称&#xff08;通常是 origin&#xff09;&#xff0c;以及远程仓库中的所有分支和标签。…...

Paint 学习笔记

目录 ippaint 外扩对象 LCM_inpaint_Outpaint_Comfy&#xff1a; 不支持文字引导 ippaint https://github.com/Sanster/IOPaint 外扩对象 https://www.iopaint.com/models/diffusion/powerpaint_v2 GitHub - open-mmlab/PowerPaint: [ECCV 2024] PowerPaint, a versatile …...

Ubuntu系统下交叉编译openssl

一、参考资料 OpenSSL&&libcurl库的交叉编译 - hesetone - 博客园 二、准备工作 1. 编译环境 宿主机&#xff1a;Ubuntu 20.04.6 LTSHost&#xff1a;ARM32位交叉编译器&#xff1a;arm-linux-gnueabihf-gcc-11.1.0 2. 设置交叉编译工具链 在交叉编译之前&#x…...

SciencePlots——绘制论文中的图片

文章目录 安装一、风格二、1 资源 安装 # 安装最新版 pip install githttps://github.com/garrettj403/SciencePlots.git# 安装稳定版 pip install SciencePlots一、风格 简单好用的深度学习论文绘图专用工具包–Science Plot 二、 1 资源 论文绘图神器来了&#xff1a;一行…...

练习(含atoi的模拟实现,自定义类型等练习)

一、结构体大小的计算及位段 &#xff08;结构体大小计算及位段 详解请看&#xff1a;自定义类型&#xff1a;结构体进阶-CSDN博客&#xff09; 1.在32位系统环境&#xff0c;编译选项为4字节对齐&#xff0c;那么sizeof(A)和sizeof(B)是多少&#xff1f; #pragma pack(4)st…...

中南大学无人机智能体的全面评估!BEDI:用于评估无人机上具身智能体的综合性基准测试

作者&#xff1a;Mingning Guo, Mengwei Wu, Jiarun He, Shaoxian Li, Haifeng Li, Chao Tao单位&#xff1a;中南大学地球科学与信息物理学院论文标题&#xff1a;BEDI: A Comprehensive Benchmark for Evaluating Embodied Agents on UAVs论文链接&#xff1a;https://arxiv.…...

ssc377d修改flash分区大小

1、flash的分区默认分配16M、 / # df -h Filesystem Size Used Available Use% Mounted on /dev/root 1.9M 1.9M 0 100% / /dev/mtdblock4 3.0M...

将对透视变换后的图像使用Otsu进行阈值化,来分离黑色和白色像素。这句话中的Otsu是什么意思?

Otsu 是一种自动阈值化方法&#xff0c;用于将图像分割为前景和背景。它通过最小化图像的类内方差或等价地最大化类间方差来选择最佳阈值。这种方法特别适用于图像的二值化处理&#xff0c;能够自动确定一个阈值&#xff0c;将图像中的像素分为黑色和白色两类。 Otsu 方法的原…...

spring:实例工厂方法获取bean

spring处理使用静态工厂方法获取bean实例&#xff0c;也可以通过实例工厂方法获取bean实例。 实例工厂方法步骤如下&#xff1a; 定义实例工厂类&#xff08;Java代码&#xff09;&#xff0c;定义实例工厂&#xff08;xml&#xff09;&#xff0c;定义调用实例工厂&#xff…...

ABAP设计模式之---“简单设计原则(Simple Design)”

“Simple Design”&#xff08;简单设计&#xff09;是软件开发中的一个重要理念&#xff0c;倡导以最简单的方式实现软件功能&#xff0c;以确保代码清晰易懂、易维护&#xff0c;并在项目需求变化时能够快速适应。 其核心目标是避免复杂和过度设计&#xff0c;遵循“让事情保…...

CVE-2020-17519源码分析与漏洞复现(Flink 任意文件读取)

漏洞概览 漏洞名称&#xff1a;Apache Flink REST API 任意文件读取漏洞CVE编号&#xff1a;CVE-2020-17519CVSS评分&#xff1a;7.5影响版本&#xff1a;Apache Flink 1.11.0、1.11.1、1.11.2修复版本&#xff1a;≥ 1.11.3 或 ≥ 1.12.0漏洞类型&#xff1a;路径遍历&#x…...

Web中间件--tomcat学习

Web中间件–tomcat Java虚拟机详解 什么是JAVA虚拟机 Java虚拟机是一个抽象的计算机&#xff0c;它可以执行Java字节码。Java虚拟机是Java平台的一部分&#xff0c;Java平台由Java语言、Java API和Java虚拟机组成。Java虚拟机的主要作用是将Java字节码转换为机器代码&#x…...