欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

字符串模拟实现(直接在源代码上实现)

最编程 2024-04-27 19:45:14
...
#include<iostream> #include<string> #include<assert.h> using namespace std; namespace bit { class string { friend ostream& operator<<(ostream& _cout, const bit::string& s); friend istream& operator>>(istream& _cin, bit::string& s); public: typedef char* iterator; public: //构造函数 string(const char* str = "") { _str = new char[strlen(str)+1]; for (int i = 0; i < strlen(str); i++) { _str[i] = str[i]; } _size = strlen(str); _str[_size] = '\0'; _capacity = strlen(str); } string(const string& s) { _str = new char[strlen(s._str)+1]; for (int i = 0; i < strlen(s._str); i++) { _str[i] = s._str[i]; } _size = strlen(s._str); _str[_size] = '\0'; _capacity = strlen(s._str); } string& operator=(const string& s); //析构 ~string() { delete[] _str; _str = nullptr; _size = _capacity = 0; } // // iterator iterator begin() { return _str; } iterator end() { return _str + _size; } / // modify void push_back(char c) { if (_size == _capacity) { reserve(_capacity == 0 ? 4 : 2 * _capacity); } _str[_size] = c; ++_size; _str[_size] = '\0'; } string& operator+=(char c); void append(const char* str) { size_t len = strlen(str); if (_size + len > _capacity) { reserve(_size + len); } strcpy(_str + _size, str); _size += len; } string& operator+=(const char* str); void clear() { _size = 0; _str[_size] = '\0'; } void swap(string& s) { char* tempstr = _str; _str = s._str; s._str = tempstr; int tcp = _capacity; _capacity = s._capacity; s._capacity = tcp; int tsz = _size; _size = s._size; s._size = tsz; } const char* c_str()const { return _str; } / // capacity size_t size()const { return _size; } size_t capacity()const { return _capacity; } bool empty()const { return _size == 0; } void resize(size_t n, char c = '\0') { if (n <= _size) { _str[n] = '\0'; _size = n; } else { reserve(n); for (size_t i = _size; i < n; i++) { _str[i] = c; } _str[n] = '\0'; _size = n; } } void reserve(size_t n) { if (n > _capacity) { char* tmp = new char[n + 1]; strcpy(tmp, _str); delete[] _str; _str = tmp; _capacity = n; } } / // access char& operator[](size_t index) { return _str[index]; } const char& operator[](size_t index)const { return _str[index]; } / //relational operators bool operator<(const string& s) { int end1 = _size; int end2 = s._size; while (end1!=0&&end2!=0) { if (_str[end1] < s._str[end2]) { return true; } else if (_str[end1] == s._str[end2]) { end1++; end2++; } else return false; } if (end1 == 0&&end2!=0) { return true; } else { return false; } } bool operator<=(const string& s) { return !(*this > s); } bool operator>(const string& s) { return !(*this < s || *this == s); } bool operator>=(const string& s) { return !(*this < s); } bool operator==(const string& s) { if (_size!= s._size) { return false; } for (int i = 0; i < _size; i++) { if (_str[i] != s._str[i]) { return false; } } return true; } bool operator!=(const string& s) { return !(*this == s); } // 返回c在string中第一次出现的位置 size_t find(char c, size_t pos = 0) const { assert(pos < _size); for (size_t i = pos; i < _size; i++) { if (_str[i] == c) return i; } return npos; } // 返回子串s在string中第一次出现的位置 size_t find(const char* s, size_t pos = 0) const { assert(pos < _size); const char* p = strstr(_str + pos, s); if (p) { return p - _str; } else { return npos; } } // 在pos位置上插入字符c/字符串str,并返回该字符的位置 string& insert(size_t pos, char c) { assert(pos <= _size); // 扩容2倍 if (_size == _capacity) { reserve(_capacity == 0 ? 4 : 2 * _capacity); } size_t end = _size + 1; while (end > pos) { _str[end] = _str[end - 1]; --end; } _str[pos] = c; ++_size; return *this; } string& insert(size_t pos, const char* str) { assert(pos <= _size); size_t len = strlen(str); if (_size + len > _capacity) { // 扩容 reserve(_size + len); } size_t end = _size + len; while (end > pos + len - 1) { _str[end] = _str[end - len]; end--; } strncpy(_str + pos, str, len); _size += len; return *this; } // 删除pos位置上的元素,并返回该元素的下一个位置 string& erase(size_t pos=0, size_t len=npos) { assert(pos < _size); if (len == npos || len >= _size - pos) { _str[pos] = '\0'; _size = pos; } if (len == npos || len >= _size - pos) { _str[pos] = '\0'; _size = pos; } return *this; } private: char* _str; size_t _capacity; size_t _size; public: static const int npos; }; const int string::npos = -1; string& string::operator=(const string& s) { char* tmp = new char[s._capacity + 1]; strcpy(tmp, s._str); delete[] _str; _str = tmp; _size = s._size; _capacity = s._capacity; return *this; } string& string::operator+=(char c) { push_back(c); return *this; } string& string::operator+=(const char* str) { append(str); return *this; } ostream& operator<<(ostream& _cout, const bit::string& s) { _cout << s._str << endl; return _cout; } istream& operator>>(istream& _cin, bit::string& s) { s.clear(); char ch; //in >> ch; ch = _cin.get(); char buff[128]; size_t i = 0; while (ch != ' ' && ch != '\n') { buff[i++] = ch; // [0,126] if (i == 127) { buff[127] = '\0'; s += buff; i = 0; } ch = _cin.get(); } if (i > 0) { buff[i] = '\0'; s += buff; } return _cin; } }