string类的模拟实现
实现string的模拟实现分为三个文件,分别为:string.h、sting.cpp、test.cpp
string.h
其中包含一些短小常用的函数的实现,头文件,函数的声明
#include<iostream>
#include<string>
#include<assert.h>using namespace std;namespace stringbyself
{class string{public:typedef char* iterator;typedef const char* const_iterator;//编译器开始iterator begin(){return _str;}//编译器结束iterator end(){return _str + _size;}const_iterator begin() const{return _str;}const_iterator end() const{return _str+_size;}//构造函数string(const char* str = ""){_size = strlen(str);_capacity = _size;_str = new char(_size + 1);strcpy(_str, str);}//析构~string(){if (_str){delete[] _str;_str = nullptr;_size = _capacity = 0;}}//c_strconst char* c_str() const{return _str;}//clearvoid clear(){_str[0] = '\0';_size = 0;}//sizesize_t size() const{return _size;}//capacitysize_t capacity() const{return _capacity;}//[]char& operator[](size_t pos){assert(pos < _size);return _str[pos];}const char& operator[](size_t pos) const{assert(pos < _size);return _str[pos];}void reserve(size_t n);void push_back(char ch);string& operator+=(char ch);string& operator+=(const char* str);void append(const char* str);void insert(size_t pos, char ch);void insert(size_t pos, const char* str);private:char* _str = nullptr;size_t _size = 0;size_t _capacity = 0;static const size_t npos;};
}
string.cpp
实现以下函数:
- reserve
为字符串预留空间。当需要空间大于字符串的空间时,需要开辟一块新的空间,将之前的空间进行释放,将新开辟的空间赋值给字符串,并修改_capacity的大小。
- push_back
尾插字符。首先判断字符串空间是否足够,进行尾插,并修改_size的值以及将末尾添加’\0’
- operator+=
在字符串后追加字符串。当添加的为字符时,直接调用push_back即可,并返回 * this指针;当返回的为字符串时,调用append即可,返回 * this指针
- append
在字符串后追加一个字符串。先计算待插入的字符串长度;判断原有字符串加上待插入字符串是否会大于_capacity的大小,并对空间进行调整;进行插入并调整_size的大小即可
- insert
在字符串中插入字符串或字符。当插入字符时,首先判断_capacity是否足够,并对空间进行调整;将数据进行挪动,然后插入数据,调整_size即可。当插入的为字符串时,先计算待插入的字符串长度;判断原有字符串加上待插入字符串是否会大于_capacity的大小,并对空间进行调整;将数据进行挪动,然后插入数据,调整_size即可。
#include"string.h"namespace stringbyself
{static const size_t npos = -1;void string::reserve(size_t n){if (n > _capacity){char* tmp = new char[n + 1];//给'\0'多开辟一个空间strcpy(tmp,_str);delete[] _str;_str = tmp;_capacity = n;}}void string::push_back(char ch){if (_size == _capacity){reserve(_capacity == 0 ? 4 : _capacity * 2);}_str[_size] = ch;_size++;_str[_size] = '\0';}string& string::operator+=(char ch){push_back(ch);return *this;}string& string::operator+=(const char* str){append(str);return *this;}void string::append(const char* str){size_t len = strlen(str);if (_size + len > _capacity){reserve(_size + len > _capacity * 2 ? _size + len : _capacity * 2);}strcpy(_str + _size, str);_size = _size + len;}void string::insert(size_t pos, char ch){assert(pos <= _size);if (_size == _capacity){reserve(_capacity == 0 ? 4 : _capacity * 2);}size_t end = _size + 1;while (end > pos){_str[end] = _str[end - 1];--end;}_str[pos] = ch;_size++;}void string::insert(size_t pos, const char* str){assert(pos <= _size);size_t len = strlen(str);if (_size + len > _capacity){reserve(_size + len > _capacity * 2 ? _size + len : _capacity * 2);}size_t end = _size + len;while (end > pos + len - 1){_str[end] = _str[end - len];--end;}for (size_t i = 0; i < len; i++){_str[pos + i] = str[i];}_size += len;}
}
test.cpp
用于测试代码是否能完成相对的功能
#include"string.h"int main()
{string s1("hello world");cout << s1 << endl;string::iterator it = s1.begin();while (s1.end() != it){cout << *it << " ";it++;}cout << endl;cout << s1.size() << endl;cout << s1.capacity() << endl;s1.push_back('#');cout << s1 << endl;s1 += "&&&";cout << s1 << endl;s1 += '*';cout << s1 << endl;s1.append("@");cout << s1 << endl;s1.insert(0, "%");cout << s1 << endl;s1.insert(0, "$$$");cout << s1 << endl;return 0;
}