C++作业3

1.仿造string将myString类中能够实现的操作都实现一遍

代码:

#include <iostream>
#include <cstring>

class myString {
private:
    char *str;          // 记录C风格的字符串
    int size;           // 记录字符串的实际长度
    int capacity;       // 记录分配的存储空间

public:
    // 无参构造
    myString() : size(0), capacity(10)
    {
        str = new char[capacity];
        str[0] = '\0';  // 初始化为空字符串
    }

    // 有参构造
    myString(const char *s)
    {
        size = strlen(s);
        capacity = size + 1;
        str = new char[capacity];
        strcpy(str, s);
    }

    // 拷贝构造
    myString(const myString &other)
    {
        size = other.size;
        capacity = other.capacity;
        str = new char[capacity];
        strcpy(str, other.str);
    }

    // 移动构造
    myString(myString &&other) noexcept : str(other.str), size(other.size), capacity(other.capacity)
    {
        other.str = nullptr;
        other.size = 0;
        other.capacity = 0;
    }

    // 析构函数
    ~myString()
    {
        delete[] str;
    }

    // 判空函数
    bool empty() const
    {
        return size == 0;
    }

    // size函数, 返回字符数
    int getSize() const
    {
        return size;
    }

    // capacity函数,返回当前对象分配的存储空间能储存的字符数量
    int getCapacity() const
    {
        return capacity;
    }

    // clear函数清除内容
    void clear()
    {
        size = 0;
        str[0] = '\0';  // 使字符串为空
    }

    // push_back函数,后附字符到结尾
    void push_back(char c)
    {
        if (size + 1 >= capacity)
        {
            resize(capacity * 2);
        }
        str[size++] = c;
        str[size] = '\0';  // 终止字符串
    }

    // pop_back函数,删除最后一个字符
    void pop_back()
    {
        if (size > 0)
        {
            str[--size] = '\0';  // 终止字符串
        }
    }

    // append函数,后附字符到结尾
    void append(const char *s)
    {
        int len = strlen(s);
        while (size + len >= capacity)
        {
            resize(capacity * 2);
        }
        strcpy(str + size, s);
        size += len;
    }

    // c_str函数,返回字符串不可修改的C字符数组版本
    const char* c_str() const
    {
        return str;
    }

    // at函数, 访问指定字符,有边界检查
    char at(int index) const
    {
        if (index < 0 || index >= size)
        {
            throw std::out_of_range("Index out of range");
        }
        return str[index];
    }

    // operator=函数,为字符串赋值
    myString& operator=(const myString &other)
    {
        if (this != &other)
        {
            delete[] str;
            size = other.size;
            capacity = other.capacity;
            str = new char[capacity];
            strcpy(str, other.str);
        }
        return *this;
    }

    // operator+=函数,后附字符到结尾
    myString& operator+=(char c)
    {
        push_back(c);
        return *this;
    }

    // operator[]函数,访问指定字符
    char operator[](int index) const
    {
        return at(index);
    }

    // data函数,返回指向字符串首字符的指针
    char* data()
    {
        return str;
    }

private:
    // 二倍扩容
    void resize(int new_capacity)
    {
        char *new_str = new char[new_capacity];
        strcpy(new_str, str);
        delete[] str;
        str = new_str;
        capacity = new_capacity;
    }
};

// 连接字符串或字符
myString operator+(const myString &lhs, const myString &rhs)
{
    myString result(lhs);
    result.append(rhs.c_str());
    return result;
}

// 比较操作符
bool operator==(const myString &L, const myString &R)
{
    return strcmp(L.c_str(), R.c_str()) == 0;
}

bool operator!=(const myString &L, const myString &R)
{
    return strcmp(L.c_str(), R.c_str()) != 0;
}

bool operator<(const myString &L, const myString &R)
{
    return R.c_str() < L.c_str();
}

bool operator>(const myString &L, const myString &R)
{
    return R.c_str() > L.c_str();
}

bool operator<=(const myString &L, const myString &R)
{
    return !(R.c_str() < L.c_str());
}

bool operator>=(const myString &L, const myString &R)
{
    return !(R.c_str() > L.c_str());
}

// 流输入输出
std::ostream& operator<<(std::ostream &os, const myString &s)
{
    os << s.c_str();
    return os;
}

std::istream& operator>>(std::istream &is, myString &s)
{
    char buffer[1024];
    is >> buffer;  // 假设输入的字符串不超过1023个字符
    s = myString(buffer);
    return is;
}

int main()
{
    myString str1("Hello");
    myString str2(" World");
    myString str3 = str1 + str2;

    // 测试输出
    std::cout << "str3: " << str3 << std::endl;

    // 测试大小和容量
    std::cout << "Size of str3: " << str3.getSize() << std::endl;
    std::cout << "Capacity of str3: " << str3.getCapacity() << std::endl;

    // 测试 push_back 和 pop_back
    str3.push_back('!');
    std::cout << "After push_back: " << str3 << std::endl;
    str3.pop_back();
    std::cout << "After pop_back: " << str3 << std::endl;

    // 测试比较操作符
    myString str4("Hello World");
    std::cout << "str3 == str4: " << (str3 == str4) << std::endl;
    std::cout << "str3 != str4: " << (str3 != str4) << std::endl;

    // 测试清空
    str3.clear();
    std::cout << "After clear, str3: '" << str3 << "'" << std::endl;

    // 测试 at 函数
    try
    {
        std::cout << "First character of str4: " << str4.at(0) << std::endl;
        std::cout << "Character at index 20: " << str4.at(20) << std::endl;
    }
    catch (const std::out_of_range &e)
    {
        std::cout << "Error: " << e.what() << std::endl;
    }

    return 0;
}

运行结果:

2.思维导图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值