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;elsecout << "s3字符串不为空!" << endl;// 判断字符串s4是否为空if (s4.empty())cout << "s4字符串为空!" << endl;elsecout << "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;
}