字符串类的实现

写一个字符串类,实现字符串的功能


MyString.h

#ifndef __MYSTRING_H__
#define __MYSTRING_H__

#include <iostream>

class MyString
{
	// 重载 << 操作符
	friend std::ostream& operator<<(std::ostream &out, MyString &s);

	// 重载 >> 操作符
	friend std::istream& operator>>(std::istream& in, MyString &s);
public:
	MyString();                   // 无参构造
	MyString(const char *s);      // 有参构造
	MyString(int len, char data = 0); // 有参构造
	MyString(const MyString &s);  // 拷贝构造

	~MyString();                  // 析构函数

// 重载=、[]操作符
public:
	MyString& operator=(const char *s);       // 普通字符串赋值
	MyString& operator=(const MyString &s);   // 类对象之间赋值
	char & operator[](int index);

// 重载 + 运算符
public:
	MyString& operator+(const char *str);
	MyString& operator+(const MyString &s);

	MyString& operator+=(const char *str);
	MyString& operator+=(const MyString &s);

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

	bool operator!=(const char *str) const;
	bool operator!=(const MyString &s) const;

// 重载 < >
public:
	bool operator>(const char *str) const;
	bool operator>(const MyString &s) const;

	bool operator<(const char *str) const;
	bool operator<(const MyString &s) const;

public:
	const char *c_str()
	{
		return m_p;
	}

	char *c_str2()
	{
		return m_p;
	}

	int leng()
	{
		return m_len;
	}
private:
	int m_len;    // 字符串长度
	char *m_p;    // 字符串数据
};




#endif // __MYSTRING_H__

MyString.h

#include "MyString.h"

MyString::MyString()
{
	m_len = 0;
	m_p = new char[1];
	m_p[0] = '\0';
}

MyString::MyString(const char *s)
{
	if(s == NULL)
	{
		m_len = 0;
		m_p = new char[1];
		m_p[0] = '\0';
	}
	else 
	{
		int len = strlen(s);
		m_len = len;
		m_p = new char[len+1];
		strcpy(m_p, s);
	}
}

MyString::MyString(int len , char data )  //默认参数只需在声明中写
{
	m_len = len;
	m_p = new char[len+1];

	memset(m_p , data, len);
	m_p[len] = '\0';
}

MyString::MyString(const MyString &s)
{
	if(s == NULL)
	{
		m_len = 0;
		m_p = new char[1];
		m_p[0] = '\0';
	}
	else
	{
		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;
	}
}

MyString& MyString::operator=(const char *s)
{
	if(s == NULL)
		return *this;
	if(m_p != NULL)
	{
		delete[] m_p;
		m_p = NULL;
	}
	m_len = strlen(s);
	m_p = new char[m_len+1];
	strcpy(m_p, s);
	return *this;
}

MyString& MyString::operator=(const MyString &s)
{
	if(s.m_p == NULL)
		return *this;
	if(m_p != NULL)
	{
		delete[] m_p;
		m_p = NULL;
	}
	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 char *str)
{
	if(str == NULL)
		return *this;

	int len = strlen(m_p) + strlen(str);
	m_len = len;
	char * tmp = m_p;

	m_p = new char[len+1];
	strcpy(m_p, tmp);
	strcat(m_p, str);

	delete []tmp;

	return *this;
}

MyString & MyString::operator+(const MyString &s)
{
	return (*this + s.m_p);
}

MyString & MyString::operator+=(const char *str)
{
	return (*this + str);
}

MyString & MyString::operator+=(const MyString &s)
{
	return (*this + s);
}

bool MyString::operator==(const char *str) const    //后面的const表示this指针也是只读变量
{
	if(m_len != strlen(str))
		return false;

	for(int i = 0; i<m_len ; i++)
	{
		if(m_p[i] != str[i])
			return false;
	}
	return true;
}

bool MyString::operator==(const MyString &s) const
{
	return (*this == s.m_p);
}

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

bool MyString::operator>(const char *str) const
{
	if(str == NULL)
		return true;

	/*int len = strlen(str);
	int max = (m_len > len ? m_len : len );

	for(int i = 0; i<max ; i++)
	{
		if(m_p[i] == str[i])
			break;

		if(m_p[i] > srt[i])
			return true;
		else
			return false;
	}

	return (m_lem > len ? true : false);*/

	//因为超过长度了就用'\0' 比较,然后返回,所以不会越界
	int len = strlen(str);
	int max = (m_len > len ? m_len : len );

	for(int i = 0; i<max ; i++)
	{
		if(m_p[i] == str[i])
			break;

		if(m_p[i] > str[i])
			return true;
		else
			return false;
	}
	return true;    //当他们长度相等,并且大小相等时,返回true
}

bool MyString::operator>(const MyString &s) const
{
	return (*this > s.m_p);
}

//小于号有一个问题,当两个字符串相等时,会返回false
bool MyString::operator<(const char *str) const
{
	return !(*this > str);
}

bool MyString::operator<(const MyString &s) const
{
	return !(*this > s.m_p);
}

std::ostream& operator<<(std::ostream &out, MyString &s)
{
	out << s.m_p;
	return out;
}

std::istream& operator>>(std::istream& in, MyString &s)
{
	in >> s.m_p;
	return in; 
}

main.cpp
#include <iostream>
#include "MyString.h"

using namespace std;

int main()
{
	MyString str1 = "hello";
	MyString str2 ;
	MyString str3(10,'a');

	cout << str1 << endl;
	cout << str2 << endl;
	cout << str3 << endl;

	//str2 = str1 + " world";
	//cout << str2 << endl;
	//str2 = str1 + str3;
	//cout << str2 << endl;

	str2 = str3;
	cout << str2 << endl;

	cout << (str2 == str3) << endl;
	cout << (str1 == "hello") << endl;
	cout << (str2 != str3) << endl;

	cout << (str1 > "hello") << endl;
	cout << (str1 < "hello") << endl;

	cout << (str1 > str2) << endl;
	cout << (str1 < str2) << endl;
	system("pause");
	return 0;
}


自己实现字符串 class CMStringImp; class CMstring { public: explicit CMstring(void); ~CMstring(void); CMstring(LPCTSTR lpszstr); CMstring(const CMstring& lpszstr); CMstring& operator = (const CMstring& lpszstr); operator LPCTSTR() const; bool operator == (const CMstring&) const; bool operator != (const CMstring&) const; bool operator < (const CMstring&) const; TCHAR operator[] (int nIndex) const; TCHAR& operator[] (int nIndex); CMstring& operator += (LPCTSTR pStr); CMstring& operator += (TCHAR ch); friend CMstring operator+(const CMstring& str1, const CMstring& str2); friend CMstring operator+(const CMstring& str1, LPCTSTR lpszstr); friend CMstring operator+(const CMstring& str1, TCHAR ch); friend CMstring operator+(TCHAR ch, const CMstring& str1); friend CMstring operator+(LPCTSTR lpszstr, const CMstring& str1); // friend wostream operator <<(wostream &is;,const CMstring &str;); public: LPCTSTR GetData() const; bool IsEmpty() const; TCHAR GetAt(int) const; TCHAR& GetAt(int); int GetLength() const; int Compare(const CMstring&) const; int CompareNoCase(const CMstring&) const; int Find(TCHAR ch, int nStart = 0) const; int Find(LPCTSTR pStr, int nStart = 0) const; int ReverseFind(TCHAR ch) const; int ReverseFind(LPCTSTR pStr) const; CMstring Right(int nCount) const; CMstring Left(int nCount ) const; public: CMstring& MakeLower(); CMstring& MakeUpper(); CMstring& MakeReverse(); int Replace(TCHAR chOld, TCHAR chNew); int Replace(LPCTSTR pszOld, LPCTSTR pszNew); int Insert(int iIndex, TCHAR ch); void Format(LPCTSTR lpszFormat, ...); private: CMStringImp* m_pImp; };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值