C++(9.24)

 

 头文件

#ifndef MY_STRING_H
#define MY_STRING_H#include <iostream>class My_string
{
private:char *ptr; // 指向字符数组的指针int size;  // 字符串的最大容量int len;   // 字符串的当前长度public:My_string();My_string(const char *src);My_string(const My_string &src);My_string& operator=(const My_string &src);~My_string();void push_back(char value);void pop_back();char &at(int index);void clear();char *data() const;int get_length() const;int get_size() const;bool is_empty() const;// 运算符重载My_string operator+(const My_string &other) const;My_string operator+(char value) const;char &operator[](int index);bool operator>(const My_string &other) const;bool operator<(const My_string &other) const;bool operator==(const My_string &other) const;bool operator>=(const My_string &other) const;bool operator<=(const My_string &other) const;bool operator!=(const My_string &other) const;My_string& operator+=(const My_string &other);My_string& operator+=(char value);friend std::ostream& operator<<(std::ostream &out, const My_string &str);friend std::istream& operator>>(std::istream &in, My_string &str);
};#endif // MY_STRING_H

 源文件

#include "My_string.h"
#include <iostream>
#include <cstring>
#include <stdexcept> // 用于异常处理using namespace std;// 默认构造函数
My_string::My_string() : size(15), len(0)
{this->ptr = new char[size];this->ptr[0] = '\0';
}// 带参数的构造函数
My_string::My_string(const char *src)
{this->len = strlen(src);this->size = this->len + 1;this->ptr = new char[size];strcpy(this->ptr, src);
}// 拷贝构造函数
My_string::My_string(const My_string &src)
{this->len = src.len;this->size = src.size;this->ptr = new char[size];strcpy(this->ptr, src.ptr);
}// 赋值运算符重载
My_string& My_string::operator=(const My_string &src)
{if (this == &src)return *this;delete[] this->ptr;this->len = src.len;this->size = src.size;this->ptr = new char[size];strcpy(this->ptr, src.ptr);return *this;
}// 尾部插入
void My_string::push_back(char value)
{if (len + 1 >= size){size *= 2;char *new_ptr = new char[size];strcpy(new_ptr, ptr);delete[] ptr;ptr = new_ptr;}ptr[len++] = value;ptr[len] = '\0';
}// 尾部删除
void My_string::pop_back()
{if (len > 0){len--;ptr[len] = '\0';}
}// 通过索引访问字符
char &My_string::at(int index)
{if (index < 0 || index >= len){throw out_of_range("Index out of range");}return ptr[index];
}// 清空字符串
void My_string::clear()
{len = 0;ptr[0] = '\0';
}// 返回 C 风格字符串
char *My_string::data() const
{return ptr;
}// 返回字符串长度
int My_string::get_length() const
{return len;
}// 返回最大容量
int My_string::get_size() const
{return size;
}// 判空函数
bool My_string::is_empty() const
{return len == 0;
}// 运算符重载实现
My_string My_string::operator+(const My_string &other) const
{My_string result;result.size = len + other.len; result.ptr = new char[result.size + 1];strcpy(result.ptr, ptr);        // 复制当前字符串strcat(result.ptr, other.ptr);  // 追加另一个字符串result.len = len + other.len;   // 更新长度return result;
}My_string My_string::operator+(char value) const
{My_string result(*this); // 拷贝当前对象result.push_back(value); // 添加字符return result;
}char &My_string::operator[](int index)
{return at(index); // 使用 at 函数检查边界
}bool My_string::operator>(const My_string &other) const
{return strcmp(ptr, other.ptr) > 0;
}bool My_string::operator<(const My_string &other) const
{return strcmp(ptr, other.ptr) < 0;
}bool My_string::operator==(const My_string &other) const
{return strcmp(ptr, other.ptr) == 0;
}bool My_string::operator>=(const My_string &other) const
{return *this >= other;
}bool My_string::operator<=(const My_string &other) const
{return this <= other;
}bool My_string::operator!=(const My_string &other) const
{return *this != other;
}My_string& My_string::operator+=(const My_string &other)
{this->push_back('\0'); // 确保字符串末尾有结束符strcat(this->ptr, other.ptr); len += other.len; // 更新长度return *this;
}My_string& My_string::operator+=(char value)
{push_back(value); // 尾部添加字符return *this;
}// 输入输出运算符重载
ostream& operator<<(ostream &out, const My_string &str)
{out << str.ptr;return out;
}istream& operator>>(istream &in, My_string &str)
{char buffer[1024]; in >> buffer;str.clear(); // 清空当前字符串str.push_back('\0'); // 先设置为结束符for (int i = 0; buffer[i] != '\0'; i++){str.push_back(buffer[i]); }return in;
}// 析构函数
My_string::~My_string()
{delete ptr;
}

 主函数

#include "My_string.h"
#include <iostream>using namespace std;int main()
{My_string s1("hello");My_string s2(" world");My_string s3 = s1 + s2; // 字符串连接cout << "s3: " << s3 << endl; // 输出 "hello world"s1 += '!';cout << "s1 after +=: " << s1 << endl; // 输出 "hello!"// 测试 []cout << "s1[1]: " << s1[1] << endl; // 输出 'e'cout << "s1 > s2: " << (s1 > s2) << endl; // 输出 1 cout << "s1 < s2: " << (s1 < s2) << endl; // 输出 0 cout << "s1 == s2: " << (s1 == s2) << endl; // 输出 0 My_string s4;cout << "Enter a string: ";cin >> s4; // 输入字符串cout << "You entered: " << s4 << endl;return 0;
}

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

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

Error:Decorators are not valid here. 使用Angular中的装饰器

Decorators are not valid here&#xff0c;项目中出现这个提示信息&#xff0c;说明装饰器未知错误、或者在不支持的元素上使用了装饰器。 如下图所示&#xff0c;我在NgModule装饰器后面加了一个导出方法&#xff0c;加完之后控制台提示了如下错误&#xff1a;Error TS1206&a…

【python】横截面数据分析及可视化报告示例

问题描述 题目&#xff1a; 共享的数据文件夹包含 2 个外部文件。第一个文件 "project data.xls "涉及国家层面的数据&#xff08;2000-2019 年&#xff09;&#xff0c;变量大多摘自世界发展指标。变量解释在第二个文件中 创建一个模型&#xff0c;解释为什么一些国…

STM32基础学习笔记-SPI通讯协议协议面试基础题8

第八章、SPI : LCD屏 1、LCD是什么 &#xff1f;发光原理 &#xff1f; 1.LCD:液晶显示 当有电流的时候&#xff0c;液晶层中的物质会有序排列 没有电流的时候&#xff0c;无序排列&#xff0c;光不可以透过 当像素点只有黑/白色的时候&#xff0c;灰度图&#xff08;非黑即…

windows C++-同步数据结构与 Windows API

将并发运行时提供的同步数据结构的行为与 Windows API 提供的同步数据结构的行为进行比较。 并发运行时提供的同步数据结构遵循协作线程模型。 在协作线程模型中&#xff0c;同步基元显式将其处理资源传递给其他线程。 这与抢占式线程模型不同&#xff0c;其中处理资源由控制调…

RtspServer:轻量级RTSP服务器和推流器

文章目录 项目概述技术分析支持的编码格式传输方式心跳检测机制RTSP 推流安全性 架构分析RtspServer 整体架构流程分析1. 客户端连接和会话建立2. 媒体数据传输3. 心跳检测和连接维护 xop 基础库项目介绍功能特性xop 整体架构 应用场景社区问题收集与解答问题一&#xff1a;刚开…

【YOLO学习】YOLOv1详解

文章目录 1. 概述2. 算法流程3. 网络结构4. 损失函数 1. 概述 1. YOLO 的全称是 You Only Look Once: Unified, Real-Time Object Detection。YOLOv1 的核心思想就是利用整张图作为网络的输入&#xff0c;直接在输出层回归 bounding box 的位置和 bounding box 所属的类别。简单…

【AI创作组】Matlab中进行符号计算

提示:代码一定要自己运行过才算数…… 1. 符号计算工具箱介绍 1.1 工具箱功能 MATLAB的符号计算工具箱,即Symbolic Math Toolbox,是一套强大的数学软件工具,它使得MATLAB具备了符号运算的能力。该工具箱提供了一系列函数,用于求解、绘制和操作符号数学方程。用户可以直接…

深度学习之表示学习 - 半监督解释因果关系篇

序言 在深度学习的广阔领域中&#xff0c;表示学习&#xff08; Representation Learning \text{Representation Learning} Representation Learning&#xff09;与半监督学习&#xff08; Semi-supervised Learning \text{Semi-supervised Learning} Semi-supervised Learnin…

C++:deque的底层原理

一、deque是是双端队列。 deque(双端队列)&#xff1a;是一种双开口的"连续"空间的数据结构&#xff0c;双开口的含义是&#xff1a;可以在头尾两端进行插入和删除操作&#xff0c;且时间复杂度为O(1)&#xff0c;与vector比较&#xff0c;头插效率高&#xff0c;不…

spring里面内置的非常实用的工具

一 、请求数据记录 Spring Boot提供了一个内置的日志记录解决方案&#xff0c;通过 AbstractRequestLoggingFilter 可以记录请求的详细信息。 AbstractRequestLoggingFilter 有两个不同的实现类&#xff0c;我们常用的是 CommonsRequestLoggingFilter。 通过 CommonsRequestL…

AlDente Pro for Mac电池健康保护工具

AlDente Pro for Mac 是一款适用于 Mac 的实用电池健康保护工具。以下是它的主要特点和优势&#xff1a; 软件下载地址 一、保护电池寿命的原理 锂离子和聚合物电池&#xff08;如 Mac 笔记本中的电池&#xff09;在 30% 到 80% 之间运行时使用寿命最长。始终将电池电量保持…

网关基础知识

1.网关路由 网关&#xff1a;就是网络的关口&#xff0c;负责请求的路由、转发、身份校验。 在SpringCloud中网关的实现包括两种&#xff1a; 1.Spring Cloud Gateway Spring官方出品 基于WebFlux响应式编程 无需调优即可获得优异性能 2.Netflix Zuul Netflix出品 基于Ser…

快递物流短信API接口代码

官网&#xff1a;快递鸟 API参数 用户信息类 一.短信模版 1.接口说明 使用快递鸟短信功能时&#xff0c;预先设置好短信模板和对应的发送规则&#xff0c;快递鸟短信API将根据设置的好的模板和规则&#xff0c;进行短信的发送和反馈。 (1)仅支持Json格式。 (2)请求指令810…

vulnhub(13):Digitalworld.local JOY(ftp 的未授权文件读写漏洞、文件覆盖提权)

端口 nmap主机发现 nmap -sn 192.168.72.0/24 ​ Nmap scan report for 192.168.72.171 Host is up (0.00020s latency). ​ 171是新出现的机器&#xff0c;他就是靶机 nmap端口扫描 nmap -Pn 192.168.72.171 -p- --min-rate 10000 -oA nmap/scan 扫描开放端口保存到 nmap/sca…

Python | Leetcode Python题解之第435题无重叠区间

题目&#xff1a; 题解&#xff1a; class Solution:def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:if not intervals:return 0intervals.sort(keylambda x: x[1])n len(intervals)right intervals[0][1]ans 1for i in range(1, n):if intervals…

网络资源模板--Android Studio 垃圾分类App

目录 一、项目演示 二、项目测试环境 三、项目详情 四、完整的项目源码 一、项目演示 网络资源模板--垃圾分类App 二、项目测试环境 三、项目详情 登陆注册 设置点击监听器&#xff1a;当用户点击注册按钮时触发事件。获取用户输入&#xff1a;从输入框获取用户名和密码&a…

springboot 接口接收及响应xml数据

1.实体类 import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement;XmlRootElement XmlAccessorType(XmlAccessType.FIELD) // …

【SpringBoot详细教程】-03-整合Junit【持续更新】

JUnit是一个用于Java编程语言的测试框架。它支持自动化单元测试&#xff0c;可以帮助开发人员测试代码的正确性和健壮性。JUnit提供了一组注解、断言和测试运行器&#xff0c;可以方便地编写和运行单元测试。 SpringBoot 整合 junit 特别简单&#xff0c;分为以下三步完成 在…

网络安全等级保护 | 规范企业网络系统安全使用 | 天锐股份助力等保制度落地

在当今数字化高速发展的时代&#xff0c;网络安全对于企业的重要性日益凸显。而近年来&#xff0c;数据泄露、网络攻击等安全事件频发&#xff0c;给企业和个人带来了前所未有的挑战。在这一背景下&#xff0c;网络安全等级保护制度&#xff08;简称“等保”&#xff09;作为国…

经颅磁刺激技术,脑科学研究——精神患者治疗方案

经颅磁刺激&#xff08;Transcranial Magnetic Stimulation &#xff0c;TMS&#xff09;技术是一种利用脉冲磁场作用于中枢神经系统&#xff08;主要是大脑&#xff09;&#xff0c;改变皮层神经细胞的膜电位&#xff0c;使之产生感应电流&#xff0c;影响脑内代谢和神经电活动…