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 判空