当前位置: 首页 > 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 …...

浅谈 React Hooks

React Hooks 是 React 16.8 引入的一组 API&#xff0c;用于在函数组件中使用 state 和其他 React 特性&#xff08;例如生命周期方法、context 等&#xff09;。Hooks 通过简洁的函数接口&#xff0c;解决了状态与 UI 的高度解耦&#xff0c;通过函数式编程范式实现更灵活 Rea…...

云原生核心技术 (7/12): K8s 核心概念白话解读(上):Pod 和 Deployment 究竟是什么?

大家好&#xff0c;欢迎来到《云原生核心技术》系列的第七篇&#xff01; 在上一篇&#xff0c;我们成功地使用 Minikube 或 kind 在自己的电脑上搭建起了一个迷你但功能完备的 Kubernetes 集群。现在&#xff0c;我们就像一个拥有了一块崭新数字土地的农场主&#xff0c;是时…...

Qt/C++开发监控GB28181系统/取流协议/同时支持udp/tcp被动/tcp主动

一、前言说明 在2011版本的gb28181协议中&#xff0c;拉取视频流只要求udp方式&#xff0c;从2016开始要求新增支持tcp被动和tcp主动两种方式&#xff0c;udp理论上会丢包的&#xff0c;所以实际使用过程可能会出现画面花屏的情况&#xff0c;而tcp肯定不丢包&#xff0c;起码…...

最新SpringBoot+SpringCloud+Nacos微服务框架分享

文章目录 前言一、服务规划二、架构核心1.cloud的pom2.gateway的异常handler3.gateway的filter4、admin的pom5、admin的登录核心 三、code-helper分享总结 前言 最近有个活蛮赶的&#xff0c;根据Excel列的需求预估的工时直接打骨折&#xff0c;不要问我为什么&#xff0c;主要…...

质量体系的重要

质量体系是为确保产品、服务或过程质量满足规定要求&#xff0c;由相互关联的要素构成的有机整体。其核心内容可归纳为以下五个方面&#xff1a; &#x1f3db;️ 一、组织架构与职责 质量体系明确组织内各部门、岗位的职责与权限&#xff0c;形成层级清晰的管理网络&#xf…...

【SQL学习笔记1】增删改查+多表连接全解析(内附SQL免费在线练习工具)

可以使用Sqliteviz这个网站免费编写sql语句&#xff0c;它能够让用户直接在浏览器内练习SQL的语法&#xff0c;不需要安装任何软件。 链接如下&#xff1a; sqliteviz 注意&#xff1a; 在转写SQL语法时&#xff0c;关键字之间有一个特定的顺序&#xff0c;这个顺序会影响到…...

[论文阅读]TrustRAG: Enhancing Robustness and Trustworthiness in RAG

TrustRAG: Enhancing Robustness and Trustworthiness in RAG [2501.00879] TrustRAG: Enhancing Robustness and Trustworthiness in Retrieval-Augmented Generation 代码&#xff1a;HuichiZhou/TrustRAG: Code for "TrustRAG: Enhancing Robustness and Trustworthin…...

GraphQL 实战篇:Apollo Client 配置与缓存

GraphQL 实战篇&#xff1a;Apollo Client 配置与缓存 上一篇&#xff1a;GraphQL 入门篇&#xff1a;基础查询语法 依旧和上一篇的笔记一样&#xff0c;主实操&#xff0c;没啥过多的细节讲解&#xff0c;代码具体在&#xff1a; https://github.com/GoldenaArcher/graphql…...

PH热榜 | 2025-06-08

1. Thiings 标语&#xff1a;一套超过1900个免费AI生成的3D图标集合 介绍&#xff1a;Thiings是一个不断扩展的免费AI生成3D图标库&#xff0c;目前已有超过1900个图标。你可以按照主题浏览&#xff0c;生成自己的图标&#xff0c;或者下载整个图标集。所有图标都可以在个人或…...

Java并发编程实战 Day 11:并发设计模式

【Java并发编程实战 Day 11】并发设计模式 开篇 这是"Java并发编程实战"系列的第11天&#xff0c;今天我们聚焦于并发设计模式。并发设计模式是解决多线程环境下常见问题的经典解决方案&#xff0c;它们不仅提供了优雅的设计思路&#xff0c;还能显著提升系统的性能…...