自学内容网 自学内容网

9.23 C++类中的特殊内容

仿照string类,实现myString

//my_string.cpp
#include "my_string.h"
#include <iostream>
#include <cstring>

using namespace std;



    My_string::My_string():size(15)
    {
        this->ptr = new char[size];
        this->ptr[0] = '\0';            //表示串为空串
        this->len = 0;
    }


    //有参构造
    My_string::My_string(const char* src)
    {
        this->len=strlen(src);
        this->size=len+1;
        this->ptr=new char[size];
        strcpy(this->ptr,src);
    }
    My_string::My_string(int num, char value):size(num+1),len(num)
    {
        this->ptr=new char[size];
        memset(this->ptr,value,num);
        this->ptr[num]='\0';
    }
    //拷贝构造
    My_string::My_string(const My_string &other)
    {
        this->len=other.len;
        this->size=other.size;
        this->ptr=new char[size];
        strcpy(this->ptr,other.ptr);
    }
    //拷贝赋值
    My_string &My_string::operator=(const My_string &other)
    {
        if(this!=&other)
        {
            delete[] this->ptr;
            this->len=other.len;
            this->size=other.size;
            this->ptr=new char[size];
            strcpy(this->ptr,other.ptr);
        }
        return *this;
    }
    //析构函数
    My_string::~My_string()
    {
        delete [] this->ptr;
    }
    //判空
    bool My_string::empty() const
    {
        return len==0;
    }
    //尾插
    void My_string::push_back(char value)
    {
        if((len+1)>=size)
        {
            size=size*2;
        }
        this->ptr[len++]=value;
        this->ptr[len]='\0';
    }
    //尾删
    void My_string::pop_back()
    {
        if(len>0)
        {
            this->ptr[len-1]='\0';
            len--;
        }
    }
    //at函数实现
    char &My_string::at(int index)
    {
        if(index>=0&&index<len)
        {
            return this->ptr[index];
        }
    }
    //清空函数
    void My_string::clear()
    {
        this->len=0;
        this->ptr[0]='\0';
    }
    //返回C风格字符串
    char *My_string::data()
    {
        return this->ptr;
    }
    //返回实际长度
    int My_string::get_length()
    {
        return this->len;
    }
    //返回当前最大容量
    int My_string::get_size()
    {
        return this->size;
    }


    //君子函数:二倍扩容
//my_string.h
#ifndef MY_STRING_H
#define MY_STRING_H

using namespace std;


class My_string
{
private:
    char *ptr;         //指向字符数组的指针
    int size;           //字符串的最大容量
    int len;            //字符串当前容量


public:
    //无参构造
    My_string();

    //有参构造
    My_string(const char* src);
    My_string(int num, char value);
    //拷贝构造
    My_string(const My_string &other);
    //拷贝赋值
    My_string &operator=(const My_string &other);
    //析构函数
    ~My_string();
    //判空
    bool empty() const;
    //尾插
    void push_back(char value);
    //尾删
    void pop_back();
    //at函数实现
    char &at(int index);
    //清空函数
    void clear();
    //返回C风格字符串
    char *data();
    //返回实际长度
    int get_length();
    //返回当前最大容量
    int get_size();
    //君子函数:二倍扩容

};

#endif // MY_STRING_H
//main.cpp
#include <iostream>
#include <cstring>
#include "my_string.h"
using namespace std;

int main()
{
    My_string s("Hello");
    cout << "String= " << s.data() << endl;
    cout << "Len= " << s.get_length() << endl;

    s.push_back('W');
    cout << "尾插: " << s.data() << endl;

    s.pop_back();
    cout << "尾删: " << s.data() << endl;

    cout << "at(3)=: " << s.at(3) << endl;

    s.clear();
    cout << "清空: " << s.data() << endl<<"Len=: " << s.get_length() << endl;



    s=My_string(5,'A');
    cout<<s.data()<<endl;

    return 0;

}


原文地址:https://blog.csdn.net/m0_68908707/article/details/142468435

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!