当前位置: 首页 > news >正文

string模拟实现

当我们已经了解了string类的各种函数及其使用,我们可以用已学的C++知识来模拟实现一下string类,实现一下string类中相关的函数

#pragma once
#include<iostream>
using namespace std;
#include<assert.h>
namespace bit
{class string{friend ostream& operator<<(ostream& out, const bit::string& s);friend istream& operator>>(istream& in, bit::string& s);public:typedef char* iterator;typedef const char* const_iterator;iterator begin(){return _str;}iterator end(){return _str+_size;}const_iterator begin() const{return _str;}const_iterator end() const{return _str + _size;}string(const char* str=""){_size = strlen(str);_capacity = _size;_str = new char[_capacity+1];memcpy(_str, str,_size+1);}string(const string& s){_str = new char[s._capacity +1];_size = s._size;_capacity = s._capacity;memcpy(_str, s._str,_size);}const char* c_str() const{return _str;}size_t size(){return _size;}size_t capacity(){return _capacity;}char& operator[](size_t pos){assert(pos < _size);return _str[pos];}const char& operator[](size_t pos) const{assert(pos < _size);return _str[pos];}void reserve(size_t n){if (n > _capacity){char* tmp = new char[n+1];strcpy(tmp, _str);_capacity = n+1;delete[] _str;_str = tmp;}}void push_back(const char ch){if (_size + 1 > _capacity){reserve(_capacity == 0 ? 4 : _capacity *= 2);}_str[_size] = ch;_size += 1;_str[_size] = '\0';}void append(const char* str){if (_size + strlen(str) > _capacity){reserve(_size + strlen(str));}memcpy(_str + _size, str, _size + strlen(str));_size += strlen(str);}string& operator+=(const char ch){push_back(ch);return *this;}string& operator+=(const char* str){append(str);return *this;}void swap(string& s){std::swap(_str, s._str);std::swap(_size, s._size);std::swap(_capacity, s._capacity);}string& operator=(string str){swap(str);return *this;}void insert(size_t pos,size_t n,const char ch){assert(pos <= _size);if (_size + n > _capacity){reserve(_size + n);}size_t tmp = _size;while (tmp>=pos&&tmp!=npos){_str[tmp + n] = _str[tmp];tmp--;}while (n--){_str[pos] = ch;pos++;}}void insert(size_t pos, const char* str){assert(pos <= _size);size_t n = strlen(str);if (_size + n > _capacity){reserve(_size + n);}size_t tmp = _size;while (tmp >= pos && tmp != npos){_str[tmp + n] = _str[tmp];tmp--;}for (size_t i = 0; i < n; i++){_str[pos] = str[i];pos++;}}void resize(size_t n, char c = '\0'){if (n > _capacity){reserve(n);int len = n - _size;while (len--)_str += c;		}else{if (n > _size){int len = n - _size;while (len--)_str += c;}else{_size = n;}}}void erase(size_t pos, size_t n = npos){assert(pos <= _size);if (n == npos || pos + n >= _size){_str[pos] = '\0';_size = pos;}else{while (n--){for (size_t i = pos; i < _size; i++){_str[i] = _str[i + 1];}//pos++;_size--;}}}string& clear(){_size = 0;_str += '\0';return *this;}size_t find(char ch,size_t pos=0){assert(pos < _size);for (size_t i = pos; i < _size; i++){if (_str[i] == ch)return i;}return npos;}size_t find(const char* str, size_t pos = 0){assert(pos < _size);const char* ptr = strstr(_str+pos, str);if (ptr)return ptr - _str;elsereturn npos;}string substr(size_t pos=0, size_t n = npos){string tmp;if (pos+n >= _size || n == npos){n = _size - pos;}tmp.reserve(n);for (size_t i = pos; i < pos+n; i++){tmp += _str[i];}return tmp;}bool empty(const string& s)const{if (s._size == 0)return true;elsereturn false;}bool operator<(const string& s){int min = s._size <= _size ? s._size : _size;for (int i = 0; i < min; i++){if (s[i] > _str[i])return true;if (s[i] < _str[i])return false;}if (_size < s._size)return true;elsereturn false;}bool operator<=(const string& s){int min = s._size <= _size ? s._size : _size;for (int i = 0; i < min; i++){if (s[i] > _str[i])return true;if (s[i] < _str[i])return false;}if (_size <= s._size)return true;elsereturn false;}bool operator>(const string& s){if ((*this<=s))return false;elsereturn true;}bool operator>=(const string& s){if (*this == s)return true;else if (*this > s)return true;elsereturn false;}bool operator==(const string& s){if (_size != s._size)return false;else{if ((*this < s) || (*this > s))return false;elsereturn true;}}bool operator!=(const string& s){if (*this == s)return false;elsereturn true;}~string(){delete[] _str;_str = nullptr;_size = _capacity = 0;}private:size_t _size;size_t _capacity;char* _str;public:const static size_t npos;};const size_t string::npos = -1;ostream& operator<<(ostream& out, const bit::string& s){for (auto e : s){out << e;}return out;}istream& operator>>(istream & in, bit::string& s){s.clear();char ch = in.get();while (ch == ' ' || ch == '\n'){ch = in.get();}int i = 0;char buff[128];while (ch != '\n'){buff[i] = ch;i++;if (i == 127){s += buff;buff[i] = '\0';i = 0;}ch = in.get();}if (i != 0){buff[i] = '\0';s += buff;}return in;}
}

http://www.lryc.cn/news/584803.html

相关文章:

  • 【Linux】C++项目分层架构:核心三层与关键辅助
  • iOS 数组如何设计线程安全
  • 速学 RocketMQ
  • 较为深入的了解c++中的string类(2)
  • Vue集成MarkDown
  • 在 React Three Fiber 中实现 3D 模型点击扩散波效果
  • CSS和CSS3区别对比
  • 【深度学习新浪潮】什么是AI个性化医疗?
  • 黑马点评系列问题之P55优惠券秒杀 快捷键问题 Ctrl+D显示不出来老师给的界面
  • 【数据结构】8. 二叉树
  • FastAPI + SQLAlchemy (异步版)连接数据库时,对数据进行加密
  • React Three Fiber 实现 3D 模型点击高亮交互的核心技巧
  • Gin 中常见参数解析方法
  • 用TensorFlow进行逻辑回归(二)
  • 闲庭信步使用图像验证平台加速FPGA的开发:第九课——图像插值的FPGA实现
  • 硬件加速(FPGA)
  • BigFoot Decursive 2.7.28 2025.07.11
  • MyBatis插件机制揭秘:从拦截器开发到分页插件实战
  • 深入剖析 ADL:C++ 中的依赖查找机制及其编译错误案例分析
  • Linux面试问题-软件测试
  • RISC-V:开源芯浪潮下的技术突围与职业新赛道 (二) RISC-V架构深度解剖(上)
  • idea如何打开extract surround
  • 【C++】——类和对象(上)
  • Linux指令与权限
  • Navicat实现MySQL数据传输与同步完整指南
  • python正则表达式(小白五分钟从入门到精通)
  • Vue 中监测路由变化时,通常不需要开启深度监听(deep: true)
  • Spring事务管理深度解析:原理、实践与陷阱
  • STM32-ADC
  • squash压缩合并