2
#include <iostream>
#include <cstring>
using namespace std;
class MyString {
private:
char *str; // 记录C风格的字符串
int size; // 记录字符串的实际长度
public:
// 定义无参构造
MyString() : size(0) {
str = new char[1];
str[0] = '\0';
cout << "MyString::无参构造,this = " << this << endl;
}
// 定义有参构造
MyString(const char *s) {
size = strlen(s);
str = new char[size + 1];
strcpy(str, s);
cout << "MyString::有参构造,this = " << this << endl;
}
// 定义拷贝构造函数
MyString(const MyString &other) : str(new char[other.size + 1]), size(other.size) {
strcpy(this->str, other.str);
cout << "MyString::拷贝构造,this = " << this << endl;
}
// 定义拷贝赋值函数
MyString &operator=(const MyString &other) {
if (this != &other) { // 防止自己给自己赋值
delete[] str;
size = other.size;
str = new char[size + 1];
strcpy(str, other.str);
}
cout << "MyString::拷贝赋值,this = " << this << endl;
return *this;
}
// 定义析构函数
~MyString() {
delete[] str;
cout << "MyString::析构函数,this = " << this << endl;
}
// 判空函数
bool empty() const {
return size == 0 || str[0] == '\0';
}
// size函数
int my_size() const {
return size;
}
// c_str函数
const char *c_str() const {
return str;
}
// at函数
char &at(int pos) {
if (pos < 0 || pos >= size) throw std::out_of_range("Index out of range");
return str[pos];
}
// [] 运算符重载
char &operator[](int pos) {
if (pos < 0 || pos >= size) throw std::out_of_range("Index out of range");
return str[pos];
}
// + 运算符重载
MyString operator+(const MyString &other) const {
char *temp = new char[size + other.size + 1];
strcpy(temp, str);
strcat(temp, other.str);
MyString result(temp);
delete[] temp;
return result;
}
// += 运算符重载
MyString &operator+=(const MyString &other) {
char *temp = new char[size + other.size + 1];
strcpy(temp, str);
strcat(temp, other.str);
delete[] str;
str = temp;
size += other.size;
return *this;
}
// += 运算符重载(加等一个字符)
MyString &operator+=(char ch) {
char *temp = new char[size + 2];
strcpy(temp, str);
temp[size] = ch;
temp[size + 1] = '\0';
delete[] str;
str = temp;
++size;
return *this;
}
// == 运算符重载
bool operator==(const MyString &other) const {
return strcmp(str, other.str) == 0;
}
// != 运算符重载
bool operator!=(const MyString &other) const {
return !(*this == other);
}
// > 运算符重载
bool operator>(const MyString &other) const {
return strcmp(str, other.str) > 0;
}
// < 运算符重载
bool operator<(const MyString &other) const {
return strcmp(str, other.str) < 0;
}
// >= 运算符重载
bool operator>=(const MyString &other) const {
return !(*this < other);
}
// <= 运算符重载
bool operator<=(const MyString &other) const {
return !(*this > other);
}
// << 运算符重载
friend ostream &operator<<(ostream &os, const MyString &ms) {
os << ms.str;
return os;
}
// >> 运算符重载
friend istream &operator>>(istream &is, MyString &ms) {
char buffer[1024];
is >> buffer;
ms = MyString(buffer);
return is;
}
};
int main() {
// 调用无参构造
MyString s1;
cout << "my_size = " << s1.my_size() << " str1 = " << s1.c_str() << endl;
// 调用有参构造
MyString s2("XHJ");
cout << "my_size = " << s2.my_size() << " str2 = " << s2.c_str() << endl;
// 调用拷贝构造
MyString s3(s2);
cout << "my_size = " << s3.my_size() << " str3 = " << s3.c_str() << endl;
// 调用at函数
cout << "第一个字符:" << s3.at(0) << endl;
cout << "第二个字符:" << s3.at(1) << endl;
cout << "第三个字符:" << s3.at(2) << endl;
// 调用有参构造
MyString s4("");
cout << "my_size = " << s4.my_size() << " str4 = " << s4.c_str() << endl;
// 调用empty函数
// 判断字符串s3是否为空
if (s3.empty())
cout << "s3字符串为空!" << endl;
else
cout << "s3字符串不为空!" << endl;
// 判断字符串s4是否为空
if (s4.empty())
cout << "s4字符串为空!" << endl;
else
cout << "s4字符串不为空!" << endl;
// 测试 += 运算符
s4 += "XHJ";
cout << "my_size = " << s4.my_size() << " str4 = " << s4.c_str() << endl;
// 测试 + 运算符
MyString s5 = s2 + s3;
cout << "s5 = " << s5.c_str() << endl;
// 测试比较运算符
cout << "s2 == s3: " << (s2 == s3) << endl;
cout << "s2 != s3: " << (s2 != s3) << endl;
cout << "s2 > s3: " << (s2 > s3) << endl;
cout << "s2 < s3: " << (s2 < s3) << endl;
cout << "s2 >= s3: " << (s2 >= s3) << endl;
cout << "s2 <= s3: " << (s2 <= s3) << endl;
// 测试流操作符
MyString s6;
cout << "请输入一个字符串: ";
cin >> s6;
cout << "你输入的字符串是: " << s6 << endl;
return 0;
}