仿照string类,自己手动实现 My_string
代码如下
MyString.h
#ifndef MYSTRING_H
#define MYSTRING_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(int num, char value);
My_string(const My_string &other);
My_string & operator= (const My_string &other);
~My_string();
bool Isvoid();
void show();
void push_back(char value);
void pop_back();
char &at(int index);
void clear();
char *data();
int get_length();
int get_size();
bool Add();
};
#endif
MyString.cpp
#include "MyString.h"
My_string::My_string():size(15)
{
this->ptr = new char[size];
this->ptr[0] = '\0';
this->len = 0;
cout<<"无参构造"<<endl;
}
My_string::My_string(const char* src):size(15) {
this->ptr = new char[size];
strcpy(ptr, src);
this->len = strlen(src);
cout << "一个形参的有参构造" << endl;
}
My_string::My_string(int num, char value):size(15),len(num){
if(num>15){
cout<<"超出默认长度"<<endl;
return;
}
this->ptr = new char[15];
for(int i=0;i<num;i++){
this->ptr[i] = value;
}
cout<<"部分形参的有参构造"<<endl;
}
My_string::My_string(const My_string &other):size(other.size),len(other.len){
this->ptr = new char[size];
strcpy(this->ptr, other.ptr);
cout<<"拷贝构造"<<endl;
}
My_string& My_string::operator= (const My_string &other){
if(this != &other){
this->len =other.len;
this->size = other.size;
for(int i=0;i<other.len;i++){
this->ptr[i] =other.ptr[i];
}
}
cout<<"拷贝赋值"<<endl;
return *this;
}
My_string::~My_string(){
cout<<this->ptr<<"析构函数"<<endl;
delete[] ptr;
}
bool My_string::Isvoid(){
return this->len ==0 ? true:false;
}
void My_string::show(){
for(int i=0;i<this->len;i++){
cout<<*(this->ptr+i);
}
cout<<endl;
}
void My_string::push_back(char value){
if(this->len < this->size-1){
*(this->ptr+len++) = value;
}else if(this->Add()){
*(this->ptr+len++) = value;
}
}
void My_string::pop_back(){
this->len--;
}
char& My_string::at(int index){
if(index<=this->len-1){
return this->ptr[index];
}else {
cout<<"下标越界"<<endl;
exit(EXIT_SUCCESS);
}
}
void My_string::clear(){
free(this->ptr);
this->ptr[0] = '\0';
this->len = 0;
}
char* My_string::data(){
return this->ptr;
}
int My_string::get_length(){
return this->len;
}
int My_string::get_size(){
return this->size;
}
bool My_string::Add(){
if(this->len == this->size-1){
char *p = new char[size*2];
strcpy(p,this->ptr);
free(this->ptr);
this->ptr = p;
return true;
}else return false;
}
main.cpp
#include "MyString.h"
int main(){
My_string s;
cout<<"s:";
s.show();
My_string s1("hello");
cout<<"s1:";
s1.show();
My_string s2(5,'A');
cout<<"s2:";
s2.show();
My_string s3 = s2;
cout<<"s3:";
s3.show();
s3 = s1;
cout<<"s3:";
s3.show();
if(s3.Isvoid()){
cout<<"s3空"<<endl;
}else cout<<"s3非空"<<endl;
cout<<"尾插:";
s3.push_back('a');
s3.show();
cout<<"尾删:";
s3.pop_back();
s3.show();
cout<<"查看下标4的值:"<<s3.at(4)<<endl;
cout<<"清空s3函数"<<endl;
s3.clear();
cout<<"s3:";
s3.show();
cout<<"s1的C风格字符串:"<<s1.data()<<endl;
cout<<"s1的实际长度:"<<s1.get_length()<<endl;
cout<<"s1当前最大容量:"<<s1.get_size()<<endl;
}
运行结果
思维导图