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

string 模拟与用法

string 用法

string

string 模拟

#pragma once
#include <assert.h>
#include <string.h>
#include <iostream>namespace sjy
{class string{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_t len = strlen(str);_size = len;_capacity = len;_str = new char[_capacity + 1]{ 0 };memcpy(_str, str, sizeof(char) * (_size + 1));}~string(){delete[] _str;_str = nullptr;_size = _capacity = 0;}string(const string& other){_size = other._size;_capacity = other._capacity;_str = new char[_capacity + 1]{ 0 };memcpy(_str, other._str, sizeof(char) * (_size + 1));}string& operator=(const string& other){if (&other != this){string tmp(other);swap(tmp);}return *this;}//size, capacity相关size_t size() const{return _size;}size_t capacity() const{return _capacity;}void resize(size_t n, char c = '\0'){if (n < _size){*(_str + n) = '\0';_size = n;}else{if (n > _capacity){reserve(n);}for (int i = _size; i < n; i++){*(_str + i) = c;}*(_str + n) = '\0';_size = n;}}void reserve(size_t n = 0){if (n > _capacity){_capacity = n;char* tmp = new char[_capacity + 1]{ 0 };memcpy(tmp, _str, sizeof(char) * (_size + 1));delete[] _str;_str = tmp;}}//添加修改相关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 push_back(char c){if (_size == _capacity){reserve(_capacity == 0 ? 4 : 2 * _capacity);}*(_str + _size) = c;_size++;*(_str + _size) = '\0';}void append(const char* str){size_t len = strlen(str);if (_size + len > _capacity){reserve(_size + len);}for (int i = 0; i <= len; i++){*(_str + _size + i) = *(str + i);}_size += len;}string& operator+=(char c){push_back(c);return *this;}string& operator+=(const char* str){append(str);return *this;}void insert(size_t pos, size_t n, char c){assert(pos <= _size);if (_size + n > _capacity){reserve(_size + n);}for (int i = _size + n; i >= pos + n && i != npos; i--){*(_str + i) = *(_str + i - n);}for (int i = 0; i < n; i++){*(_str + pos + i) = c;}_size += n;}void insert(size_t pos, const char* str){assert(pos <= _size);size_t len = strlen(str);if (_size + len > _capacity){reserve(_size + len);}for (int i = _size + len; i >= pos + len && i != npos; i--){*(_str + i) = *(_str + i - len);}for (int i = 0; i < len; i++){*(_str + pos + i) = *(str + i);}_size += len;}//删除相关string& erase(size_t pos = 0, size_t len = npos){assert(pos <= _size);if (len == npos || pos + len >= _size){*(_str + pos) = '\0';_size = pos;}else{for (int i = pos + len; i <= _size; i++){*(_str + i - len) = *(_str + i);}_size -= len;}return *this;}//查找与字符串相关size_t find(char c, size_t pos = 0) const{for (int i = pos; i < _size; i++){if (*(_str + i) == c){return i;}}return npos;}size_t find(const char* str, size_t pos = 0) const{const char* ptr = strstr(_str + pos, str);if (ptr == nullptr){return npos;}else{return ptr - _str;}}string substr(size_t pos = 0, size_t len = npos) const{assert(pos < _size);if (len == npos || pos + len > _size){len = _size - pos;}char* tmp = new char[len + 1]{ 0 };for (int i = 0; i < len; i++){*(tmp + i) = *(_str + pos + i);}string str(tmp);delete[] tmp;return str;}//其他const char* c_str(){return _str;}void clear(){_size = 0;*_str = '\0';}void swap(string& str){std::swap(_str, str._str);std::swap(_size, str._size);std::swap(_capacity, str._capacity);}bool operator<(const string& str) const{int size1 = this->_size;int size2 = str._size;int minsize = size1 < size2 ? size1 : size2;for (int i = 0; i < minsize; i++){if (*(_str + i) > *(str._str + i)){return false;}else if (*(_str + i) < *(str._str + i)){return true;}}if (size1 == size2 || size1 > size2){return false;}else if (size1 < size2){return true;}}bool operator==(const string& str) const{if (_size == str._size && memcmp(_str, str._str, sizeof(char) * _size) == 0){return true;}return false;}bool operator>(const string& str) const{return !((*this) < str || (*this) == str);}bool operator<=(const string& str) const{return ((*this) < str || (*this) == str);}bool operator>=(const string& str) const{return ((*this) > str || (*this) == str);}private:char* _str;size_t _size;size_t _capacity;static size_t npos;};size_t string::npos = -1;std::istream& operator>>(std::istream& cin, string& str){str.clear();char c = cin.get();while (c == ' ' || c == '\0'){c = cin.get();}char buff[128] = { 0 };int i = 0;while (c != ' ' && c != '\0'){buff[i++] = c;if (i == 127){str += buff;i = 0;}c = cin.get();}if (i != 0){*(buff + i) = '\0';str += buff;}return cin;}std::ostream& operator<<(std::ostream& cout, const string& str){for (auto c: str){cout << c;}return cout;}
}
http://www.lryc.cn/news/167857.html

相关文章:

  • [NLP] LLM---<训练中文LLama2(一)>训练一个中文LLama2的步骤
  • 华为云云耀云服务器L实例使用教学 | 利用华为云服务器搭建--> 基于Spring Boot+WebSocket+WebRtc实现的多人自习室
  • Postman应用——接口请求(Get和Post请求)
  • k8s pod概念、分类及策略
  • C++系列-左移运算符重载
  • 【Vue】vue中v-if的用法
  • 企业架构LNMP学习笔记54
  • C【函数】
  • 【简单教程】利用Net2FTP构建免费个人网盘,实现便捷的文件管理
  • 05-Flask-Flask查询路由方式
  • lua环境搭建数据类型
  • c++11的一些新特性
  • K8S名称空间和资源配额
  • 鼠标拖拽拖动盒子时,与盒子内某些点击事件冲突问题解决
  • PMP项目管理证书是什么?有什么用?
  • iframe的父子通讯
  • 使用docker创建minio镜像并上传文件,提供demo
  • 02 java ---- Android 基础app开发
  • 鲁棒性与稳定性区别
  • C++项目实战——基于多设计模式下的同步异步日志系统-⑦-日志输出格式化类设计
  • Android---底部弹窗之BottomSheetDialog
  • Cesium 地球网格构造
  • C++深度优化——cacheline测试
  • 【数字IC/FPGA】Verilog中的递归调用
  • 禁用Win10自动更新
  • 算法通关村-----动态规划高频问题
  • 记一起小意外事件引起的批量重命名文件名
  • 【Excel函数】Excel的Len函数求对象的字符数
  • 小白备战大厂算法笔试(八)——搜索
  • 〔022〕Stable Diffusion 之 生成视频 篇