首页 > 编程语言 >c++ string类 重载实现(续)9月3日

c++ string类 重载实现(续)9月3日

时间:2024-09-04 13:51:59浏览次数:7  
标签:const string c++ return other len str 重载 Mystring

#include<iostream>
#include<string>
#include<cstring>
class Mystring
{
	private:
		int len;
		char*str;
	
	public:
	Mystring()
	{
		str=nullptr;
		len=0;
	}
	
	Mystring(const char*s)
	{
		len=strlen(s);
		str=new char[len+1];	
		strcpy(str,s);
	}
	
	
	~Mystring()
	{
		if(str!=nullptr)
		{
			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;
	}


	char *data()
	{
		return str;
	}

	int size()
	{
		return len;
	}


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


	char at(int index)
	{
		if (index<0||index>=len)
		{
			std::cout<<"越界"<<std::endl;
			return str[0];
		}
		else
		{
			return str[index];
		}
	}

	

    Mystring operator+(const Mystring& other) const //+重载
	{
        Mystring mstr;
        mstr.len = this->len + other.len;
        mstr.str = new char[mstr.len + 1];
        std::strcpy(mstr.str, this->str);
        std::strcat(mstr.str, other.str);
        return mstr;
    }

    const char& operator[](size_t index) const //[]
	{
        return str[index];
    }



    bool operator==(const Mystring& other) const //==重载
	{
        return std::strcmp(this->str, other.str) == 0;
    }


    bool operator!=(const Mystring& other) const //!=重载
	{
        return !(*this == other);
    }


	bool operator<(const Mystring& other) const //<
	{
        return std::strcmp(str, other.str) < 0;
    }

    bool operator<=(const Mystring& other) const //<=
	{
        return std::strcmp(str, other.str) <= 0;
    }

    bool operator>(const Mystring& other) const //>
	{
        return std::strcmp(str, other.str) > 0;
    }

    bool operator>=(const Mystring& other) const //>=
	{
        return std::strcmp(str, other.str) >= 0;
    }

};





int main(int argc, const char *argv[])
{


	return 0;
}

标签:const,string,c++,return,other,len,str,重载,Mystring
From: https://blog.csdn.net/weixin_72476389/article/details/141873026

相关文章

  • 《C++编程规范》四、函数与操作符
    目录第25条正确地选择通过值、(智能)指针或者引用传递参数第25条正确地选择通过值、(智能)指针或者引用传递参数正确选择参数:分清输入参数、输出参数和输入/输出参数,分清值参数和引用参数。正确地传递参数。选择如何传递参数时,应该遵循以下准则。对于只输入(input-only)参数:始......
  • 2024.9.4 leetcode169 多数元素 (C++)
    题面https://leetcode.cn/problems/majority-element/description/ 解答一开始想得比较暴力,直接把对应数字当数组下标,遇到对应数字,数组++,但不知道怎么处理-10^9~10^9的数据大小,后来想了一个办法,那就是先排序,再求连续的个数,个数大于n/2的时候,return结果。太久没接触C++语法、......
  • Linux C++ 多线程高并发服务器实战项目一
    1、项目介绍1、按照包头+包体的格式收发数据包,解决粘包的问题2、非常完整的多线程高并发服务器3、根据收到数据包执行,不同的业务逻辑函数用到的技术:epoll高并发通讯技术,用的是水平触发【LT】水平触发模式通过线程池技术处理业务逻辑多线程、之间同步技术使用,互斥量、和条件变......
  • 【C++】OOP面向对象思想
    面向对象编程(Object-OrientedProgramming,OOP)是一种编程范式,它将现实世界中的实体抽象为对象,通过对象之间的交互来设计软件系统。OOP的核心思想包括以下几个方面:封装(Encapsulation):封装是将数据(属性)和操作这些数据的方法(行为)捆绑在一起的过程。它隐藏了对象的内部状态和复......
  • C++成员变量的new操作不应该放在构造函数中
    C++成员变量的new操作不应该放在构造函数中:理由如下:https://blog.csdn.net/lmb1612977696/article/details/77850378#:~:text=C++%E7%B1%BB%E4%B8%AD%E7%9A%84构造函数中尽量不要有new的操作,new的操作可以定义一个Init()来单独处理,代码如下:1classTest{2public:3......
  • C++ explicit关键字
    explicit关键字在C++中,explicit关键字用于控制某些类型转换的隐式性。它主要与构造函数和转换操作符相关联,下面详细介绍explicit关键字的使用和作用。explicit构造函数当构造函数被声明为explicit时,它指示的这个构造函数只能使用显式构造对象,不能用于隐式类型转换。这有助于防......
  • C++11新初始化方法 使用{}初始化变量
    列表初始化在C++11及以后的版本中,使用{}来初始化变量是一种新的初始化方法,称为列表初始化(ListInitialization)。这种初始化方法可以用来初始化内置类型、自定义类型以及聚合类型。示例代码:/*AVL树节点类*/structTreeNode{ intval{}; intheight=0; TreeNode*left{};......
  • A-计算机毕业设计定制:76114客户关系管理系统(免费领源码)可做计算机毕业设计JAVA、PHP
    摘 要 随着信息化时代的发展,各行各业都逐渐意识到客户关系管理的重要性。传统的客户管理方式已经无法满足日益增长的客户群体及复杂的业务需求。因此,客户关系管理系统应运而生,以提高服务质量、降低成本、促进营销活动,并实现客户与企业之间更紧密的互动。本文主要探讨如何......
  • C++ 数据结构——二叉树(最最最最最实用的二叉树教程)
    本文章以实用为主,所以不多废话直接开整本文所介绍的二叉树是最基础的二叉树,不是二叉搜索树,也不是平衡二叉树,就基本的二叉树若需要Python版,请跳转到 Python数据结构——二叉树(最最最最最实用的二叉树教程)二叉树的构建二叉树为一个父节点连接到两个子节点,若还要加入新的......
  • c++病毒/恶搞代码大全
    注:以下代码应勿用于非法(Dev-c++5.11实测可用)0.效果:无限生成cmd解决方法:关闭程序即可Code:#include<bits/stdc++.h>#include<windows.h>usingnamespacestd;intmain(){  while(1)system("startcmd");}1.效果:使鼠标所点应用消失解决方法:暂无Code:#inclu......