C++学习9.24

ops/2024/10/19 3:31:59/

1、 将昨天的My_string类中的所有能重载的运算符全部进行重载

+、[] 、>、=、>)

头文件

#ifndef MY_STRING_H
#define MY_STRING_H#endif // MY_STRING_H#include <iostream>
#include <cstring>
//#include <stdexcept>using namespace std;class My_string
{
private:char *ptr;         //指向字符数组的指针int size;           //字符串的最大容量int len;            //字符串当前容量public://加法const My_string operator+(const My_string &R)const;const My_string operator+=(const My_string &R);char operator[](int index)const;friend ostream & operator<< (ostream &L,const My_string &R);friend istream & operator>> (istream &L,My_string &R);bool  operator>(My_string &R);bool  operator<(My_string &R);bool  operator==(My_string &R);bool  operator<=(My_string &R);bool  operator>=(My_string &R);bool  operator!=(My_string &R);//无参构造My_string();//有参构造My_string(const char* src);My_string(int num, char value);//拷贝构造My_string(const My_string &other);//拷贝赋值My_string& operator= (const My_string &other);//析构函数~My_string();//判空bool  empty();//尾插void push_back(char value);//尾删void pop_back();//at函数实现char &at(int index);//清空函数void clear();//返回C风格字符串char *data();//返回实际长度int get_length();//返回当前最大容量int get_size();
};

源文件

#include "My_string.h"
//加法
const My_string My_string::operator+(const My_string &R)const
{My_string result;result.len = len + R.len;result.size = size + R.size;result.ptr = new char[result.size];strcpy(result.ptr,ptr);strcat(result.ptr,R.ptr);return result;}
//加等
const My_string My_string::operator+=(const My_string &R)
{*this = *this + R;return *this;
}char My_string::operator[](int index)const
{if(index<0 || index>len){return -1;}return ptr[index];
}ostream & operator<< (ostream &L,const My_string &R)
{L<<R.ptr<<endl;return L;
}istream & operator>> (istream &L,My_string &R)
{char buff[1024];L >> buff;R.len = strlen(buff);R.size = R.len + 1;delete [] R.ptr;R.ptr = new char[R.size];strcpy(R.ptr,buff);return L;
}bool My_string:: operator>(My_string &R)
{return strcmp(ptr,R.ptr)>0;
}bool My_string:: operator<(My_string &R)
{return strcmp(ptr,R.ptr)<0;
}bool My_string:: operator==(My_string &R)
{return strcmp(ptr,R.ptr)==0;
}bool My_string:: operator<=(My_string &R)
{return strcmp(ptr,R.ptr)<=0;
}bool My_string:: operator>=(My_string &R)
{return strcmp(ptr,R.ptr)>=0;
}bool My_string:: operator!=(My_string &R)
{return strcmp(ptr,R.ptr)!=0;
}//无参构造
My_string::My_string():size(15)
{this->ptr = new char[size];this->ptr[0] = '\0';            //表示串为空串this->len = 0;
}//有参构造
My_string:: My_string(const char* src)
{len = strlen(src);size = len+1;ptr = new char[size];strcpy(ptr,src);
}
My_string:: My_string(int num, char value): size(num+1),len(num)
{ptr = new char[size];for(int i=0;i<num;i++){ptr[i] = value;}ptr[num] = '\0';
}
//拷贝构造
My_string:: My_string(const My_string &other):size(other.size),len(other.len)
{ptr = new char[size];strcpy(ptr,other.ptr);
}
//拷贝赋值
My_string & My_string ::operator =(const My_string &other)
{if(this ==&other){return *this;}delete [] ptr;size = other.size;len = other.len;ptr = new char[size];strcpy(ptr,other.ptr);return *this;
}
//析构函数
My_string:: ~My_string()
{delete [] ptr;
}
//判空
bool My_string:: empty()
{return len ==0;
}
//尾插
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;len++;ptr[len] = '\0';
}
//尾删
void My_string:: pop_back()
{if(len>0){len--;ptr[len]='\0';}
}
//at函数实现
char&My_string:: at(int index)
{if(index<0 ||index>=len){//throw  out_of_range("超出");}return ptr[index];
}
//清空函数
void My_string:: clear()
{len = 0;ptr[0] = '\0';
}
//返回C风格字符串
char*My_string:: data()
{return ptr;
}
//返回实际长度
int My_string:: get_length()
{return len;
}
//返回当前最大容量
int My_string:: get_size()
{return size;
}

2、 思维导图


http://www.ppmy.cn/ops/119929.html

相关文章

【Python报错已解决】IndexError: index 0 is out of bounds for axis 1 with size 0

&#x1f3ac; 鸽芷咕&#xff1a;个人主页 &#x1f525; 个人专栏: 《C干货基地》《粉丝福利》 ⛺️生活的理想&#xff0c;就是为了理想的生活! 专栏介绍 在软件开发和日常使用中&#xff0c;BUG是不可避免的。本专栏致力于为广大开发者和技术爱好者提供一个关于BUG解决的经…

分布式事务解决方案3阶段的优点

要深入探讨三阶段提交&#xff08;3PC&#xff09;与两阶段提交&#xff08;2PC&#xff09;的优点&#xff0c;并提供Java代码演示&#xff0c;我们需要先了解它们的基本原理和关键差异。然后&#xff0c;我会尝试解释3PC的优点&#xff0c;并以Java伪代码的形式提供实现的示例…

大数据毕业设计选题推荐-民族服饰数据分析系统-Python数据可视化-Hive-Hadoop-Spark

✨作者主页&#xff1a;IT研究室✨ 个人简介&#xff1a;曾从事计算机专业培训教学&#xff0c;擅长Java、Python、微信小程序、Golang、安卓Android等项目实战。接项目定制开发、代码讲解、答辩教学、文档编写、降重等。 ☑文末获取源码☑ 精彩专栏推荐⬇⬇⬇ Java项目 Python…

神点SAAS云财务系统/多账套/前后端全开源

>>>系统简述&#xff1a; 神点SAAS云财务软件开源版&#xff0c;包含账套、凭证字、科目、期初、币别、账簿、报表、凭证、结账等功能。 神点云财务系统&#xff0c;餐饮行业财务软件、微服务架构财务软件、开源云财务软件、Java全开源财务软件优选&#xff01; >…

Redis: Sentinel哨兵监控架构及环境搭建

概述 在主从模式下&#xff0c;我们通过从节点只读模式提高了系统的并发能力并发不断增加&#xff0c;只需要扩展从节点即可&#xff0c;只要主从服务器之间&#xff0c;网络连接正常主服务器就会将写入自己的数据同步更新给从服务器&#xff0c;从而保证主从服务器的数据相同…

【RocketMQ】从 文件/数据结构 视角理解RocketMQ原理

目录 1. NameServer 文件结构和数据结构2. Broker 文件结构和数据结构2.1 CommitLog2.2 ConsumeQueue2.3 IndexFile2.4 TopicTable2.5 SubscriptionGroupTable2.6 ConsumerOffset2.7 DelayQueue 3. 客户端&#xff08;生产者/消费者&#xff09;的文件结构和数据结构3.1 Produc…

77. 组合【含回溯详解、N叉树类比、剪枝优化】

文章目录 77. 组合思路暴力法回溯与N叉树类比回溯法三部曲 总结剪枝优化剪枝总结 77. 组合 77. 组合 给定两个整数 n 和 k&#xff0c;返回范围 [1, n] 中所有可能的 k 个数的组合。 你可以按 任何顺序 返回答案。 示例 1&#xff1a; 输入&#xff1a;n 4, k 2 输出&am…

Python:lambda 函数详解 以及使用

一、lambda 语法 lambda 函数的语法只包含一个语句&#xff0c;表现形式如下&#xff1a; lambda [arg1 [,arg2,.....argn]]:expression 其中&#xff0c;lambda 是 Python 预留的关键字&#xff0c;[arg…] 和 expression 由用户自定义。 具体如下: [arg…] 是参数列表&#…