MyString字符串类
包括:有参构造、拷贝构造、移动构造、析构、拷贝赋值和移动赋值。
MyString.h文件
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
using namespace std;
class MyString
{
private:
char* str;
unsigned int MaxSize;
unsigned int Length = 0;
public:
MyString() = default; /*默认构造函数*/
MyString(unsigned int m); /*构造函数,空字符串*/
MyString(const char* S); /*构造函数,写入字符串*/
MyString(const MyString& S); /*拷贝构造函数*/
MyString( MyString && S) noexcept; /*移动构造函数*/
~MyString(); /*析构函数*/
/*获取字符串长度*/
unsigned int GetLength();
/*字符串输出*/
friend ostream& operator<< (ostream& Output, MyString& S);
/*字符串输入*/
friend istream& operator>> (istream& Input, MyString& S);
/*获取单个字符*/
char & operator[] (unsigned int i);
/*添加或修改单个字符*/
void at(char ch);
void at(unsigned int i, char ch);
/*字符串比较*/
bool operator== (MyString & S2);
bool operator> (MyString & S2);
bool operator< (MyString & S2);
/*拷贝赋值运算符重载*/
MyString& operator= (MyString& S);
/*移动赋值运算符重载*/
MyString& operator= (MyString&& S) noexcept;
/*字符串链接函数*/
MyString operator+ (MyString& S2);
};
#endif
MyString.cpp文件
#include "MyString.h"
/*构造函数,空字符串*/
MyString::MyString(unsigned int m) :MaxSize(m)
{
MaxSize += 1;
str = new char[MaxSize] {};
str[MaxSize-1] = '\0';
cout << "空构造函数:";
}
/*构造函数,写入字符串*/
MyString::MyString(const char* S)
{
const char* p = S;
while (*p != '\0')
{
p++;
Length++;
}
MaxSize = Length + 1;
str = new char[MaxSize] {};
for (unsigned int i = 0; i < Length; i++)
{
str[i] = S[i];
}
str[Length] = '\0';
cout << "字符串构造函数:" ;
}
/*拷贝构造函数*/
MyString::MyString(const MyString& S)
{
Length = S.Length;
MaxSize = S.MaxSize;
str = new char[MaxSize] {};
for (unsigned int i = 0; i < MaxSize; i++)
{
str[i] = S.str[i];
}
cout << "拷贝构造函数:";
}
/*移动构造函数*/
MyString::MyString(MyString&& S) noexcept
{
Length = S.Length;
MaxSize = S.MaxSize;
str =S.str;
S.str = nullptr;
cout << "移动构造函数:";
}
/*析构函数*/
MyString::~MyString()
{
delete[] str;
cout << "析构函数" << endl;
}
/*返回字符串长度*/
unsigned int MyString::GetLength()
{
return Length;
}
/*字符串输入*/
ostream& operator<< (ostream& Output, MyString& S)
{
Output << S.str;
return Output;
}
/*字符串输出*/
istream& operator>> (istream& Input, MyString& S)
{
Input >> S.str;
return Input;
}
/*获取单个字符*/
char & MyString::operator[] (unsigned int i)
{
return str[i];
}
/*添加或修改单个字符*/
void MyString::at(char ch)
{
if (Length < MaxSize - 1)
str[Length] = ch;
Length++;
}
void MyString::at(unsigned int i, char ch)
{
if( i < Length)
str[i] = ch;
}
/*字符串比较函数*/
bool MyString::operator== (MyString & S2)
{
char* str1 = str;
char* str2 = S2.str;
while (*str1 != '\0' || *str2 != '\0')
{
if (*str1 > *str2)
return false;
if (*str1 < *str2)
return false;
str1++;
str2++;
}
return true;
}
bool MyString::operator> (MyString & S2)
{
char* str1 = str;
char* str2 = S2.str;
while (*str1 != '\0' || *str2 != '\0')
{
if (*str1 > *str2)
return true;
if (*str1 < *str2)
return false;
str1++;
str2++;
}
return false;
}
bool MyString::operator< (MyString & S2)
{
char* str1 = str;
char* str2 = S2.str;
while (*str != '\0' || *str2 != '\0')
{
if (*str1 > *str2)
return false;
if (*str1 < *str2)
return true;
str1++;
str2++;
}
return false;
}
/*赋值运算符重载*/
MyString & MyString::operator= (MyString& S)
{
delete[] str;
Length = S.Length;
MaxSize = S.MaxSize;
str = new char[MaxSize] {};
for (unsigned int i = 0; i < MaxSize; i++)
{
str[i] = S.str[i];
}
cout << "拷贝赋值运算符重载:";
return *this;
}
/*移动赋值运算符重载*/
MyString& MyString::operator= (MyString&& S) noexcept
{
delete[] str;
Length = S.Length;
MaxSize = S.MaxSize;
str = S.str;
S.str = nullptr;
cout << "移动赋值运算符重载:";
return *this;
}
/*字符串链接函数*/
MyString MyString::operator+ (MyString& S2)
{
MaxSize += S2.MaxSize;
char* temp = new char[MaxSize] {};
for (unsigned int i = 0; i < Length; i++)
{
temp[i] = str[i];
}
temp[Length] = ' ';
Length += 1;
for (unsigned int i = Length; i < MaxSize; i++)
{
temp[i] = S2.str[i - Length];
}
Length += S2.Length;
delete[]str;
str = temp;
return *this;
}
main.cpp测试:
#include <iostream>
using namespace std;
#include "MyString.h"
MyString GetString()
{
// MyString n = "1234";
// MyString m(2);
MyString temp = "C++";
return temp;
}
int main()
{
MyString S1{ "Hello World!" }; //构造函数,字符串初始化
cout << S1 <<' '<< S1.GetLength() << endl;
MyString S2{ S1 } ; //拷贝构造函数
cout << S2 <<' '<< S2.GetLength() << endl;
MyString S3{ 3 }; //构造函数,空字符串
S3.at('A');
S3.at('B');
S3.at('C');
cout << S3 <<' '<< S3.GetLength() << endl;
S3[2] = 'Z';
cout << S3[2] << endl;
S1 = S3; /拷贝赋值
cout << S1 << ' ' << S1.GetLength() << endl;
cout << (S1 < S2) << endl;
cout << (S1 > S2) << endl;
cout << (S1 == S2) << endl;
MyString S4{ move(S1) }; //移动构造函数
cout << S4 << ' ' << S4.GetLength() << endl;
S4 = GetString(); //移动赋值
cout << S4 << ' ' << S4.GetLength() << endl;
S4 = S2 + S3;
cout << S4 << ' ' << S4.GetLength() << endl;
}