当前位置: 首页 > 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.dtcms.com/a/274604.html

相关文章:

  • 信号肽预测工具PrediSi本地化
  • 《打破预设的编码逻辑:Ruby元编程的动态方法艺术》
  • 内存踩踏全解析:原理 + 实战案例 + 项目排查技巧
  • 2025十大免费销售管理软件推荐
  • 基于物联网的智能体重秤设计与实现
  • 测试第一定律
  • 如何通过公网IP访问部署在kubernetes中的服务?
  • AVL平衡二叉树
  • 为什么必须掌握Java异常处理机制?——从代码健壮性到面试必考题全解析
  • 阿里云服务器,CentOS7.9上安装YApi 接口管理平台
  • Linux修炼:权限
  • vue2往vue3升级需要注意的点(个人建议非必要别直接升级)
  • 基于规则匹配的文档标题召回
  • Leaflet面试题及答案(21-40)
  • PHT-CAD 笔记
  • 【每日算法】专题八_分治_归并排序
  • k8s新增jupyter服务
  • 7.11 dp 图
  • 企业IT管理——医院数据备份与存储制度模板
  • spring-ai RAG(Retrieval-Augmented Generation)
  • 【网络】Linux 内核优化实战 - net.netfilter.nf_conntrack_max
  • 【网络】Linux 内核优化实战 - net.netfilter.nf_conntrack_buckets
  • 基于深度学习的人类活动识别模型研究:HAR-DeepConvLG的设计与应用
  • C++,从汇编角度看《虚拟继承的邪恶》
  • 多模态联邦学习
  • STM32F103ZET6 TFTLCD显示图片
  • Docker构建银河麒麟arm架构redis镜像
  • Windows下基于docker desktop 使用Claude code
  • MySQL中使用group_concat遇到的问题及解决
  • 容器管理: 单机用Docker Compose,多机用Kubernetes