C++ STL string模拟实现全解析
C STL string 模拟实现下1. 迭代器实现class MyString { // ... 成员变量声明 public: using iterator char*; using const_iterator const char*; iterator begin() noexcept { return _str; } iterator end() noexcept { return _str _size; } const_iterator cbegin() const noexcept { return _str; } const_iterator cend() const noexcept { return _str _size; } };2. 查找操作实现size_t find(char ch, size_t pos 0) const { for (size_t i pos; i _size; i) { if (_str[i] ch) return i; } return npos; } size_t find(const char* s, size_t pos 0) const { const size_t len strlen(s); if (len 0 || pos len _size) return npos; for (size_t i pos; i _size - len; i) { if (strncmp(_str i, s, len) 0) return i; } return npos; }http://my.tv.sohu.com/us/443549799/712652965.shtmlhttps://tv.sohu.com/v/dXMvNDQzNTQ5Nzk5LzcxMjY1Mjk2NS5zaHRtbA.htmlhttp://my.tv.sohu.com/us/443549799/712653011.shtmlhttps://tv.sohu.com/v/dXMvNDQzNTQ5Nzk5LzcxMjY1MzAxMS5zaHRtbA.htmlhttp://my.tv.sohu.com/us/443549799/712652863.shtmlhttps://tv.sohu.com/v/dXMvNDQzNTQ5Nzk5LzcxMjY1Mjg2My5zaHRtbA.htmlhttp://my.tv.sohu.com/us/443549799/712652969.shtmlhttps://tv.sohu.com/v/dXMvNDQzNTQ5Nzk5LzcxMjY1Mjk2OS5zaHRtbA.htmlhttp://my.tv.sohu.com/us/443549799/712652869.shtmlhttps://tv.sohu.com/v/dXMvNDQzNTQ5Nzk5LzcxMjY1Mjg2OS5zaHRtbA.htmlhttp://my.tv.sohu.com/us/443549799/712652877.shtmlhttps://tv.sohu.com/v/dXMvNDQzNTQ5Nzk5LzcxMjY1Mjg3Ny5zaHRtbA.htmlhttp://my.tv.sohu.com/us/443549799/712653146.shtmlhttps://tv.sohu.com/v/dXMvNDQzNTQ5Nzk5LzcxMjY1MzE0Ni5zaHRtbA.htmlhttp://my.tv.sohu.com/us/443549799/712653027.shtmlhttps://tv.sohu.com/v/dXMvNDQzNTQ5Nzk5LzcxMjY1MzAyNy5zaHRtbA.htmlhttp://my.tv.sohu.com/us/443549799/712653030.shtmlhttps://tv.sohu.com/v/dXMvNDQzNTQ5Nzk5LzcxMjY1MzAzMC5zaHRtbA.htmlhttp://my.tv.sohu.com/us/443549799/712653153.shtmlhttps://tv.sohu.com/v/dXMvNDQzNTQ5Nzk5LzcxMjY1MzE1My5zaHRtbA.html3. 子串操作实现MyString substr(size_t pos 0, size_t len npos) const { if (pos _size) throw std::out_of_range(Invalid position); len (len npos) ? _size - pos : std::min(len, _size - pos); MyString result; result.reserve(len); result._size len; strncpy(result._str, _str pos, len); result._str[len] \0; return result; }4. 替换操作实现MyString replace(size_t pos, size_t len, const char* s) { if (pos _size) throw std::out_of_range(Invalid position); len std::min(len, _size - pos); const size_t new_len strlen(s); const size_t total _size - len new_len; if (total _capacity) reserve(total * 2); // 移动原有数据 memmove(_str pos new_len, _str pos len, _size - pos - len 1); // 插入新内容 strncpy(_str pos, s, new_len); _size total; return *this; }http://my.tv.sohu.com/us/443549799/712652965.shtmlhttps://tv.sohu.com/v/dXMvNDQzNTQ5Nzk5LzcxMjY1Mjk2NS5zaHRtbA.htmlhttp://my.tv.sohu.com/us/443549799/712653011.shtmlhttps://tv.sohu.com/v/dXMvNDQzNTQ5Nzk5LzcxMjY1MzAxMS5zaHRtbA.htmlhttp://my.tv.sohu.com/us/443549799/712652863.shtmlhttps://tv.sohu.com/v/dXMvNDQzNTQ5Nzk5LzcxMjY1Mjg2My5zaHRtbA.htmlhttp://my.tv.sohu.com/us/443549799/712652969.shtmlhttps://tv.sohu.com/v/dXMvNDQzNTQ5Nzk5LzcxMjY1Mjk2OS5zaHRtbA.htmlhttp://my.tv.sohu.com/us/443549799/712652869.shtmlhttps://tv.sohu.com/v/dXMvNDQzNTQ5Nzk5LzcxMjY1Mjg2OS5zaHRtbA.htmlhttp://my.tv.sohu.com/us/443549799/712652877.shtmlhttps://tv.sohu.com/v/dXMvNDQzNTQ5Nzk5LzcxMjY1Mjg3Ny5zaHRtbA.htmlhttp://my.tv.sohu.com/us/443549799/712653146.shtmlhttps://tv.sohu.com/v/dXMvNDQzNTQ5Nzk5LzcxMjY1MzE0Ni5zaHRtbA.htmlhttp://my.tv.sohu.com/us/443549799/712653027.shtmlhttps://tv.sohu.com/v/dXMvNDQzNTQ5Nzk5LzcxMjY1MzAyNy5zaHRtbA.htmlhttp://my.tv.sohu.com/us/443549799/712653030.shtmlhttps://tv.sohu.com/v/dXMvNDQzNTQ5Nzk5LzcxMjY1MzAzMC5zaHRtbA.htmlhttp://my.tv.sohu.com/us/443549799/712653153.shtmlhttps://tv.sohu.com/v/dXMvNDQzNTQ5Nzk5LzcxMjY1MzE1My5zaHRtbA.html5. 运算符重载MyString operator(char ch) { push_back(ch); return *this; } MyString operator(const char* s) { append(s); return *this; } friend std::ostream operator(std::ostream os, const MyString str) { return os str.c_str(); } friend std::istream operator(std::istream is, MyString str) { str.clear(); char ch; while (is.get(ch) !isspace(ch)) { str.push_back(ch); } return is; }6. 完整类框架class MyString { private: char* _str nullptr; size_t _size 0; size_t _capacity 0; static const size_t npos -1; void reserve(size_t new_cap) { if (new_cap _capacity) return; char* new_str new char[new_cap 1]; if (_str) { strcpy(new_str, _str); delete[] _str; } _str new_str; _capacity new_cap; } public: // 构造/析构/拷贝等详见上篇 // 迭代器见上文 // 查找/替换/子串见上文 // 运算符重载见上文 void clear() noexcept { if (_str) _str[0] \0; _size 0; } const char* c_str() const noexcept { return _str ? _str : ; } size_t size() const noexcept { return _size; } bool empty() const noexcept { return _size 0; } };关键设计要点写时复制优化实际STL实现采用引用计数减少拷贝短字符串优化小字符串直接存储在对象内部异常安全关键操作提供强异常保证内存管理使用内存池提升分配效率完整实现需处理边界条件、异常安全和C11移动语义等特性。实际STL实现更加复杂包含SSOSmall String Optimization等优化技术。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2551772.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!