仿照string类,自己手动实现 My_string
代码如下
MyString.h
cpp
#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();
//at函数实现
char &at(int index);
//清空函数
void clear();
//返回C风格字符串
char *data();
//返回实际长度
int get_length();
//返回当前最大容量
int get_size();
//君子函数:二倍扩容
bool Add();
};
#endif // MYSTRING_H
MyString.cpp
cpp
#include "MyString.h"
My_string::My_string() : size(15), len(0) {
ptr = new char[size];
ptr[0] = '\0'; // 表示串为空串
cout << "无参构造" << endl;
}
My_string::My_string(const char* src) : size(15) {
ptr = new char[size];
strcpy(ptr, src); // 复制字符串
len = strlen(src);
cout << "一个形参的有参构造" << endl;
}
My_string::My_string(int num, char value) : size(15), len(num) {
if (num > 15) {
cout << "超出默认长度" << endl;
return;
}
ptr = new char[size];
for (int i = 0; i < num; i++) {
ptr[i] = value;
}
ptr[num] = '\0'; // 确保字符串以'\0'结尾
cout << "部分形参的有参构造" << endl;
}
My_string::My_string(const My_string &other) : size(other.size), len(other.len) {
ptr = new char[size];
strcpy(ptr, other.ptr); // 复制字符串
cout << "拷贝构造" << endl;
}
My_string& My_string::operator= (const My_string &other) {
if (this != &other) {
delete[] ptr; // 释放旧内存
size = other.size;
len = other.len;
ptr = new char[size];
strcpy(ptr, other.ptr); // 复制字符串
}
cout << "拷贝赋值" << endl;
return *this;
}
My_string::~My_string() {
cout << ptr << "析构函数" << endl;
delete[] ptr;
}
bool My_string::Isvoid() {
return len == 0;
}
void My_string::show() {
cout << ptr << endl;
}
void My_string::push_back(char value) {
if (len < size - 1) {
ptr[len++] = value;
ptr[len] = '\0'; // 确保字符串以'\0'结尾
} else if (Add()) {
ptr[len++] = value;
ptr[len] = '\0'; // 确保字符串以'\0'结尾
}
}
void My_string::pop_back() {
if (len > 0) {
len--;
ptr[len] = '\0'; // 确保字符串以'\0'结尾
}
}
char& My_string::at(int index) {
if (index < len) {
return ptr[index];
} else {
cout << "下标越界" << endl;
exit(EXIT_FAILURE);
}
}
void My_string::clear() {
len = 0;
ptr[0] = '\0'; // 确保字符串以'\0'结尾
}
char* My_string::data() {
return ptr;
}
int My_string::get_length() {
return len;
}
int My_string::get_size() {
return size;
}
bool My_string::Add() {
if (len == size - 1) {
char *p = new char[size * 2];
strcpy(p, ptr);
delete[] ptr; // 释放旧内存
ptr = p;
size *= 2; // 更新容量
return true;
}
return false;
}
main.cpp
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的值:";
if (s3.get_length() > 4) {
cout << s3.at(4) << endl;
} else {
cout << "下标越界" << 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;
cout << "s3的二倍扩容" << endl;
cout << "请输入一串字符串:";
char c = ' ';
while (true) {
cin >> c;
if (c == '#') break; // 直接在这里判断并退出循环
s3.push_back(c);
}
s3.show();
return 0;
}