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;
}
相关推荐
Yhame.2 分钟前
【使用层次序列构建二叉树(数据结构C)】
c语言·开发语言·数据结构
言之。8 分钟前
【Go语言】RPC 使用指南(初学者版)
开发语言·rpc·golang
雾月5526 分钟前
LeetCode 1292 元素和小于等于阈值的正方形的最大边长
java·数据结构·算法·leetcode·职场和发展
投笔丶从戎1 小时前
Kotlin Multiplatform--01:项目结构基础
android·开发语言·kotlin
OpenC++1 小时前
【C++QT】Buttons 按钮控件详解
c++·经验分享·qt·leetcode·microsoft
杜小暑1 小时前
动态内存管理
c语言·开发语言·动态内存管理
想不明白的过度思考者1 小时前
Java从入门到“放弃”(精通)之旅——JavaSE终篇(异常)
java·开发语言
YuforiaCode2 小时前
第十二届蓝桥杯 2021 C/C++组 直线
c语言·c++·蓝桥杯
我真的不会C2 小时前
QT窗口相关控件及其属性
开发语言·qt
CodeCraft Studio2 小时前
Excel处理控件Aspose.Cells教程:使用 Python 在 Excel 中进行数据验
开发语言·python·excel