C++(9.24)
头文件
#ifndef MY_STRING_H
#define MY_STRING_H
#include <iostream>
class My_string
{
private:
char *ptr; // 指向字符数组的指针
int size; // 字符串的最大容量
int len; // 字符串的当前长度
public:
My_string();
My_string(const char *src);
My_string(const My_string &src);
My_string& operator=(const My_string &src);
~My_string();
void push_back(char value);
void pop_back();
char &at(int index);
void clear();
char *data() const;
int get_length() const;
int get_size() const;
bool is_empty() const;
// 运算符重载
My_string operator+(const My_string &other) const;
My_string operator+(char value) const;
char &operator[](int index);
bool operator>(const My_string &other) const;
bool operator<(const My_string &other) const;
bool operator==(const My_string &other) const;
bool operator>=(const My_string &other) const;
bool operator<=(const My_string &other) const;
bool operator!=(const My_string &other) const;
My_string& operator+=(const My_string &other);
My_string& operator+=(char value);
friend std::ostream& operator<<(std::ostream &out, const My_string &str);
friend std::istream& operator>>(std::istream &in, My_string &str);
};
#endif // MY_STRING_H
源文件
#include "My_string.h"
#include <iostream>
#include <cstring>
#include <stdexcept> // 用于异常处理
using namespace std;
// 默认构造函数
My_string::My_string() : size(15), len(0)
{
this->ptr = new char[size];
this->ptr[0] = '\0';
}
// 带参数的构造函数
My_string::My_string(const char *src)
{
this->len = strlen(src);
this->size = this->len + 1;
this->ptr = new char[size];
strcpy(this->ptr, src);
}
// 拷贝构造函数
My_string::My_string(const My_string &src)
{
this->len = src.len;
this->size = src.size;
this->ptr = new char[size];
strcpy(this->ptr, src.ptr);
}
// 赋值运算符重载
My_string& My_string::operator=(const My_string &src)
{
if (this == &src)
return *this;
delete[] this->ptr;
this->len = src.len;
this->size = src.size;
this->ptr = new char[size];
strcpy(this->ptr, src.ptr);
return *this;
}
// 尾部插入
void My_string::push_back(char value)
{
if (len + 1 >= size)
{
size *= 2;
char *new_ptr = new char[size];
strcpy(new_ptr, ptr);
delete[] ptr;
ptr = new_ptr;
}
ptr[len++] = value;
ptr[len] = '\0';
}
// 尾部删除
void My_string::pop_back()
{
if (len > 0)
{
len--;
ptr[len] = '\0';
}
}
// 通过索引访问字符
char &My_string::at(int index)
{
if (index < 0 || index >= len)
{
throw out_of_range("Index out of range");
}
return ptr[index];
}
// 清空字符串
void My_string::clear()
{
len = 0;
ptr[0] = '\0';
}
// 返回 C 风格字符串
char *My_string::data() const
{
return ptr;
}
// 返回字符串长度
int My_string::get_length() const
{
return len;
}
// 返回最大容量
int My_string::get_size() const
{
return size;
}
// 判空函数
bool My_string::is_empty() const
{
return len == 0;
}
// 运算符重载实现
My_string My_string::operator+(const My_string &other) const
{
My_string result;
result.size = len + other.len;
result.ptr = new char[result.size + 1];
strcpy(result.ptr, ptr); // 复制当前字符串
strcat(result.ptr, other.ptr); // 追加另一个字符串
result.len = len + other.len; // 更新长度
return result;
}
My_string My_string::operator+(char value) const
{
My_string result(*this); // 拷贝当前对象
result.push_back(value); // 添加字符
return result;
}
char &My_string::operator[](int index)
{
return at(index); // 使用 at 函数检查边界
}
bool My_string::operator>(const My_string &other) const
{
return strcmp(ptr, other.ptr) > 0;
}
bool My_string::operator<(const My_string &other) const
{
return strcmp(ptr, other.ptr) < 0;
}
bool My_string::operator==(const My_string &other) const
{
return strcmp(ptr, other.ptr) == 0;
}
bool My_string::operator>=(const My_string &other) const
{
return *this >= other;
}
bool My_string::operator<=(const My_string &other) const
{
return this <= other;
}
bool My_string::operator!=(const My_string &other) const
{
return *this != other;
}
My_string& My_string::operator+=(const My_string &other)
{
this->push_back('\0'); // 确保字符串末尾有结束符
strcat(this->ptr, other.ptr);
len += other.len; // 更新长度
return *this;
}
My_string& My_string::operator+=(char value)
{
push_back(value); // 尾部添加字符
return *this;
}
// 输入输出运算符重载
ostream& operator<<(ostream &out, const My_string &str)
{
out << str.ptr;
return out;
}
istream& operator>>(istream &in, My_string &str)
{
char buffer[1024];
in >> buffer;
str.clear(); // 清空当前字符串
str.push_back('\0'); // 先设置为结束符
for (int i = 0; buffer[i] != '\0'; i++)
{
str.push_back(buffer[i]);
}
return in;
}
// 析构函数
My_string::~My_string()
{
delete ptr;
}
主函数
#include "My_string.h"
#include <iostream>
using namespace std;
int main()
{
My_string s1("hello");
My_string s2(" world");
My_string s3 = s1 + s2; // 字符串连接
cout << "s3: " << s3 << endl; // 输出 "hello world"
s1 += '!';
cout << "s1 after +=: " << s1 << endl; // 输出 "hello!"
// 测试 []
cout << "s1[1]: " << s1[1] << endl; // 输出 'e'
cout << "s1 > s2: " << (s1 > s2) << endl; // 输出 1
cout << "s1 < s2: " << (s1 < s2) << endl; // 输出 0
cout << "s1 == s2: " << (s1 == s2) << endl; // 输出 0
My_string s4;
cout << "Enter a string: ";
cin >> s4; // 输入字符串
cout << "You entered: " << s4 << endl;
return 0;
}