仿照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), 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'; 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'; } else if (Add()) {ptr[len++] = value;ptr[len] = '\0'; }
}void My_string::pop_back() {if (len > 0) {len--;ptr[len] = '\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';
}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
#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;
}
运行结果
思维导图