写一个字符串类,实现字符串的功能
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;
}