string類介面實現
阿新 • • 發佈:2018-12-05
namespace bit { class String { public: typedef char* Iterator; public: String(const char* str = " ") { if(str == nullptr) { assert(false); return ; } _size = strlen(str); _capacity = _size; _str = new char[strlen(str) + 1]; strcpy(_str, str); } String(const String& s) : _str(new char[strlen(s._str) + 1]) , _size(s._size) , _capacity(s._capacity) { strcpy(_str, s._str); } String& operator = (const String& s) { if(this != &s) { char* pstr = new char[strlen(s._str) + 1]; strcpy(pstr, s._str); delete[] _str; _str = pstr; _size = s._size; _capacity = s._capacity; } return *this; } ~String() { if(_str) { delete[] _str; _str = nullptr; } } 迭代器 Iterator Begin() { return _str; } Iterator End() { return _str + _size; } void Reserve(size_t newCapacity) { if(newCapacity > _capacity) { char* str = new char[newCapacity + 1]; strcpy(str, _str); delete[] _str; _str = str; _capacity = newCapacity; } } void PushBack(char c) { if(_size == _capacity) Reserve(_capacity * 2); _str[_size++] = c; _str[_size] = '\0'; } void Append(size_t n, char c) { for(int i = 0; i < n; i++) { PushBack(c); } } String& operator += (char c) { PushBack(c); return *this; } bool operator<(const String& s) { return (&s < this); } bool operator<=(const String& s) { return !(&s > this); } bool operator>(const String& s) { return (&s > this); } bool operator>=(const String& s) { return !(&s < this); } bool operator==(const String& s) { return (&s == this); } bool operator!=(const String& s) { return !(&s == this); } 返回c在string中第一次出現的位置 size_t Find (char c, size_t pos = 0) const { for(size_t i = pos; i < _size; i++) { if(c == _str[i]) return i; } return 0; } size_t rFind (char c) { for(size_t i = _size; i >= 0; --i) { if(c == _str[i]) return i; } return 0; } 返回子串s在string中第一次出現的位置 size_t Find (const char* s, size_t pos = 0) const { assert(s); size_t len = strlen(s); for(int i = pos; i < _size; i++) { int j = 0; if(_str[i] != s[j]) continue; while(j < len && i + j < _size) { j++; if(_str[i+j] != s[j]) break; } if(j == len) return i + 1; } return 0; } 擷取string從pos位置開始的n個字元 String SubStr(size_t pos, size_t n) { if(pos > 0 && (pos <= _size)) { char *tmp = new char[n]; memcpy(tmp, _str + pos - 1, n - 1); } return *this; } 在pos位置上插入字元c/字串str,並返回該字元的位置 String& Insert(size_t pos, const char* str) { 判斷pos的值是否合理 if(pos > 0 && pos <= _size) { 檢查容量是否足夠 Reserve(strlen(str)); 建立一塊臨時空間去接收_str中pos之後的資料 char *tmp = new char[_size - pos + 2]; strcpy(tmp, _str + pos -1); 此時需要將str的資料插入到pos之後,用strcat函式即可,但是注意被追加字串的哪個字串必須以'\0'結尾 _str[pos - 1] = '\0'; strcat(_str, str); strcat(_str, tmp); _size += strlen(str); _str[_size] = '\0'; delete[] tmp; } return *this; } 刪除pos位置上的元素,並返回該元素的下一個位置 String& Erase(size_t pos, size_t len) { if(pos > 0 && pos <= _size) { size_t npos = pos - 1; size_t index = npos + len; while(npos + len != _size) { _str[npos++] = _str[index++]; } _size -= len; _str[_size] = '\0'; } return *this; } void Append(const char* str) { size_t len = strlen(str); if(len > _capacity - _size) Reserve(2 * _capacity + len); /*for(int i = 0; i < strlen(str); i++) { PushBack(str[i]); }*/ strcat(_str, str); _size += len; } void Resize(size_t newSize, char c = char()) { if(newSize > _size) { 如果newSize大於舊_size,就需要開闢新空間 Reserve(newSize); memset(_str + _size, c, newSize - _size); } _size = newSize; _str[_size] = '\0'; } char& operator [](size_t index) { assert(index < _size); return _str[index]; } const char& operator [](size_t index)const { assert(index < _size); return _str[index]; } private: friend ostream& operator << (ostream& _cout,const String& s); private: char* _str; size_t _size; size_t _capacity; }; ostream& bit::operator << (ostream& _cout,const String& s) { cout << s._str; return _cout; } }