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

C++重载实现Mystring

#include<iostream>
#include<cstring>        //可以使用string类
#include<string>       //#include <string.h>

using namespace std;

class Mystring
{
    public:
        Mystring():str(nullptr), len(0){
        }

        Mystring(const char *const str1)
        {
            if (str1)
            {
                len = strlen(str1);
                str = new char[len + 1]; 
                strcpy(str, str1);
            }
            else
            {
                str = nullptr;
                len = 0;
            }
        }

        ~Mystring()
        {
            delete[] str;
        }
                                                                     
        //  拷贝构造函数  
        Mystring(const Mystring &other)
        {
            len =other.len;
            str = new char[len+1];
            strcpy(str, other.str);
        }

        //拷贝赋值函数 
        Mystring& operator=(const Mystring& other)
        {
            if(&other != this)
            {
                if(str != nullptr)
                {
                    delete []str;
                    str = nullptr;
                }
				len = other.len;
                str = new char[len+1];
                strcpy(str, other.str);
            }

            return *this;
        }

        //data()
        char* data()
        {
            return str;
        }

        //size()
        int size()
        {
            return len;
        }

        //empty()
        bool empty()
        {
            return 0==len;
        }

        //at()
        char& at(int index)
        {
            if(index<0 || index>=len)
            {
                cout << index <<"位置越界" << endl;
                return str[0];
            }

            return str[index];      //*(str+index)
        }
		
		//拼接
		Mystring operator+(const Mystring& R) const
		{
			Mystring s;
			s.len = R.len+len;
			s.str=new char[s.len+1];
			strcpy(s.str,str);
			strcat(s.str,R.str);
			return s;
		}
		
		//两侧字符串不相等
		bool operator!=(const Mystring& R) const
		{
			return strcmp(str,R.str)!=0;
		}
		
		//两侧字符串相等
		bool operator==(const Mystring& R) const
		{
			return strcmp(str,R.str)==0;
		}
		
		//一侧字符串小于另一侧
		bool operator<(const Mystring& R) const
		{
			return strcmp(str,R.str)<0;
		}
		
		//一侧字符串小于等于另一侧
		bool operator<=(const Mystring& R) const
		{
			return strcmp(str,R.str)<=0;
		}
		
		//一侧字符串大于另一侧
		bool operator>(const Mystring& R) const
		{
			return strcmp(str,R.str)>0;
		}
		
		//一侧字符串大于等于另一侧
		bool operator>=(const Mystring& R) const
		{
			return strcmp(str,R.str)>=0;
		}
		
		
	friend istream& operator>>(istream& L,Mystring& R);
	friend ostream& operator<<(ostream& L,const Mystring& R);

    private:
        int len;
        char *str;
};

//重载<<
ostream& operator<<(ostream& L,const Mystring& R)
{
	L<<R.str;
	return L;
}

//重载>>
istream& operator>>(istream& L,Mystring& R)
{
	char buf[100];	
	L>>buf;
	R=Mystring(buf);
	return L;
}




int main(int argc,const char* argv[])
{
	Mystring str("ni hao");
	
	//字符串拼接
	Mystring str1;
	str1=str+" wen jie";
	cout<<"str1="<<str1<<endl;     
	
	//两侧字符串不相等
	Mystring str2("keli");
	bool res0 = str2 != str;
	cout<<"res0="<<res0<<endl;      
	
	//两侧字符串相等
	Mystring str3("ycd");
	bool res1 = str3 == "ycd";
	cout<<"res1="<<res1<<endl; 
	
	//一侧字符串小于另一侧
	Mystring str4("ycdab");
	bool res2 = str4 < "ycdab";
	cout<<"res2="<<res2<<endl; 
	
	//一侧字符串小于等于另一侧
	Mystring str5("ycdab");
	bool res3 = str5 <= "ycdab";
	cout<<"res3="<<res3<<endl; 
	
	//一侧字符串大于另一侧
	Mystring str6("ycdabe");
	bool res4 = str6 > "ycdab";
	cout<<"res4="<<res4<<endl; 
	
	//一侧字符串大于等于另一侧
	Mystring str7("ycdab");
	bool res5 = str7 >= "ycdab";
	cout<<"res5="<<res5<<endl; 
	
	Mystring str8;
	cout<<"请输入一个字符串"<<endl;
	cin>>str8;
	cout<<"str8="<<str8<<endl;
	
	
	return 0;
}


http://www.kler.cn/news/289610.html

相关文章:

  • qt5.15.2 模拟LVGL8.3
  • DataX导入或导出hive数据
  • 读书学习笔记入门 # Datawhale X 李宏毅苹果书 AI夏令营
  • 人活着的意义是什么
  • 监控平台之pvuv/点击事件/路由上报
  • 树莓派扩展RGB点阵屏的使用
  • vue-----window.open打开新窗口文件并且修改窗口标题下载文件
  • 云微客短视频矩阵系统,如何让企业赢在起跑线?
  • Flask框架默认session处理机制
  • gRPC学习之六:gRPC-Gateway集成swagger
  • 山东省行政执法证照片要求及图像处理方法
  • 实训day40(8.30)
  • 数据结构-广义表
  • 在uni-app中使用SQLite
  • office套件打开时 提示操作系统当前的配置不能运行此应用程序
  • VS2022使用指定的LLVM版本
  • windows 环境下安装OpenCV For Java
  • 一些可能很有用的矩阵知识
  • 新手c语言讲解及题目分享(十)——数组专项练习
  • 免费GPU平台教程,助力你的AI, pytorch tensorflow 支持cuda
  • Vue3组件通信概览
  • [Raspberry Pi]如何利用docker執行motioneye,並利用Line Notify取得即時通知和照片?
  • 2024车牌识别系统十大品牌发布,车牌智能识别系统品牌哪家好?
  • 【408 数据结构】第1章绪论
  • 开发基础软件安装地址(持续更新中)
  • Spring Boot实战:运用享元模式优化微服务间共享资源
  • 使用VM创建centos7环境
  • 知识点复习3
  • 速盾:什么是高防cdn?高防cdn的特点和好处有哪些?
  • 微信小程序知识点(一)