在string类:版本、组件、构造、操作及应用和 C++中string的一些超常用函数 (附习题)这两篇文章中我们已经了解到了string,现在让我们再来手动实现模拟一下吧~
模拟实现string是为了更好的理解string函数的使用和深浅拷贝方面的知识~
总体整理了两张思维导图,大概是这个样子的,XMind资源已经上传啦,可以按需下载~
整体代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
namespace yzq
{class string{public://string()//无参构造函数// //初始化列表// :_str(new char[1])//为了析构都是用delete[],匹配使用// ,_size(0)// ,_capaicty(0)//{// _str[0] = '\0';//}//string(const char*str)//带参构造函数// // :_size(strlen(str))//{// _capaicty = _size;// _str = new char[_capaicty+1];//因为有'\0'的存在所以多开一个空间// strcpy(_str, str);//拷贝//}typedef char* iterator;typedef const char* const_iterator;iterator begin(){return _str;}iterator end(){return _str + _size;}iterator begin()const {return _str;}iterator end()const{return _str + _size;}string(const char* str="")//构造函数:_size(strlen(str)){if (_size == 0){_capacity = 3;}else{_capacity = _size;}_str = new char[_capacity + 1];strcpy(_str, str);}string(const string& s)//拷贝构造:_size(s._size),_capacity(s._capacity){//深拷贝_str = new char[_capacity + 1];//开辟一块空间strcpy(_str, s._str);//将s2的值传给s1}string& operator=(const string& s)//赋值 s1=s3{if (this != &s)//排除赋值本身的情况{char* tmp = new char[s._capacity + 1];strcpy(tmp, s._str);delete[] _str;_str = tmp;_size = s._size;_capacity = s._capacity;}return *this;}size_t size()const{return _size;}const char* C_str()//返回const char*类型的指针 {return _str;}char& operator[](size_t pos)//operator[]{return _str[pos];}char& operator[](size_t pos)const //函数重载{return _str[pos];}bool operator==(const string& s)const //s1==s2{return strcmp(_str, s._str)==0;}bool operator<(const string& s)const //s1<s2{return strcmp(_str, s._str) < 0;}bool operator<=(const string& s)const //s1<=s2{return *this < s || *this == s;}bool operator>(const string& s)const //s1>s2{return !(*this <= s);}bool operator>=(const string& s)const //s1>=s2{//复用return *this > s || *this == s;}bool operator!=(const string& s)const //s1!=s2{return !(*this == s);}void reserve(size_t n)//开辟空间{if (n > _capacity)//防止缩容的问题{char* tmp = new char[n + 1];//多开一个'\0'strcpy(tmp, _str);delete[]_str;_str = tmp;_capacity = n;//计算有效}}void resize(size_t n,char ch)//开辟空间+初始化{if (n <= _size)//删除数据保留前n个{_size = n;_str[n] = '\0';}else //n>_size{if (n >_capacity)//扩容{reserve(n);}int i = _size;while (i < n){_str[i] = ch;i++;}_size = n;_str[_size] = '\0';}}void push_back(char ch)//尾插字符{if (_size + 1 > _capacity){reserve(2 * _capacity);//开辟2倍空间}_str[_size] = ch;_size++;//ch是一个字符,所以用单独处理'\0'_str[_size] = '\0';}void append(const char* str)//尾插 字符串{ int len = strlen(str);if (_size + len > _capacity){reserve(_size + len);}strcpy(_str + _size, str);//在原来的字符串后拷贝字符串_size += len;//str是一个字符串,本身带'\0'}string& operator+=(char ch)//+= 字符{push_back(ch);return *this;}string& operator+=(const char* str)//+= 字符串 函数重载{append(str);return *this;}string& insert(size_t pos, char ch)//在pos位置前插入字符ch{if (_size + 1 > _capacity)//扩容{reserve(2 * _capacity);}size_t end = _size + 1;while (end > pos){//把前面传给后面_str[end] = _str[end - 1];end--;}_str[pos] = ch;_size++;return *this;}string& insert(size_t pos, const char* str)//在pos位置前插入字符串str{int len = strlen(str);if (_size + len > _capacity)//扩容{reserve(_size + len);}size_t end = _size + len;while (end > pos+len-1){//把前面传给后面_str[end] = _str[end-len];end--;}strncpy(_str + pos, str, len);//拷贝len个字节,不包含'\0'_size += len;return *this;}static const size_t npos = -1;string& erase(size_t pos = 0, size_t len = npos)//从pos位置开始删除len个数据{if (len==npos||pos+len>=_size){//全部删除_str[pos] = '\0';_size = pos;}else{strcpy(_str + pos, _str +pos+len);//包含'\0'_size -= len;}return *this;}void swap(string &s)//交换{std::swap(_str, s._str);std::swap(_capacity, s._capacity);std::swap(_size, s._size);}size_t find(char c, size_t pos =0){int i = 0;for (i = pos; i < size(); i++){if (_str[i] == c){return i;}}return npos; }size_t find(const char* str, size_t pos = 0)//从pos位置开始找子串{char*p=strstr(_str+pos, str);if (p == nullptr){return npos;}else{return p - _str;//指针相减为个数}}void clear()//清空{_str[0] = '\0';}~string()//析构{delete[]_str;_str = nullptr;_size = 0;_capacity = 0;}private:char* _str;size_t _size;size_t _capacity;};ostream& operator<<(ostream& out, const string&s)//<<{int i = 0;for (i = 0; i < s.size(); i++){out << s[i];}return out;}istream& operator>>(istream& in, string& s)//>>{s.clear();char ch = in.get();char buf[128];size_t index = 0;while (ch != ' ' && ch != '\n'){buf[index++] = ch;if (index == 127)//为了防止频繁扩容{buf[127] = '\0';s += buf;index = 0;}ch = in.get();}if (index != 0){buf[index] = '\0';s += buf;}return in;}void print(const string& s){string::const_iterator it = s.begin();while (it != s.end()){cout << *it << " ";it++;}cout << endl;}void test(){string s1;cin >> s1;cout << s1;}
}