MyString.h
#ifndef __MYSTRING_H__
#define __MYSTRING_H__
#include <iostream>
using namespace std;
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; // 字符串数据
};
#endif // __MYSTRING_H__
MyString.cpp
#include "MyString.h"
#include <string.h>
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];
}
MyString::MyString(const char *s)
{
if (s == NULL)
{
m_len = 0;
m_p = new char[1];
}
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;
}
}
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()
{
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];
}
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;
}
main.cpp
#include <iostream>
#include "MyString.h"
int main()
{
MyString str = "hello";
char * p = str.c_str2();
printf ("%s\n", str.c_str());
return 0;
}
int main7()
{
MyString str1 = "chelo";
MyString str2 = "world";
if (str1 > "bhello")
{
printf ("str1 > hello\n");
}
if (str1 > str2)
{
printf ("str1 > str2\n");
}
else
{
printf ("str1 < str2\n");
}
return 0;
}
int main6()
{
MyString str1 = "helo";
MyString str2 = "world";
if (str1 == "hello")
{
printf ("str1 == hello\n");
}
else
{
printf ("str1 != hello\n");
}
if (str1 == str2)
{
printf ("str1 == str2\n");
}
else
{
printf ("str1 != str2\n");
}
return 0;
}
int main5()
{
MyString str1 = "hello";
std::cout << str1[3] << std::endl;
str1[3] = 'w';
std::cout << str1 << std::endl;
return 0;
}
int main4()
{
MyString str1 = "hello ";
MyString str2 = "world";
str1 = str1 + "borld";
// str1 = str1 + str1;
// str1 += "world";
//str1 += str2;
std::cout << str1 << std::endl;
return 0;
}
int main3()
{
MyString str1;
// MyString & operator(MyString &str, const char *s)
str1 = "hello"; // 重载赋值操作符 =
//str1 = NULL;
std::cout << str1 << std::endl;
MyString str2;
str2 = str1;
std::cout << str2 << std::endl;
str2 = str2;
return 0;
}
int main2()
{
MyString str1 = "hello world"; // 用一个字串去初始化类对象
// <<
// ostream & operator<<(ostream &out, MyString &str)
std::cout << str1 << std::endl;
MyString str2(4);
// >>
// istream& operator>>(istream& in, MyString &str);
std::cout << "请输入字符串:" ;
std::cin >> str2;
std::cout << str2 << std::endl;
return 0;
}
int main8()
{
MyString str; // 空字符串
MyString str1 = "hello world"; // 用一个字串去初始化类对象
cout << str1 << endl;
MyString str2 = NULL; // 空字符串
MyString str3 = str1; // 用一个字符串对象初始化当前对象
MyString str4(10, 'a'); // 长度为10,每一个元素都是'a'
cout << str4 << endl;
MyString str5(20);
return 0;
}