c++ string 类实现

c ++ string 类实现

main.cpp

#include <iostream>
#include"mystring.h"
using namespace std;

int main()
{
   mystring str1(8,'x');
   mystring str3("1234567890");
   cout<<str1.c_str()<<endl;
   cout<<str1.maxsize<<endl;

   cout<<str3.c_str()<<endl;
   cout<<str3.maxsize<<endl;
   str3+=str1;
   cout<<str3.c_str()<<endl;
   cout<<str3.maxsize<<endl;
   cout<<&str3<<endl;
   string a;
   cout<<&a<<endl;
    return 0;
}

mystring.h

#ifndef MYSTRING_H
#define MYSTRING_H
#include<iostream>
#include<cstring>
using namespace std;

class mystring
{
    char *str;
    int size;
	int maxsize;//记录容量
public:
  
    mystring();
    mystring(const char *);
    mystring(int n,char ch);
    ~mystring();
    //*******//
    //拷贝构造函数
    mystring(const mystring &other);
    //拷贝赋值函数
    mystring & operator=(const mystring &other);
    mystring & operator=(const char * str1);
    //判空函数
    bool isempty();
    //size函数
    int mysize();
    //c_str函数
    char * c_str();
    //at函数
    char & at(int n);
    //二倍扩容
    void expent();
    //实现+=运算符重载            s1 += s2
    mystring & operator+=(const mystring &other);
    mystring & operator+=(const char* &);
    //取地址运算符重载
    mystring * operator&();
};


#endif // MYSTRING_H

mystring.cpp

#include "mystring.h"
#define SIZE 8
#include"mystring.h"
//计算所需最小满足len的容量 每次都是8的倍数
int max_size(int len,int maxsize){

    while(len>maxsize){
        maxsize*=2;
    }
    return maxsize;
}
//构造函数
mystring::mystring(){
    //初始化
    size=0;
    maxsize=SIZE;
    str=new char[size];
    cout<<"无参构造"<<endl;
}
//有参构造
mystring::mystring(const char * str1){
    //初始化
     size=strlen(str1);
    maxsize=max_size(size+1,SIZE);//计算出最小满足len的容量 需要考虑'\0'
    str=new char[maxsize];
    //拷贝
    memcpy(str,str1,size+1);//需要考虑'\0'
    cout<<"mystring::有参"<<endl;

}
mystring::mystring(int n,char ch){
    maxsize=max_size(n+1,SIZE);//\0
    size=n;
    str=new char[maxsize];
    int i=0;
    for(i=0;i<size;i++){
        str[i]=ch;
    }
    str[i]='\0';

}
//析构函数
mystring::~mystring(){
    delete []str;
    cout<<"析构"<<endl;
}
//拷贝构造函数
mystring ::mystring(const mystring &other){
    //初始化
    if(other.str==NULL){return ;}
    size=other.size;
    maxsize=other.size;
    str=new char[maxsize];
    //拷贝
    memcpy(str,other.str,maxsize);
    cout<<"拷贝构造函数"<<endl;
}


//拷贝赋值函数
mystring & mystring::operator=(const mystring &other){
    size=other.size;
    maxsize=other.maxsize;
    delete []str;
    str=new char[maxsize];
    memcpy(str,other.str,maxsize);
    cout<<"拷贝赋值函数"<<endl;
    return *this;
}
mystring & mystring::operator=(const char * str1){
    //初始化
     size=strlen(str1);
     maxsize=SIZE;
    maxsize=max_size(size+1,maxsize);
    str=new char[maxsize];//需要考虑'\0'
    //拷贝
    memcpy(str,str1,size+1);
    cout<<"拷贝赋值函数"<<endl;
    return *this;
}
//判空函数
bool mystring::isempty(){
    if(size==0){
        return true;
    }
    return false;
}
//size函数
int mystring::mysize(){
    if(str==NULL) return -1;
    return size;
}
//c_str函数
char * mystring::c_str(){
    return str;
}
//at函数
char & mystring::at(int n){
    return str[n];
}
//二倍扩容
void mystring::expent(){
    char * temp=new char[maxsize*2];
    if(temp==NULL){
        perror("expent");
        return ;
    }
    memcpy(temp,str,maxsize);
    delete []str;
    str=temp;
    maxsize*=2;

}
//实现+=运算符重载            s1 += s2
mystring & mystring::operator+=(const mystring &other){
    //扩容
    while(1){
        //如果可以容纳就跳出循环
        if(maxsize>=other.size+size+1)//注意\0
        {
          break;
        }
        //扩容
        expent();
    }
    //追加
    strcat(str,other.str);

    return *this;
}

mystring & mystring::operator+=(const char* &str1){
    //计算大小
    int len=strlen(str1);
    //扩容
    while(1){
        //如果可以容纳就跳出循环
        if(maxsize>=len+size+1)//注意\0
        {
          break;
        }
        //扩容
        expent();
    }
    //追加
    strcat(str,str1);

    return *this;
}
//取地址运算符重载
mystring * mystring::operator&(){
    cout<<"取地址运算重载"<<endl;
return this;
}



测试

无参构造测试

在这里插入图片描述

有参构造测试

在这里插入图片描述

拷贝构造函数

在这里插入图片描述

赋值函数

在这里插入图片描述

size c_str 判空

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值