C++实现自定义string类

在一些C++笔试题里,会有这样一道题,那就是让你自己实现一个简单的string类。自己在面试的时候就遇到过这个题。在这里说一下自己是怎么做的。

1.编写头文件

主要包含一些基本的操作,构造函数、拷贝构造函数和析构函数。

#pragma once
#include <iostream>
using namespace std;

class  MyString
{
public:
	MyString(int len = 0);//构造函数
	MyString(const char *p);//构造函数
	MyString(const MyString& s);//拷贝构造函数
	~MyString();//析构函数
private:
	int		m_len;//数组长度
	char	 *m_p;//指针

};

2.cpp文件

#include"stdafx.h"
#include"MyString.h"
MyString::MyString(int len)
{
	if (len == 0)//长度为0是赋值为""
	{
		m_len = 0;
		m_p = new char[m_len + 1];
		strcpy(m_p, "");
	}
	else//长度不为空时初始化新申请的内存
	{
		m_len = len;
		m_p = new char[m_len + 1];
		memset(m_p, 0, m_len);
	}
}
MyString::MyString(const char* p)
{
	if (p == NULL)//当指针为空时,赋值为""
	{
		m_len = 0;
		m_p = new char[m_len + 1];
		strcpy(m_p, "");
	}
	else//不为空时将p指向的内容赋值给新开辟的内存
	{
		m_len = strlen(p);
		m_p = new char[m_len + 1];
		strcpy(m_p, p);
	}
}
MyString::MyString(const MyString& s)//拷贝构造函数
{
	m_len = s.m_len;
	m_p = new char[m_len + 1];
	strcpy(m_p, s.m_p);
}
MyString::~MyString()//析构函数
{
	if (m_p != NULL)
	{
		delete[] m_p;
		m_p = NULL;
		m_len = 0;
	}
}

到这里简单的string类就实现了,接下来是一些运算符的重载操作。

3.头文件(新增了一些重载操作)

#pragma once
#include <iostream>
using namespace std;

class  MyString
{
public:
	MyString(int len = 0);//构造函数
	MyString(const char *p);//构造函数
	MyString(const MyString& s);//拷贝构造函数
	~MyString();//析构函数

public: //重载=号操作符以及[],+
	MyString& operator=(const char *p);
	MyString& operator=(const MyString &s);
	char& operator[] (int index);
	MyString operator+(const MyString &s) const;

public: //重载 == !== 
	bool operator==(const char *p) const;
	bool operator==(const MyString& s) const;
	bool operator!=(const char *p) const;
	bool operator!=(const MyString& s) const;

public: //重载<>
	int operator<(const char *p);
	int operator>(const char *p);
	int operator<(const MyString& s);
	int operator>(const MyString& s);
private:
	int		m_len;//数组长度
	char	 *m_p;//指针
};

4.cpp文件(增加重载操作)


//重载等号=操作符和[]
MyString& MyString::operator=(const char*p)
{
	if (m_p != NULL)
	{
		delete[] m_p;
		m_len = 0;
	}
	if (m_p == NULL)
	{
		m_len = 0;
		m_p = new char[m_len + 1];
		strcpy(m_p, "");
	}
	else
	{
		m_len = strlen(p);
		m_p = new char[m_len + 1];
		strcpy(m_p, p);
	}

	return *this;
}
MyString& MyString::operator=(const MyString& s)
{
	if (m_p != NULL)
	{
		delete[] m_p;
		m_len = 0;
	}

	m_len = s.m_len;
	m_p = new char[m_len + 1];
	strcpy(m_p, s.m_p);

	return *this;
}
char& MyString::operator[](int index)
{
	return m_p[index];
}

MyString MyString::operator+(const MyString &s) const
{
	MyString newString;
	newString.m_len = m_len + s.m_len;
	newString.m_p = new char[newString.m_len + 1];
	strcpy(newString.m_p, m_p);
	strcat(newString.m_p, s.m_p);
	return newString;
}

//重载 == !=
bool MyString::operator==(const char* p) const
{
	if (p == NULL)
	{
		if (m_len == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else 
	{
		if (m_len == strlen(p))
		{
			return !strcmp(m_p, p);
		}
		else
		{
			return false;
		}
	}
}
bool MyString::operator==(const MyString& s)const
{
	if (s.m_len != m_len)
	{
		return false;
	}
	else
	{
		return !strcmp(m_p, s.m_p);
	}
}

bool MyString::operator!=(const char* p)const
{
	return !(*this == p);
}
bool MyString::operator!=(const MyString& s)const
{
	return !(*this == s);
}

//重载<>
int MyString::operator>(const char* p)
{
	return strcmp(p, this->m_p);
}
int MyString::operator<(const char* p)
{
	return strcmp(this->m_p, p);
}
int MyString::operator>(const MyString& s)
{
	return strcmp(s.m_p, this->m_p);
}
int MyString::operator<(const MyString& s)
{
	return strcmp(this->m_p, s.m_p);
}

至此一个由自己编写的简单string类就实现了。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值