C++重写string类

C++提供的string并不是那么好用,而Qt中的Qstring提供了很多接口。往往我们需要重写string类

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

	// 重载 >> 操作符
	friend std::istream& operator>>(std::istream& in, MyString &str);
	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 &str) const;

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

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

		bool operator<(const char *str) const;
		bool operator<(const MyString &str) 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;    // 字符串数据
};

// 下面是实现
std::ostream& operator<<(std::ostream &out, MyString &str)
{
	out << str.m_p << " ";

	return out;
}

std::istream& operator>>(std::istream& in, MyString &str)
{
	in >> str.m_p;
	//fgets (str.m_p, str.m_len, stdin);
	return in;
}

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
	{
	    m_len = strlen(s);
	    m_p = new char[m_len + 1];
	    strcpy (m_p, s);
	}
}

MyString::MyString(int len, char data)
{
	m_len = len;
	m_p = new char[m_len+1];
	for (int i = 0; i < m_len; i++)
	{
	    m_p[i] = data;
	}
	m_p[m_len] = '\0';
}

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

MyString& MyString::operator=(const char *s)
{
	// 先释放原有空间
	delete [] m_p;
	if (s == NULL)
	{
	    m_len = 0;
	    m_p = new char[1];
	    m_p[0] = '\0';
	}
	else
	{
	    m_len = strlen(s);
	    m_p = new char[m_len+1];
	    strcpy(m_p, s);
	}

	return *this;
}

MyString& MyString::operator=(const MyString &s)
{
	// 是本身的情况下,直接返回当前对象
	if (this == &s)
    {
	    return *this;
    }
	// 先释放
	delete[] m_p;

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

	return *this;
}

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

	m_len += strlen(str);
	char *tmp = m_p;            // 将原有数据保存下来
	m_p = new char[m_len + 1];  // 分配新空间
	strcpy (m_p, tmp);          // 将原来的数据导入到新空间
	strcat (m_p, str);          // 将新数据粘到原有数据后面
	delete [] tmp;              // 释放原来的空间

	return *this;
}

MyString& MyString::operator+(const MyString &s)
{
	m_len += s.m_len;
	char *tmp = m_p;            // 将原有数据保存下来
	m_p = new char[m_len + 1];  // 分配新空间
	strcpy (m_p, tmp);          // 将原来的数据导入到新空间
	
	// 如果是自己,复制原有空间内容
	if (this == &s)
	{		
	    strcat (m_p, tmp);
	}
	else
	{	
		// 复制新字符串的内容
	    strcat (m_p, s.m_p);
	}	
	// 释放原来的空间
	delete [] tmp;              

	return *this;
}

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

	return *this;
}

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

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

bool MyString::operator==(const char *str) const
{
	if (str == NULL)
	{
		if (m_len == 0)
        {
		    return true;
        }   
		else
        {
		    return false;
        }
	}
	else
	{
		if (strcmp(m_p, str) == 0)
        {
            return true;
        }
			
		else
        {
            return false;
        }	
	}
}

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

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

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

bool MyString::operator>(const char *str) const
{
	if(str == NULL)
    {
        return true;
    }
	
	int i = 0;
	int len = (m_len < strlen(str) ? m_len : strlen(str)); // 保存较短的字符串的长度
	while (m_p[i] == str[i] && i<len)
    {
        i++;
    }
		
	return (m_p[i] > str[i] ? true : false);
}

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

bool MyString::operator<(const char *str) const
{
	if(str == NULL)
    {
        return true;
    }
		
	int i = 0;
	int len = (m_len < strlen(str) ? m_len : strlen(str)); // 保存较短的字符串的长度
	while (m_p[i] == str[i] && i<len)
    {
		i++;
    }

	return (m_p[i] < str[i] ? true : false);
}

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

int main()
{
	MyString str;            // 空字符串
	MyString str1 = "hello"; // 有参构造
	MyString str2 = NULL;
	MyString str3(10);       // 定义一个字符串,长度为10
	MyString str4(10, 'a');
	MyString str5 = str1;    // 拷贝构造
	cout << str5 << endl;

	cout << str1 << endl;
	cin >> str3;
	cout << "str3: " << str2 << endl;

	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值