坐牢第三十三天(手搓string)

 一.mystring类 

#include <iostream>
#include <cstring> // 引入cstring以使用memcpy
using namespace std;
class myString
{
    char *str;       // 记录c风格的字符串
    int size;        // 记录字符串的实际长度
    int my_capacity; // 记录最大容量
public:
    // 无参构造
    myString();
    // 有参构造
    myString(const char *s);
    // 深度构造
    myString(const myString &S);
    // 判空函数
    bool empty();
    // size函数
    int get_size();
    // length函数
    int get_length();
    // 当前对象分配的储存空间能保存的字符数量
    int capacity1() const;
    // c_str函数
    const char *c_str();
    // at函数
    char &at(int index);
    // data函数
    char *data();
    // const data函数
    const char *data() const;
    // 定义成员函数版完成等于运算符
    myString &operator=(const myString &other);
    // 定义成员函数版完成加法运算符
    myString operator+(const myString &other);
    // 定义成员函数版完成加等于运算符
    myString &operator+=(const myString &other);
    // 定义成员函数版完成等等于运算符
    bool operator==(const myString &other)const;
    // 定义成员函数版完成[]运算符//可修改
    char &operator[](int pos);
    // 后附字符到结尾
    void push_back(char c);
    // 移除末尾字符
    void pop_back();
    // append末尾加字符或字符串函数
    void append(const char *new_str);
    // 扩容函数
    void reserve(int new_capacity);
    // 析构函数
    ~myString();
    // 输入
    friend istream &operator>>(istream &L, myString &S);
    // 输出
    friend ostream &operator<<(ostream &L, myString &S);
};
// 全局函数输出重载
istream &operator>>(istream &L, myString &S)
{
    char temp[1024];
    L >> temp;
    S.size = strlen(temp);
    S.str = new char[+1];
    strcpy(S.str, temp);
    return L;
}
// 全局函数输入重载
ostream &operator<<(ostream &L, myString &S)
{
    L << S.str;
    return L;
}
// 无参构造
myString::myString() : size(10)
{
    str = new char[size]; // 构造一个长度为10的字符串
}
// 有参构造
myString::myString(const char *s)
{
    size = strlen(s);
    my_capacity = size;
    str = new char[size + 1];
    strcpy(str, s);
}
// 深拷贝
myString::myString(const myString &other)
{
    size=other.size;
    str= new char[size+1];
    strcpy(str,other.str);
    my_capacity=size;
}
// 析构函数
myString::~myString()
{
    delete[] str;
    size = 0;
    my_capacity = 0;
    cout << "析构函数" << endl;
}
// 判空函数
bool myString::empty()
{
    if (size == 0)
    {
        return true;
    }
    return false;
}
// size函数
int myString::get_size()
{
    return size;
}
// length函数
int myString::get_length()
{
    return size;
}
// 当前对象分配的储存空间能保存的字符数量
int myString::capacity1() const
{
    return my_capacity;
}
// c_str函数
const char *myString::c_str()
{
    if (str == NULL || empty())
    {
        cout << "转换失败" << endl;
    }
    char *qtr = str;
    cout << "转换成功:";
    return qtr;
}
// at函数
char &myString::at(int index)
{
    if (str == NULL || empty() || index < 0 || index >= size)
    {
        cout << "查看失败";
    }
    return str[index];
}
// 二倍扩容
void myString::reserve(int new_capacity)
{
    if (new_capacity > my_capacity)
    {
        char *new_str = new char[new_capacity + 1];
        strcpy(new_str, str);
        delete[] str;
        str = new_str;
        my_capacity = new_capacity;
    }
}
// data函数
char *myString::data()
{
    return str;
}
// const data函数
const char *myString::data() const
{
    return str;
}
// 定义成员函数版完成等于运算符
myString &myString::operator=(const myString &other)
{
    if (this == &other)
    {
        return *this;
    }
    delete[] str;
    size = other.size;
    str = new char[size];
    strcpy(str, other.str);
    // cout << *this <<endl;
    // cout << str <<endl;
    return *this;
}
// 定义成员函数版完成加等于运算符
myString &myString::operator+=(const myString &other)
{
    int new_size = size + other.size;
    if (new_size > my_capacity)
    {
        reserve(new_size);
    }
    strcat(str, other.str);
    size = new_size;
    return *this;
}
// 定义成员函数版完成等等于运算符
bool myString::operator==(const myString &other)const
{
    if (size==other.size && !strcmp(str,other.str))
    {
        return true;
    }
    return false;
}
// 定义成员函数版完成加法运算符
myString myString::operator+(const myString &other)
{
    myString temp = *this;
    temp += other;
    return temp;
}
// 定义成员函数版完成[]运算符
char &myString::operator[](int pos)
{
    if (pos < 0 || pos >= size)
    {
        cout << "查看失败";
    }
    return str[pos];
}
// 后附字符到结尾
void myString::push_back(char c)
{
    if (size == my_capacity)
    {
        reserve(my_capacity * 2);
    }
    str[size++] = c;
    str[size] = '\0';
}
// 移除末尾字符
void myString::pop_back()
{
    if (size > 0)
    {
        size--;
        str[size] = '\0';
    }
}
// append末尾加字符或字符串函数
void myString::append(const char *new_str)
{
    int new_size = strlen(new_str);
    if (size + new_size > my_capacity)
    {
        reserve(size + new_size);
    }
    strcat(str, new_str);
    size += new_size;
}
int main()
{
    myString s4("Hello");
    myString s5("Word");

    myString s6;
    s6=s5;
    cout << s6 << endl;
    cout << "****************************" << endl;
    if (s6==s5)
    {
        cout << "yes" << endl;
    }
    else
    {
        cout << "no" << endl;
    }
    return 0;
}

二.思维导图

  • 11
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值