仿照string类,自己手动实现 My_string
头文件
cpp
#ifndef SEQLIST_H
#define SEQLIST_H
#include <iostream>
#include <cstring>
using namespace std;
using datatype = char;
class My_string
{
private:
datatype *ptr; //顺序表字符数组
int size = 15; //数组的最大
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(); //判空
void push_back(datatype value); //尾插
void pop_back(); //尾删
int listsize(); //求长度
datatype & at(int inex); //获取任意位置元素
void clear();//清空
char *data();//返回C风格字符串
int get_length();//返回当前最大容器
void show(); //展示
void append(const char *ptr);//扩容
};
#endif // SEQLIST_H
源文件
cpp
#include "seqlist.h"
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)
{
len = strlen(src)+1;
this->ptr = new char[len];
strcpy(ptr,src);
}
My_string::My_string(int num,char value)
{
ptr = new char[num +1];
for(int i = 0;i<num;i++)
{
this->ptr[i] = value;
}
this->len = num;
}
//拷贝构造
My_string::My_string(const My_string &other):ptr(new char[other.size]),size(other.size),len(other.len)
{
strcpy(this->ptr,other.ptr);
this->size = other.size;
this->len = other.len;
}
//拷贝赋值
My_string &My_string::operator = (const My_string &other)
{
if(this != &other)
{
delete []ptr;
size = other.size;
ptr = new char[size + 1];
strcpy(ptr,other.ptr);
}
return *this;
}
//析构函数
My_string::~My_string()
{
delete []ptr;
}
char *My_string::data()//返回C风格字符串
{
return ptr;
}
//判空
bool My_string::empty()
{
return ptr[0] == 0;
}
//尾插
void My_string::push_back(datatype value)
{
this->ptr[len++] = value;
}
//尾删
void My_string::pop_back()
{
if(this->empty())
{
cout<<"表为空无删除对象"<<endl;
return;
}
this->len--;
}
//求长度
int My_string::listsize()
{
return this->len;
}
//获取任意位置元素
datatype & My_string::at(int index)
{
if(this->empty())
{
throw std::out_of_range("表为空无对象");
}
if(index>this->len||index<=0)
{
throw std::out_of_range("位置错误");
}
return this->ptr[index-1];
}
//展示
void My_string::show()
{
if(this->empty())
{
cout<<"表为空无对象"<<endl;
return;
}
cout<<"当前顺序表中的元素是:";
cout<<"ptr = "<<ptr<<" ";
len = strlen(ptr);
cout<<"len = "<<len<<endl;
}
int My_string::get_length()//返回当前最大容器
{
return this->size;
}
void My_string::clear()//清空
{
delete []ptr;
ptr = new char[1];
ptr[0] = '\0';
len = 0;
}
void My_string::append(const char *src)
{
int src_len = strlen(src);
while(len+src_len >= size)
{
size *= 2;
char *new_ptr = new char[size];
strcpy(new_ptr,ptr);
delete []ptr;
ptr = new_ptr;
}
strcat(ptr,src);
len += src_len;
}
主程序
cpp
#include "seqlist.h"
int main()
{
My_string s1("Hello");
//拷贝构造
My_string s2 = s1;
s2.show();
//无参构造
My_string s3;
//拷贝赋值
s3 = s1;
s3.show();
s3.push_back('a');
//C字符风格
cout<<s3.data()<<endl;
s3.pop_back();
s3.append(" world ,good");
s3.show();
//求实际长度
cout<<"s3容器的实际长度 = "<<s3.listsize()<<endl;
cout<<"s3容器的最大长度 = "<<s3.get_length()<<endl;
cout<<"s3容器2号元素= "<<s3.at(2)<<endl;
//clear
s3.clear();
s3.show();
return 0;
}