#pragma once #include <iostream> #include <assert.h> using namespace std; namespace my_string { //自定义的命名空间,避免与std中的string冲突 class string { public: string(const char* str = "") //构造函数 :_size(strlen(str)) { _capacity = _size; _str = new char[_capacity + 1]; // ' '不计为有效值 strcpy(_str, str); } string(const string& s) { //拷贝构造 _capacity = s._capacity; _size = s._size; _str = new char[_capacity + 1]; strcpy(_str, s._str); } ~string() { //析构 delete[] _str; _str = nullptr; _size = _capacity = 0; } string& operator=(const string& s) { //赋值运算符重载 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; } void reserve(size_t new_capacity) { //扩容 if (new_capacity > _capacity) { char* tmp = new char[new_capacity + 1]; strcpy(tmp, _str); delete[] _str; _str = tmp; _capacity = new_capacity; } } void resize(size_t new_size,char x=' ') { if (new_size <= _size) { _size = new_size; _str[_size] = x; } else { if (new_size > _capacity) { reserve(new_size); } for (size_t i = _size; i < new_size; i++) { _str[i] = x; } _size = new_size; _str[_size] = ' '; } } //比较运算符重载 bool operator==(const string& s) const{ return strcmp(_str, s._str) == 0; } bool operator<(const string& s) const{ return strcmp(_str, s._str) < 0; } bool operator<=(const string& s) const { return (*this < s || *this == s); } bool operator!=(const string& s) const { return !(*this == s); } bool operator>(const string& s) const { return !(*this <= s ); } bool operator>=(const string& s) const { return !(*this < s); } // char& operator[](size_t i) { //索引访问(可变) return _str[i]; } const char& operator[](size_t i)const { return _str[i]; } //索引访问(不可变) const char* c_str() { //返回数据 return _str; } const int size() { //返回有效个数 return _size; } const int capacity() { //返回容量 return _capacity; } typedef char* iterator;//迭代器 typedef const char* const_iterator; string::iterator begin() { return _str; } string::iterator end() { return _str + _size; } const string::const_iterator cbegin() { return _str; } const string::const_iterator cend() { return _str + _size; } //尾插字符 void pushback(char x) { if (_size == _capacity) { reserve(2* _capacity); } _str[_size++] = x; _str[_size] = ' '; } //尾插字符串 void append(const char* str) { size_t length = strlen(str); if (_size + length > _capacity) { reserve(_size + length); } for (size_t i = _size; i < _size + length; i++) { _str[i] = str[i - _size]; } _size += length; _str[_size] = ' '; } //中间插入字符串 void insert(size_t pos, const char* str) { assert(pos < _size); size_t length = strlen(str); if (_size + length > _capacity) { reserve(_size + length); } size_t end = _size + length; while (end > pos-1+length) { _str[end] = _str[end - length]; end--; } for (size_t i = 0; i < length; i++) { _str[pos + i] = str[i]; } _size += length; } //删除 void erase(size_t pos, size_t len = npos) { if (len == npos || pos + len >= _size) { //必须写明两个条件 _str[pos] = ' '; _size = pos; } else { strcpy(_str + pos, _str + pos + len); _size -= len; } } //查找 size_t find(const char* str,size_t pos=0) { const char* p = strstr(_str+pos, str); if (p == nullptr) { return npos; } return p - _str; } void clear() { _str[0] = ' '; _size = 0; } private: char* _str; //指向堆区 int _size; //有效字符个数 int _capacity; //容量 static const size_t npos = -1; //此处-1是一个非常大的数 }; }