写了3个小时才完成,运算符重载还得多练习
开发工具:宇宙第一ide VS2019!
废话不多说,直接上代码!
File: MyString.h
#pragma once
#include <iostream>
class MyString
{
public: // 构造与析构
MyString(const char* str = "");
MyString(const MyString& str);
~MyString();
public:
// 访问属性的函数
int length(); // 返回存储字符串的长度
char* c_str(); // 返回存储的字符串
char* data(); // 返回存储的字符串
// 判断空
bool isEmpty(); // 判断是否为空
bool isNotEmpty(); // 判断是否不为空
protected: // 保护属性
char* pStr;
int strLen;
public: // 重载运算符
// 赋值
void operator=(MyString str);
void operator=(const char* str);
// 元素、地址访问
char operator[](int sub);
char* operator+(int sub);
// 比较
bool operator==(MyString str);
bool operator!=(MyString str);
bool operator>(MyString str);
bool operator<(MyString str);
bool operator>=(MyString str);
bool operator<=(MyString str);
// 非(判断是否为空,空返回true)
bool operator!();
// 连接
friend MyString operator+(MyString str1, MyString str2);
friend MyString operator+(MyString str1, const char* str2);
friend MyString operator+(const char* str1, MyString str2);
friend void operator+=(MyString& str1, MyString str2);
friend void operator+=(MyString& str1, const char* str2);
// 重复
friend MyString operator*(MyString str, int multiplier);
friend MyString operator*(int multiplier, MyString str);
friend void operator*=(MyString& str, int multiplier);
// 流重载
friend std::istream& operator>>(std::istream& in, MyString& str);
friend std::ostream& operator<<(std::ostream& out, MyString& str);
public: // 迭代器
class Iterator
{
public:
Iterator()
{
this->pmstr = nullptr;
this->curSub = 0;
}
Iterator(MyString* pmstr, int sub = 0)
{
this->pmstr = pmstr;
this->curSub = sub;
}
protected:
const MyString* pmstr;
int curSub;
public:
void operator=(Iterator iterator)
{
this->pmstr = iterator.pmstr;
this->curSub = iterator.curSub;
}
bool operator!=(Iterator iterator)
{
if (this->pmstr != iterator.pmstr || this->curSub != iterator.curSub)
return true;
else
return false;
}
void operator++()
{
this->curSub++;
}
void operator++(int)
{
++this->curSub;
}
char operator*()
{
return *(this->pmstr->pStr + this->curSub);
}
};
Iterator begin()
{
return Iterator(this, 0);
}
Iterator end()
{
return Iterator(this, this->strLen);
}
};
File: MyString.cpp
#include "MyString.h"
#include <cstring>
#include <crtdbg.h> // 断言
MyString::MyString(const char* str)
{
this->strLen = strlen(str);
this->pStr = new char[this->strLen + 1];
strcpy_s(this->pStr,this->strLen + 1, str);
}
// 深拷贝构造
MyString::MyString(const MyString& str)
{
this->strLen = str.strLen;
this->pStr = new char[this->strLen + 1];
strcpy_s(this->pStr, this->strLen + 1, str.pStr);
}
MyString::~MyString()
{
delete[] this->pStr;
this->pStr = nullptr;
}
int MyString::length()
{
return this->strLen;
}
char* MyString::c_str()
{
return this->pStr;
}
char* MyString::data()
{
return this->pStr;
}
bool MyString::isEmpty()
{
return (strcmp(this->pStr, "") == 0);
}
bool MyString::isNotEmpty()
{
return (strcmp(this->pStr, "") != 0);
}
void MyString::operator=(MyString str)
{
this->strLen = str.strLen;
delete[] this->pStr;
this->pStr = new char[this->strLen + 1];
strcpy_s(this->pStr, this->strLen + 1, str.pStr);
}
void MyString::operator=(const char* str)
{
this->strLen = strlen(str);
delete[] this->pStr;
this->pStr = new char[this->strLen + 1];
strcpy_s(this->pStr, this->strLen + 1, str);
}
char MyString::operator[](int sub)
{
if (sub < 0 || sub >= this->strLen)
_ASSERT_EXPR(false, L"String's Subscript Out of Range!");
else
return this->pStr[sub];
}
char* MyString::operator+(int sub)
{
if (sub < 0 || sub >= this->strLen)
_ASSERT_EXPR(false, L"String's Subscript Out of Range!");
else
return this->pStr + sub;
}
bool MyString::operator==(MyString str)
{
return (strcmp(this->pStr, str.pStr) == 0);
}
bool MyString::operator!=(MyString str)
{
return (strcmp(this->pStr, str.pStr) != 0);
}
bool MyString::operator>(MyString str)
{
return (strcmp(this->pStr, str.pStr) > 0);
}
bool MyString::operator<(MyString str)
{
return (strcmp(this->pStr, str.pStr) < 0);
}
bool MyString::operator>=(MyString str)
{
return (strcmp(this->pStr, str.pStr) >= 0);
}
bool MyString::operator<=(MyString str)
{
return (strcmp(this->pStr, str.pStr) <= 0);
}
bool MyString::operator!()
{
return (strcmp(this->pStr, "") == 0);
}
MyString operator+(MyString str1, MyString str2)
{
int size = str1.strLen + str2.strLen + 1;
char* returnStr = new char[size];
strcpy_s(returnStr, size, str1.pStr);
strcat_s(returnStr, size, str2.pStr);
return MyString(returnStr);
}
MyString operator+(MyString str1, const char* str2)
{
MyString tmp(str2);
return str1 + tmp;
}
MyString operator+(const char* str1, MyString str2)
{
MyString tmp(str1);
return tmp + str2;
}
void operator+=(MyString& str1, MyString str2)
{
str1 = str1 + str2;
}
void operator+=(MyString& str1, const char* str2)
{
str1 = str1 + str2;
}
MyString operator*(MyString str, int multiplier)
{
// 乘数小于1返回空字符串(大于0小于1会转换为0)
if(multiplier < 1)
return MyString();
else
{
MyString tmp(str);
for (int i = 0; i < multiplier - 1; i++)
str += tmp;
return str;
}
}
MyString operator*(int multiplier, MyString str)
{
return str * multiplier;
}
void operator*=(MyString& str, int multiplier)
{
str = str * multiplier;
}
std::istream& operator>>(std::istream& in, MyString& str)
{
char input[1024] = { 0 };
in >> input;
str = MyString(input);
return in;
}
std::ostream& operator<<(std::ostream& out, MyString& str)
{
out << str.pStr;
return out;
}
测试程序 File: test.cpp
#include "MyString.h"
#include <iostream>
using namespace std;
int main()
{
MyString str("I Love You!");
// 重载[]测试
for (int i = 0; i < str.length(); i++)
cout << str[i];
cout << endl;
// 重载<<测试
cout << str << endl;
// 拷贝构造测试
MyString str2(str);
cout << str2 << endl;
// 一些连接的测试
str2 = str + "I Miss You!";
cout << str2 << endl;
str2 = str + str;
cout << str2 << endl;
str2 += "Hah";
cout << str2 << endl;
str2 *= 4;
cout << str2 << endl;
// 迭代器测试
MyString::Iterator it;
for (it = str2.begin(); it != str2.end(); it++)
cout << *it;
cout << endl;
return 0;
}
运行结果(测试全部通过!)
?
?时候不早了,睡觉
|