string類的模擬實現
阿新 • • 發佈:2018-12-04
class String { public: typedef char* Iterator; public: String() { } String(const char* str) { if (str == nullptr) { assert(str); return; } _str = new char[strlen(str) + 1]; _size = strlen(str); _capacity = _size+1; strcpy(_str,str); } String(const char* str, size_t size)//用字串前size個位元組 建立一個物件 :_str(new char[size+1]) , _size(size) , _capacity(size*3/2) { size_t len = strlen(str); if (size > len) size = len; size_t i; for (i=0; i < size; i++) _str[i] = *str++; _str[i] = '\0'; } String(const String& s)//深拷貝傳統寫法 :_str(new char[strlen(s._str)+1]) { strcpy(_str,s._str); } /*String(const String& s)//深拷貝現代寫法 :_str(nullptr) { String strtmp(s._str); swap(_str, strtmp._str); }*/ String& operator=(const String& s) { if (this!=&s) { char* pstr = new char[s._capacity + 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; } Iterator RBegin() { return End(); } Iterator REnd() { return Begin(); } void PushBack(char c) { Reserve(_size + 1); _str[_size++] = c; _str[_size] = '\0'; } void Append(const char* str)//追加一個字串 { size_t len = strlen(str); if (len > _capacity - _size) { Reserve(_capacity * 2+len); } strcat(_str,str); _size = _size + len; } char& operator[](size_t index) { return _str[index]; } const char& operator[](size_t index)const { return _str[index]; } void ReSize(size_t newSize) { if (newSize < _size) _size = newSize; else { size_t i; for (i = _size; i < newSize; i++) _str[i] = 0; _str[i] = '\0'; _size = newSize; _capacity *= 2; } } void ReSize(size_t newSize, char c)//size變成newsize 多的字元用c填充 { if (newSize < _size) _size = newSize; else { size_t i; for (i = _size; i < newSize; i++) _str[i] = c; _str[i] = '\0'; _size = newSize; _capacity *= 2; } } void Reserve(size_t newCapacity) { if (newCapacity>_capacity) _capacity = newCapacity; } int Size()const { return _size; } bool Empty()const { return 0 == _size; } void Clear() { _size = 0; } int Find(char c, size_t pos = 0) { for (size_t i = 0; i < _size; i++) { if (c == _str[i]) return i; } return -1; } int rFind(char c) { for (int i = _size - 1; i>0;i--) { if (c == _str[i]) return i; } return -1; } const char* C_Str()const { return _str; } void Swap(String& s) { swap(_size, s._size); swap(_str, s._str); swap(_capacity, s._capacity); } String StrSub(size_t pos, size_t size)//從pos位置擷取size個位元組返回 { //gfdasgfu size_t len = pos + size; if (size > len) size = len; String ret(_str + pos, size); return ret; } //字串的比較 bool operator<(const String& s) { size_t i = 0; size_t len1 = strlen(this->_str); size_t len2 = strlen(s._str); if (len2<len1) len1 = len2; for (; i < len1; i++) { if (this->_str[i] < s._str[i]) return true; if (this->_str[i] > s._str[i]) return false; } if (this->_size>s._size) return false; else return true; } bool operator<=(const String& s) { size_t i = 0; size_t len1 = strlen(this->_str); size_t len2 = strlen(s._str); if (len2<len1) len1 = len2; for (; i < len1; i++) { if (this->_str[i] < s._str[i]) return true; if (this->_str[i] > s._str[i]) return false; } if (this->_size>s._size) return false; else return true; } bool operator>(const String& s) { size_t i = 0; size_t len1 = strlen(this->_str); size_t len2 = strlen(s._str); if (len2>len1) len1 = len2; for (; i < len1; i++) { if (this->_str[i] > s._str[i]) return true; if (this->_str[i] <s._str[i]) return false; } if (this->_size>s._size) return true; else return false; } bool operator>=(const String& s) { size_t i = 0; size_t len1 = strlen(this->_str); size_t len2 = strlen(s._str); if (len2>len1) len1 = len2; for (; i < len1; i++) { if (this->_str[i] > s._str[i]) return true; if (this->_str[i] <s._str[i]) return false; } if (this->_size>s._size) return true; else return false; } bool operator==(const String& s) { size_t i = 0; size_t len1 = strlen(this->_str); size_t len2 = strlen(s._str); if (len2 !=len1) return false; for (; i < len1; i++) { if (this->_str[i] > s._str[i]) return false; if (this->_str[i] <s._str[i]) return false; } return true; } bool operator!=(const String& s) { size_t i = 0; size_t len1 = strlen(this->_str); size_t len2 = strlen(s._str); if (len2 != len1) return true; for (; i < len1; i++) { if (this->_str[i] > s._str[i]) return true; if (this->_str[i] <s._str[i]) return true; } return false; } friend ostream& operator<<(ostream& _cout, const String& s) { cout << s._str; return _cout; } private: char* _str; size_t _capacity; size_t _size; };