C++(9.24)

头文件

cpp 复制代码
#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

源文件

cpp 复制代码
#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;
}

主函数

cpp 复制代码
#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;
}
相关推荐
Sylvia33.18 分钟前
火星数据体育API|一站式接入足球篮球电竞等18+项目实时数据
java·开发语言·python·websocket·游戏
Data_Journal43 分钟前
使用 AutoScraper 进行网页抓取:分步教程
大数据·开发语言·数据库·python·scrapy
CodeRecycle44 分钟前
Python 自动配置 pip 支持库(通过 Windows Bat 脚本)
算法
yume_sibai1 小时前
12-Rust 性能优化指南(性能分析 + 内存优化 + SIMD + 并发优化 + 编译器优化 + 基准测试)
开发语言·性能优化·rust
liliangcsdn1 小时前
特质偏度因子的计算示例和分析
算法
萧瑟余晖1 小时前
Hibernate 核心原理与架构详解
开发语言·架构·hibernate
爱喝水的鱼丶1 小时前
SAP-ABAP:SAP MM 模块物料主数据批量导入与增强开发:字段扩展、校验逻辑与批量导入工具开发
开发语言·性能优化·sap·abap·增强·经验交流·mm
geovindu1 小时前
rust:Builder Pattern
开发语言·设计模式·rust·建造者模式
白远山1 小时前
健身场馆无人自动化解决方案:从架构到落地实践
java·开发语言·数据库·数据挖掘·需求分析
荆棘鸟智能2 小时前
无人机遥感图像实时拼接算法工程实践:从特征提取到全景融合的完整技术链路
算法·无人机