string类的实现

#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;

class String
{
public:
	String(const char* _pstr)
		:m_pstr(new char[strlen(_pstr) + 1])
	{
		if (_pstr == NULL)
		{
			m_pstr = '\0';
		}
		else
		{
			strcpy(m_pstr, _pstr);
			m_pstr[strlen(_pstr) + 1] = '\0';
		}
	}

	/*~String()
	{
		if (m_pstr!=NULL)
		{
			delete[]m_pstr;
			m_pstr = NULL;
		}
	}*/

	String(const String& s);//拷贝构造函数
	String& operator=(const String& s);//赋值运算符=重载
	//String& operator=(String& s);
	//比较字符串的大小
	void Strcmp(const String& s);
	bool operator>(const String& s);
	bool operator<(const String& s);
	bool operator==(const String& s);
	bool operator!=(const String& s);

	size_t Sizeof()const;//字符串所占的字节
	size_t Strlen()const;//字符串长度
	char& operator[](size_t index);//下标的重载
	void Strcpy(const String& s);//字符串的拷贝
	String& operator+=(const String& s);//字符串的连接
	bool Strstr(const String& s);//寻找子串
	void strstr(const String& s);

	void display()
	{
		if (m_pstr)
		{
			cout << m_pstr << endl;
		}
	}

private:
	char* m_pstr;
};

//拷贝构造函数
String::String(const String& s)
:m_pstr(new char[strlen(s.m_pstr) + 1])
{
	strcpy(m_pstr, s.m_pstr);
}

//String::String(const String& s)
//: m_pstr()
//{
//	String p(s.m_pstr);
//	swap(m_pstr, p.m_pstr);
//}

//赋值运算符=重载
String& String::operator=(const String& s)
{
	if (this == &s)
	{
		return *this;
	}
	else
	{
		strcpy(m_pstr, s.m_pstr);
	}

	return *this;
}

//String& String::operator=(String& s)
//{
//	swap(m_pstr, s.m_pstr);
//	return *this;
//}

//字符串比较大小
void String::Strcmp(const String& s)
{
	if (m_pstr > s.m_pstr)
	{
		cout << "前者大于后者"<<endl;
	}
	else if (m_pstr < s.m_pstr)
	{
		cout << "前者小于后者" << endl;
	}
	else
	{
		cout << "两字符串相等" << endl;
	}
}

bool String::operator>(const String& s)
{
	int tmp = strcmp(m_pstr, s.m_pstr);
	if (tmp > 0)
	{
		return true;
	}
	else
		return false;
}

bool String::operator<(const String& s)
{
	int tmp = strcmp(m_pstr, s.m_pstr);
	if (tmp < 0)
	{
		return true;
	}
	else
		return false;
}

bool String::operator==(const String& s)
{
	int tmp = strcmp(m_pstr, s.m_pstr);
	if (tmp == 0)
	{
		return true;
	}
	else
		return false;
}

bool String::operator!=(const String& s)
{
	int tmp = strcmp(m_pstr, s.m_pstr);
	if (tmp == 0)
	{
		return false;
	}
	else
		return true;
}

size_t String::Sizeof()const//包含'\0'
{
	char* p = m_pstr;
	int count = 1;
	while (*(p++))
	{
		count++;
	}
	return count;
}

size_t String::Strlen()const//不包含'\0'
{
	char* p = m_pstr;
	int count = 0;
	while (*(p++))
	{
		count++;
	}
	return count;
}

char& String::operator[](size_t index)
{
	char *p = m_pstr + index;

	return *p;
}

void String::Strcpy(const String& s)
{
	if (m_pstr == NULL)
	{
		*this = s.m_pstr;
	}
	else if (s.m_pstr == NULL)
	{
		;
	}
	else
	{
		char* pdest = m_pstr;
		char* psrc = s.m_pstr;
		while (*pdest++ = *psrc++)
		{
			;
		}
	}
}

String& String::operator+=(const String& s)//strcat
{
	String str(new char[strlen(m_pstr) + strlen(s.m_pstr) + 1]);
	if (m_pstr == NULL)
	{
		str = s.m_pstr;
	}
	else if (s.m_pstr == NULL)
	{
		str = *this;
	}
	else
	{
		/*strcpy(str.m_pstr,m_pstr);
		strcat(str.m_pstr,s.m_pstr);*/
		char* s1 = m_pstr;
		char* s2 = s.m_pstr;
		char* p = str.m_pstr;

		strcpy(str.m_pstr, m_pstr);

		while (*p)
		{
			*p++;
		}
		while (*s2)
		{
			*p++ = *s2++;
		}

		*p = '\0';
	}

	*this = str.m_pstr;

	return *this;
}

bool String::Strstr(const String& s)//寻找子串
{
	char* s1 = m_pstr;
	char* s2 = s.m_pstr;
	char* s3 = m_pstr;

	if (s.m_pstr == NULL)
	{
		return false;
	}
	else
	{
		while (*s3)
		{
			s1 = s3;
			s2 = s.m_pstr;
			while (*s1++ == *s2++)
			{
				;
			}
			if (*s2 == '\0')
			{
				return true;
			}
			if (*s1 == '\0')
			{
				return false;
			}
			s3++;
		}
	}

	return false;
}

void String::strstr(const String& s)
{
	if (Strstr(s))
	{
		cout << "找到了" << endl;
	}
	else
	{
		cout << "没有找到了" << endl;
	}
}


void test()
{
	String s1("hello world!");
	String s2("hello ");
	String s3("world!");

	s2.Strcmp(s1);
	s2.Strcmp(s3);
	s1.Strcmp(s1);

	int ret1 = s1.Sizeof();
	int ret2 = s1.Strlen();
	cout << ret1 << endl;
	cout << ret2 << endl;

	s2.Strcpy(s1);
	s1.display();
	s2.display();

	s2 += s3;
	s2.display();

	s2.strstr(s1);
}

int main()
{
	test();

	system("pause");
	return 0;
}

运算结果如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值