c++9月24日

1,My_string

头文件

#ifndef MY_STRINGHEAD_H
#define MY_STRINGHEAD_H


#include <iostream>
#include <cstring>
using namespace std;

class My_string
{
private:
    char *ptr;//指向字符数组的指针
    int size;//字符数组的最大容量
    int len ;//当前字符串的长度
public:
    My_string();
    My_string(const char *src);//有参构造
    My_string(const My_string & other);//拷贝构造
    My_string & operator=(const My_string &other);
    ~My_string();//析构函数
    bool My_empty();//判空
    void push_back(char value);//尾插
    void pop_back();//尾删
    char * data ();
    char &at(int index);
    int get_len();
    int get_size();
    void Double_String();//双倍扩容
    void show();
    My_string operator+(const My_string &other);
    bool operator>(const My_string &other);
    My_string &operator+=(const My_string &other);
    friend ostream &operator<<(ostream &s,const My_string &other);
    char & operator[](int idnex);
};

#endif // MY_STRINGHEAD_H

功能函数

#include "my_stringhead.h"
My_string::My_string():ptr(nullptr),size(0),len(0){}
//有参构造
My_string::My_string(const char *src)
{
    this->size = strlen(src);
    this->len=this->size;
    this->ptr = new char[this->size+1];
    strcpy(ptr,src);
}
//拷贝构造
My_string::My_string(const My_string & other):size(other.size),len(other.len)
{
    this->ptr = new char[this->size+1];
    strcpy(this->ptr,other.ptr);
}
//拷贝赋值
My_string &My_string::operator=(const My_string &other)
{
    if(this!=&other)
    {
        delete []ptr;
        this->size = other.size;
        this->len = other.len;
        this->ptr = new char[this->size+1];
        strcpy(this->ptr,other.ptr);
    }
    return *this;
}
My_string::~My_string()//析构函数
{
    delete []ptr;
}
bool My_string:: My_empty()//判空
{
    return len==0;
}
void My_string:: push_back(char value)//尾插
{
    this->size++;
    char *Newptr=new char[this->size+1];
    strcpy(Newptr,ptr);
    delete this->ptr;
    this->ptr=Newptr;
    this->ptr[this->len]=value;
    this->len++;
    this->ptr[this->len]='\0';
}
void My_string::pop_back()//尾删
{
    if(len>0)
    {
        this->len--;
        this->ptr[this->len]='\0';
    }
}
char & My_string::at(int index)
{
    if(index>=0&&index<this->len)
    {
        return this->ptr[index];
    }
    else
    {
        cout<<"输入不合法"<<endl;
        exit(-1);
    }
}
char *My_string::data ()//转换为c风格字符串
{
    return this->ptr;
}
int  My_string::get_len()
{
    return this->len;
}
int My_string:: get_size()
{
    return this->len;
}
void My_string::Double_String()//双倍扩容
{
    if(this->len+1>=this->size)
    {
        this->size*=2;
        char *Newptr=new char[this->size];
        strcpy(Newptr,ptr);
        delete this->ptr;
        this->ptr=Newptr;
    }
}
void My_string:: show()
{
    cout<<this->ptr<<endl;
}
My_string My_string::operator+(const My_string &other)
{
    My_string Newptr;
    Newptr.len = this->len+other.len;
    Newptr.size = Newptr.len;
    Newptr.ptr = new char[Newptr.size+1];
    strcpy(Newptr.ptr,this->ptr);
    strcat(Newptr.ptr,other.ptr);
    return Newptr;
}
bool My_string::operator>(const My_string &other)
{
    return strcmp(this->ptr,other.ptr)>0;
}
My_string &My_string::operator+=(const My_string &other)
{
    this->size=other.size;
    char *Newptr=new char[this->size+1];
    strcpy(Newptr,this->ptr);
    strcat(Newptr,other.ptr);
    delete []this->ptr;
    this->ptr=Newptr;
    this->len+=other.len;
    return *this;
}
ostream &operator<<(ostream &s,const My_string &other)
{
    s<<other.ptr;
    return s;
}
char & My_string::operator[](int idnex)
{
  return this->ptr[idnex-1];
}

主函数

#include "my_stringhead.h"

int main()
{
    My_string str("hello world");
    str.push_back('y');
    str.pop_back();
    str.show();
    cout<<str.get_len()<<endl;
    cout<<str.get_size()<<endl;
    My_string str1;
    str1=str;
    str1.show();
    My_string str2;
    str2 = str1+str;
    str2.show();
    if(str2>str)
    {
        cout<<"大于"<<endl;
    }
    My_string str3("make time ");
    str3+=str;
    str3.show();
    cout<<str3<<endl;
    cout<<str3[2]<<endl;
    return 0;
}

2,思维导图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值