封装一个字符串
MyString.h
#ifndef __MYSTRING_H__
#define __MYSTRING_H__
#include <iostream>
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 <cstring>
MyString::MyString()
{
m_len = 0;
m_p = nullptr;
}
MyString::MyString(const char *s)
{
if (NULL == s)
{
m_len = 0;
m_p = nullptr;
}
else
{
m_len = strlen(s);
m_p = new char[m_len+1];
strcpy(m_p, s);
}
}
MyString::MyString(int len, char data)
{
if (len <= 0)
{
m_p = nullptr;
m_len = 0;
}
else
{
m_len = len;
m_p = new char[m_len+1];
for (int i = 0;i < len; i++)
{
m_p[i] = data;
}
m_p[len] = '\0';
}
}
MyString::MyString(const MyString &s)
{
if (nullptr == s.m_p)
{
m_len = 0;
m_p = nullptr;
}
else
{
m_len = s.m_len;
m_p = new char[m_len+1];
strcpy(m_p, s.m_p);
}
}
MyString::~MyString()
{
if (m_p != nullptr)
delete[] m_p;
m_p = nullptr;
m_len = 0;
}
MyString& MyString::operator=(const char *s)
{
MyString str = s;
*this = str; // 赋值
return *this;
}
MyString& MyString::operator=(const MyString &s)
{
if (this != &s)
{
MyString tmp = s;
char *p = tmp.m_p;
tmp.m_p = m_p;
m_p = p;
m_len = s.m_len;
}
return *this;
}
MyString MyString::operator+(const char *str)
{
int len = m_len + strlen(str);
MyString tmp(len);
strcpy(tmp.m_p, m_p);
strcat(tmp.m_p, str);
return tmp;
}
MyString MyString::operator+(const MyString &s)
{
int len = m_len + s.m_len;
MyString tmp(len);
strcpy(tmp.m_p, m_p);
strcat(tmp.m_p, s.m_p);
return tmp;
}
MyString& MyString::operator+=(const char *str)
{
// *this = *this + str;
int len = m_len + strlen(str);
char *tmp = m_p;
m_p = new char[len+1];
strcpy(m_p, tmp);
strcat(m_p, str);
m_len = len;
delete[] tmp;
return *this;
}
MyString& MyString::operator+=(const MyString &s)
{
// *this = *this + s;
int len = m_len + s.m_len;
char *tmp = m_p;
m_p = new char[len+1];
strcpy(m_p, tmp);
if (this == &s)
strcat(m_p, tmp);
else
strcat(m_p, s.m_p);
m_len = len;
delete[] tmp;
return *this;
}
char & MyString::operator[](int index)
{
return m_p[index];
}
bool MyString::operator==(const char *str) const
{
// if (strcmp(m_p, str) == 0)
// return true;
// else
// return false;
return (strcmp(m_p, str) == 0);
}
bool MyString::operator==(const MyString &str) const
{
return (strcmp(m_p, str.m_p) == 0);
}
bool MyString::operator!=(const char *str) const
{
return (strcmp(m_p, str) != 0);
}
bool MyString::operator!=(const MyString &str) const
{
return (strcmp(m_p, str.m_p) == 0);
}
bool MyString::operator>(const char *str) const
{
// if (strcmp(m_p, str) == 0)
// return true;
// else
// return false;
return (strcmp(m_p, str) > 0);
}
bool MyString::operator>(const MyString &str) const
{
return (strcmp(m_p, str.m_p) > 0);
}
bool MyString::operator<(const char *str) const
{
return (strcmp(m_p, str) < 0);
}
bool MyString::operator<(const MyString &str) const
{
return (strcmp(m_p, str.m_p) < 0);
}
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;
return in;
}