hello,各位小伙伴,本篇文章跟大家一起学习《C++:string的的模拟实现》,感谢大家对我上一篇的支持,如有什么问题,还请多多指教 !
如果本篇文章对你有帮助,还请各位点点赞!!!
话不多说,开始进入正题
文章目录
- :heartbeat:模拟实现string头文件
- 1.:maple_leaf:iterator begin();和iterator end();:maple_leaf:const_iterator begin() const;和const_iterator end() const;
- 2.:maple_leaf:构造函数和拷贝构造函数
- :leaves:构造函数
- :leaves:拷贝构造函数
- 3.:maple_leaf:析构函数
- 4.:maple_leaf:赋值重载函数
- 5.:maple_leaf:const char* c_str() const;
- 6.:maple_leaf:size_t size() const;
- 7. :maple_leaf:[]运算符重载
- :leaves:char& operator[](size_t pos);
- :leaves:const char& operator[](size_t pos) const;
- 8.:maple_leaf:void reserve(size_t n);
- 9.:maple_leaf:void push_back(char ch);
- 10.:maple_leaf:void append(const char* str);
- 11.:maple_leaf:+=运算符重载函数
- :leaves:string& operator+=(char ch);
- :leaves:string& operator+=(const char* str);
- 12.:maple_leaf:insert的实现
- :leaves:void insert(size_t pos, char ch);
- :leaves:void insert(size_t pos, const char* str);
- 13.:maple_leaf:void erase(size_t pos = 0, size_t len = npos);
- 14.:maple_leaf:find函数实现
- :leaves:size_t find(char ch, size_t pos);
- :leaves:size_t find(const char* str, size_t pos);
- 15.:maple_leaf:swap函数的实现
- 16.:maple_leaf:substr的实现
- 17.:maple_leaf:运算符重载
- 18.:maple_leaf:>>重载函数和<<重载函数
- :leaves:>>重载函数
- :leaves:<<重载函数
🍁💓
💓模拟实现string头文件
#pragma once #include
#include using namespace std; namespace My_string { class string { public: typedef char* iterator; typedef const char* const_iterator; iterator begin(); iterator end(); const_iterator begin() const; const_iterator end() const; //string(); string(const char* str = ""); string(const string& s); string& operator=(string s); ~string(); const char* c_str() const; size_t size() const; char& operator[](size_t pos); const char& operator[](size_t pos) const; void reserve(size_t n); void push_back(char ch); void append(const char* str); string& operator+=(char ch); string& operator+=(const char* str); void insert(size_t pos, char ch); void insert(size_t pos, const char* str); void erase(size_t pos = 0, size_t len = npos); size_t find(char ch, size_t pos = 0); size_t find(const char* str, size_t pos = 0); void swap(string& s); string substr(size_t pos = 0, size_t len = npos); bool operator<(const string& s) const; bool operator>(const string& s) const; bool operator<=(const string& s) const; bool operator>=(const string& s) const; bool operator==(const string& s) const; bool operator!=(const string& s) const; void clear(); private: // char _buff[16]; char* _str = nullptr; size_t _size = 0; size_t _capacity = 0; const static size_t npos; }; istream& operator>> (istream& is, string& str); ostream& operator<< (ostream& os, const string& str); } 1.🍁iterator begin();和iterator end();🍁const_iterator begin() const;和const_iterator end() const;
之前说过iterator是个类指针的工具,所以我们在这里用指针的方式模拟:
string:: iterator string::begin() { return _str; } string::iterator string::end() { return _str + _size; } string::const_iterator string::begin() const { return _str; } string::const_iterator string::end() const { return _str + _size; }
2.🍁构造函数和拷贝构造函数
🍃构造函数
string::string(const char* str) :_size(strlen(str)) { _str = new char[_size + 1]; _capacity = _size; strcpy(_str, str); }
🍃拷贝构造函数
这里复用构造函数:
string:string(const string& s) { string tmp(s._str); swap(tmp); }
3.🍁析构函数
string::~string() { _size = _capacity = 0; delete[] _str; _str = nullptr; }
4.🍁赋值重载函数
🍃传统写法:
string& string::operator=(const string& s) { if(this != &s) { char* tmp = new char[s._capacity + 1]; strcpy(tmp,s._str); delete[] _str; _size = s._size; _capacity = s._capacity; _str = tmp; } return *this; }
🍃现代写法:
这里用传值传参,形成一个实参的临时拷贝,也就是复用了拷贝构造函数:
string& string::operator=(string s) { swap(s);// 让别人来干活 return *this; }
5.🍁const char* c_str() const;
const char* string::c_str() const { return _str; }
6.🍁size_t size() const;
size_t string::size()const { return _size; }
7. 🍁[]运算符重载
🍃char& operator[](size_t pos);
char& string::operator[](size_t pos) { assert(pos < _size); return _str[pos]; }
🍃const char& operator[](size_t pos) const;
const char& string::operator[](size_t pos) const { assert(pos < _size); return _str[pos]; }
8.🍁void reserve(size_t n);
void string::reserve(size_t n) { if (_capacity < n) { char* tmp = new char[n + 1]; strcpy(tmp,_str); delete[] _str; _str = tmp; _capacity = n; } }
9.🍁void push_back(char ch);
🍃在实现insert之前的写法:
void string::push_back(char ch) { if(_size == _capacity) { size_t newcapacity = _capacity == 0 ? 4 : 2*_capacity; reserve(newcapacity); } _str[_size] = ch; _str[_size + 1] = '\0'; ++_size; }
🍃在实现insert之后的写法:
void string::push_back(char ch) { insert(_size,ch); }
10.🍁void append(const char* str);
🍃在实现insert之前的写法:
void string::append(const char* str) { size_t len - strlen(str); if(_size + len >= _capacity) { size_t newcapacity = _capacity == 0 ? 4 : 2*_capacity; reserve(newcapacity); } strcpy(_str + _size, str); _size += len; }
🍃在实现insert之后的写法:
void string::append(const char* str) { insert(_size,str); }
11.🍁+=运算符重载函数
🍃string& operator+=(char ch);
string& string::operator+=(char ch) { push_back(ch); return *this; }
🍃string& operator+=(const char* str);
string& string::operator+=(const char str) { append(str); return *this; }
12.🍁insert的实现
🍃void insert(size_t pos, char ch);
void string::insert(size_t pos, char ch) { assert(pos <= _size); if(_size == _capacity) { size_t newcapacity = _capacity == 0 ? 4 : 2*_capacity; reserve(newcapacity); } size_t end = _size + 1; while(end > pos) { _str[end] = _str[end - 1]; --end; } _str[pos] = ch; ++_size; }
🍃void insert(size_t pos, const char* str);
void 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) { _str[end] = _str[end - len]; --end; } memcpy(_str + pos,str,len); // 由于strcpy会将后面的字符覆盖,所以这里要用memcpy _size += len; }
13.🍁void erase(size_t pos = 0, size_t len = npos);
const size_t string::npos = -1; void string::erase(size_t pos, size_t len) { assert(_size > pos); if(len > _size - pos) { _str[pos] = '\0'; _size = pos; } else { strcpy(_str + pos, _str + pos + len); _size -= len; } }
14.🍁find函数实现
🍃size_t find(char ch, size_t pos);
size_t string::find(char ch, size_t pos) { assert(_size > pos); int index = pos; // npos在前面有定义 while (index < _size) { if (_str[index] == ch) return index; ++index; } return npos; }
🍃size_t find(const char* str, size_t pos);
size_t string::find(const char* str, size_t pos) { char* p = strstr(_str + pos, str); return p - _str; }
15.🍁swap函数的实现
void string::swap(string& s) { std::swap(_str,s._str); std::swap(_size,s._size); std::swap(_capacity,s._capacity); }
16.🍁substr的实现
string string::substr(size_t pos, size_t len) { if(len >= _size - pos) { string tmp(_str + pos); return tmp; } else { string tmp; tmp.reserve(len); for(int i = 0;i < len;++i) { tmp._str[i] = _str[pos + i]; } // 这么些也可以 // memcpy(tmp._str,_str + pos,len); tmp._size = len; return tmp; } }
17.🍁运算符重载
bool operator<(const string& s) const; bool operator>(const string& s) const; bool operator<=(const string& s) const; bool operator>=(const string& s) const; bool operator==(const string& s) const; bool operator!=(const string& s) const;
bool string::operator<(const string& s) const { return strcmp(_str, s._str) < 0; } bool string::operator>(const string& s) const { return !(_str < s._str); } bool string::operator<=(const string& s) const { return _str < s._str || _str == s._str; } bool string::operator>=(const string& s) const { return !(_str < s._str) || _str == s._str; } bool string::operator==(const string& s) const { return strcmp(_str, s._str) == 0; } bool string::operator!=(const string& s) const { return !(_str == s._str); }
18.🍁>>重载函数和<<重载函数
🍃>>重载函数
由于流提取需要将整个值改变,所以需要清空原本的值,所以引用函数clear()。
由于流提取会自动忽略空格和换行,所以我们用get()函数来解决流提取问题
void string::clear() { _str[0] = '\0'; _size = 0; } istream& My_string::operator>> (istream& is, string& str) { str.clear(); char buff[128]; char ch = is.get(); int i = 0; while (ch != ' ' && ch != '\n') { buff[i] = ch;// 临时变量,能够快速生成 ++i; if (i == 127)// 当buff已经满了,进行+=操作 { buff[i] = '\0'; str += buff; i = 0;// 记得i要置为0 } ch = is.get();// 提取下一个字符 } if (i > 0)// 当还有字符没有进行插入时,继续进行+=操作 { buff[i] = '\0'; str += buff; } return is; }
🍃<<重载函数
ostream& My_string::operator<<(ostream& os,const string& str) { for (size_t i = 0; i < str.size(); i++) { os << str[i]; } return os; }
你学会了吗?
好啦,本章对于《C++:string的的模拟实现》的学习就先到这里,如果有什么问题,还请指教指教,希望本篇文章能够对你有所帮助,我们下一篇见!!!
如你喜欢,点点赞就是对我的支持,感谢感谢!!!
还没有评论,来说两句吧...