cpp中string模拟实现

发布时间:2026/7/8 1:09:30
cpp中string模拟实现 头文件#pragma once#define _CRT_SECURE_NO_WARNINGS#includeiostreamusing namespace std;namespace bit{ostream operator(ostream _cout, const string s);istream operator(istream _cin, string s);class string{friend ostream operator(ostream _cout, const bit::string s);friend istream operator(istream _cin, bit::string s);public:typedef char* iterator;typedef const char* const_iterator;public:string(const char* str );string(const string s);string operator(const string s);~string();//////////////////////////////////////////////////////////////// iteratoriterator begin();iterator end();const_iterator begin() const;const_iterator end() const;/////////////////////////////////////////////////////////////// modifyvoid push_back(char c);string operator(char c);void append(const char* str);string operator(const char* str);void clear();void swap(string s);const char* c_str()const;/////////////////////////////////////////////////////////////// capacitysize_t size()const;size_t capacity()const;bool empty()const;void resize(size_t n, char c \0);void reserve(size_t n);/////////////////////////////////////////////////////////////// accesschar operator[](size_t index);const char operator[](size_t index)const;///////////////////////////////////////////////////////////////relational operatorsbool operator(const string s);bool operator(const string s);bool operator(const string s);bool operator(const string s);bool operator(const string s);bool operator!(const string s);// 返回c在string中第一次出现的位置size_t find(char c, size_t pos 0) const;// 返回子串s在string中第一次出现的位置size_t find(const char* s, size_t pos 0) const;// 在pos位置上插入字符c/字符串str并返回该字符的位置string insert(size_t pos, char c);string insert(size_t pos, const char* str);// 删除pos位置上的元素并返回该元素的下一个位置string erase(size_t pos, size_t len);static const size_t npos -1;private:char* _str;size_t _capacity;size_t _size;};};函数实现文件#includestring.hvoid bit::string::swap(string s){std::swap(this-_str,s._str);std::swap(this-_capacity,s._capacity);std::swap(this-_size,s._size);}ostream bit::operator(ostream _cout, const bit::string s){for (auto ch : s){_cout ch ;}_cout endl;return _cout;}istream bit::operator(istream _cin, bit::string s){s.clear();char ch;while ((_cin ch)){break;}do {s ch;_cin.get(ch);} while (ch ! ch ! \n ch ! \t);s[s.size()] \0;return _cin;}bit::string::string(const char* str){if (str nullptr){_str new char[1];_size _capacity 0;_str[0] \0;return;}//erase 必须写\0就是因为此处用strlen不支持_size_capacity _size strlen(str);_str new char[_capacity 1];strcpy(_str, str);}bit::string::string(const string s){string tmp(s._str);this-swap(tmp);}bit::string bit::string::operator(const string s){string tmp(s._str);swap(tmp);return *this;}bit::string::~string(){delete[] this-_str;}// iteratorbit::string::iterator bit::string::begin(){return this-_str;}bit::string::iterator bit::string::end(){return (this-_str this-_size);}bit::string::const_iterator bit::string::begin() const{return this-_str;}bit::string::const_iterator bit::string::end() const{return (this-_str this-_size);}// modify//void bit::string::push_back(char c){(*this) c;}bit::string bit::string::operator(char c){reserve(_size 1);(*this)[_size] c;(*this)[_size] \0;return *this;}//void append(const char* str);//bit::string bit::string::operator(const char* str){if (str nullptr){return *this;}reserve(_size strlen(str));::strcat(_str, str);_size _size strlen(str);return *this;}void bit::string::clear(){_size 0;}const char* bit::string::c_str()const{return _str;}// capacitysize_t bit::string::size()const{return _size;}size_t bit::string::capacity()const{return _capacity;}bool bit::string::empty()const{return _size 0;}void bit::string::resize(size_t n, char c){reserve(n);if (n _size){_size n;}else{for (size_t i n - _size; i 0; i--){(*this) c;}}(*this)[n] \0;}void bit::string::reserve(size_t n){if (n 0){return;}if (n _capacity){return;}_capacity n 2 * _capacity ? n : 2 * _capacity;char* tmp new char[_capacity 1];strcpy(tmp, _str);delete[] _str;_str tmp;}// accesschar bit::string::operator[](size_t index){return *(_str index);}const char bit::string::operator[](size_t index)const{return *(_str index);}//relational operatorsbool bit::string::operator(const string s){size_t i 0;size_t j 0;for (; i _size || j s._size; i, j){if ((*this)[i] s[j]){return true;}else if ((*this)[i] s[j]){return false;}}if (i _size){return false;}if (j s._size){return true;}}bool bit::string::operator(const string s){return (((*this) s) || ((*this) s));}bool bit::string::operator(const string s){return !((*this) s);}bool bit::string::operator(const string s){return !((*this) s);}bool bit::string::operator(const string s){if (_size ! s._size){return false;}size_t i 0;size_t j 0;for (; i _size || j s._size; i, j){if ((*this)[i] ! s[j]){return false;}}return true;}bool bit::string::operator!(const string s){return !((*this) s);}// 返回c在string中第一次出现的位置size_t bit::string::find(char c, size_t pos) const{for (; pos _size; pos){if ((*this)[pos] c){return pos;}}return npos;}// 返回子串s在string中第一次出现的位置size_t bit::string::find(const char* s, size_t pos) const{if (s nullptr){return npos;}size_t len strlen(s);for (; pos _size - len; pos){if (strncmp((_str pos), s, len) 0){return pos;}}return npos;}// 在pos位置上插入字符c/字符串str并返回该字符的位置bit::string bit::string::insert(size_t pos, char c){if (pos _size){return *this;}reserve(_size 1);size_t i 0;for (i _size; i pos; i--){_str[i] _str[i - 1];}_str[pos] c;_size;_str[_size] \0;return *this;}bit::string bit::string::insert(size_t pos, const char* str){size_t len strlen(str);if (len 0){return (*this);}reserve(_size len);for (size_t i _size len - 1; i pos len; i--){_str[i] _str[i - len];}for (size_t i pos; i pos len; i){_str[i] str[i - pos];}_size len;_str[_size] \0;return (*this);}// 删除pos位置上的元素并返回该元素的下一个位置bit::string bit::string::erase(size_t pos, size_t len){if (pos _size){return (*this);}if (pos len _size){_size pos;_str[pos] \0;return (*this);}for (size_t i pos; i _size - len; i){_str[i] _str[i len];}_size-len;_str[_size] \0;return (*this);}