#include <iostream>
#include <cstring>
#include <stdexcept> // For std::out_of_range
class Mystring
{
public:
// 默认构造函数
Mystring() : str(nullptr), len(0) {}
// 有参构造函数
Mystring(const char* s)
{
len = strlen(s);
str = new char[len + 1]; // 分配空间,并多留一个位置给'\0'
strcpy(str, s);
}
// 拷贝构造函数
Mystring(const Mystring& other)
{
len = other.len;
str = new char[len + 1];
strcpy(str, other.str);
}
// 拷贝赋值操作符
Mystring& operator=(const Mystring& other)
{
if (this == &other)
{
return *this; // 检查自赋值
}
delete[] str; // 删除旧的字符串数据
len = other.len;
str = new char[len + 1];
strcpy(str, other.str);
return *this;
}
// 析构函数
~Mystring()
{
delete[] str;
}
// 获取字符串的C风格表示
const char* c_str() const
{
return str;
}
// 获取字符串长度
int size() const
{
return len;
}
// 获取字符串长度
int length() const
{
return len;
}
// 判断字符串是否为空
bool empty() const
{
return len == 0;
}
// 获取字符串中某个位置的字符
char& at(int index)
{
if (index < 0 || index >= len)
{
throw std::out_of_range("Index out of range");
}
return str[index];
}
// 常量版本的at函数
const char& at(int index) const
{
if (index < 0 || index >= len)
{
throw std::out_of_range("Index out of range");
}
return str[index];
}
// 重载 + 运算符,用于连接两个 Mystring 对象
Mystring operator+(const Mystring& other) const
{
int new_len = len + other.len;
char* new_str = new char[new_len + 1];
strcpy(new_str, str);
strcat(new_str, other.str);
Mystring temp;
temp.str = new_str;
temp.len = new_len;
return temp;
}
// 重载 == 运算符
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 strcmp(str, other.str) > 0;
}
// 重载 >= 运算符
bool operator>=(const Mystring& other) const
{
return strcmp(str, other.str) >= 0;
}
// 模板函数:插入到输出流
template<typename T>
friend std::ostream& operator<<(std::ostream& os, const Mystring& obj);
// 模板函数:从输入流提取字符串
template<typename T>
friend std::istream& operator>>(std::istream& is, Mystring& obj);
private:
char* str; // 指向字符串的指针
int len; // 字符串长度
};
// 模板函数:插入到输出流
template<typename T>
std::ostream& operator<<(std::ostream& os, const Mystring& obj)
{
os << obj.c_str();
return os;
}
// 模板函数:从输入流提取字符串
template<typename T>
std::istream& operator>>(std::istream& is, Mystring& obj)
{
// 读取输入流中的内容
char buffer[1024]; // 假设最大输入长度为1023字符
is.getline(buffer, 1024);
// 重新分配内存并复制数据
obj.len = strlen(buffer);
delete[] obj.str; // 删除旧的字符串数据
obj.str = new char[obj.len + 1];
strcpy(obj.str, buffer);
return is;
}
int main() {
Mystring s1("Hello");
Mystring s2("World");
Mystring s3;
s3 = s1 + s2; // 使用 + 运算符连接字符串
std::cout << "s1: " << s1 << ", size: " << s1.size() << std::endl;//测试输出
std::cout << "s2: " << s2 << ", size: " << s2.size() << std::endl;//
std::cout << "s3 (s1 + s2): " << s3 << ", size: " << s3.size() << std::endl;
std::cout << "s1 == s2? " << (s1 == s2 ? "Yes" : "No") << std::endl;
std::cout << "s1 != s2? " << (s1 != s2 ? "Yes" : "No") << std::endl;
std::cout << "s1 < s2? " << (s1 < s2 ? "Yes" : "No") << std::endl;
std::cout << "s1 <= s2? " << (s1 <= s2 ? "Yes" : "No") << std::endl;
std::cout << "s1 > s2? " << (s1 > s2 ? "Yes" : "No") << std::endl;
std::cout << "s1 >= s2? " << (s1 >= s2 ? "Yes" : "No") << std::endl;
std::cout << "Enter a new string for s3: ";
std::cin >> s3;
std::cout << "Updated s3: " << s3 << std::endl;
return 0;
}
标签:const,进阶,len,return,other,str,Mystring,20240903mystring
From: https://blog.csdn.net/qq_65171301/article/details/141869436