C++实现String

xiaoxiao2021-02-28  117

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<assert.h> using namespace std; class String { friend ostream&operator<<(ostream&_cout, const String&s); friend istream&operator>>(istream&_cin,String&s); public: String(char* str = "") : _str(new char[strlen(str) + 1]) , _size(strlen(str)) , _capacity(strlen(str) + 1) { strcpy(_str, str); } String(const String& s) :_str(new char[strlen(s._str) + 1]) , _size(strlen(s._str)) , _capacity(strlen(s._str) + 1) { _str = s._str; _size = s._size; _capacity = s._capacity; } String& operator=(String s) { if (*this != s) { _size = s._size; _capacity = s._capacity; swap(_str, s._str); } return *this; } void Swap(String& s) { if (*this != s) { swap(_str, s._str); swap(_size, s._size); swap(_capacity, s._capacity); } } ~String() { if (_str) { delete[] _str; } } char* GetStr() { return _str; } size_t Size() { return _size; } size_t Capacity() { return _capacity; } // 增删查改 void PushBack(char ch) { if (_size >= _capacity-1) { Expand(_capacity*2); } _str[_size] = ch; _size+=1; _str[_size] = '\0'; } void PushBack(const char* str) { size_t len = strlen(str); if (len + _size > _capacity) { Expand(len + _size); } strcpy(_str + _size, str); _size = strlen(_str); } void PopFront() { if (_size > 0) { size_t i = 0; for (i = 0; i < _size; i++) { _str[i] = _str[i + 1]; } _size--; } } void PopBack() { if (_size>0) { _str[_size - 1] = _str[_size]; _size--; } } void Insert(size_t pos, char ch) { if (_size >= _capacity) { Expand(_capacity * 2); } size_t end = _size; while (end >= pos) { _str[end+1] = _str[end]; --end; } _str[pos] = ch; ++_size; } void Insert(size_t pos, const char* str) { size_t len = strlen(str); size_t temp = len; if (_size + len > _capacity) { Expand(_size + len); } size_t end = _size; while (end >= pos) { _str[end + len] = _str[end]; --end; } while (len-- >0) { _str[pos++] = *str++; } _size += temp; } void Erase(size_t pos, size_t count) { if (pos + count >= _size - 1) { _str[pos] = '\0'; _size = pos; } else { strcpy(_str + pos, _str + pos + count); _size -= count; } } size_t Find(char ch) const { for (size_t i = 0; i < _size; ++i) { if (_str[i] == ch) { return 1; } } return -1; } size_t Find(const char* str) const { assert(str); const char* srcStr = _str; const char* subStr = str; size_t subLen = strlen(subStr); size_t srcIndex = 0; size_t subIndex = 0; while (srcIndex < _size - subLen - 1) { size_t matchIndex = srcIndex; while (srcStr[matchIndex] == subStr[subIndex]) { ++matchIndex; ++subIndex; if (subIndex == subLen) return srcIndex; } } } char& operator[](size_t pos) { assert(pos < _size); return _str[pos]; } inline bool operator<(const String& s) const { size_t i = 0; for ( i = 0; i < _size&&i < s._size; ++i) { if (_str[i] < s._str[i]) { return true; } else { return false; } } if (i == _size) { return true; } else { return false; } } 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); } inline bool operator==(const String& s) const { size_t i = 0; for ( i = 0; i<_size&&s._size; ++i) { if (_str[i] != s._str[i]) { return false; } } if (i == _size&&i == s._size) { return true; } else { return false; } } bool operator!=(const String& s)const { return !(*this == s); } void Expand(size_t n)//扩容 { if (n > _capacity) { _str = (char*)realloc(_str, n + 1); assert(_str); _capacity = n+1; cout << "已经扩容" << endl; } } private: char* _str; size_t _size; // 字符个数 size_t _capacity; // 容量空间 }; ostream&operator<<(ostream&_cout, const String&s) { _cout << s._str<<" 字符个数:"<< s._size<<" 容量空间:"<<s._capacity<<endl; return _cout; } istream&operator>>(istream&_cin, String&s) { _cin >> s._str; return _cin; } int main() { String s1; char* s2 = "hello"; s1 = s2; cout << s1; String s3; char*s4 = "worlder"; s3 = s4; cout << s3; s1.Swap(s3); cout << s1; cout << s3; system("pause"); return 0; }
转载请注明原文地址: https://www.6miu.com/read-34872.html

最新回复(0)