網站首頁 編程語言 正文
#include<cstring>
#include <iostream>
#include<assert.h>
using namespace std;
namespace zjq
{
class string
{
public:
string()
: _size(0)
,_capacity(0)
{
_s = new char[_size + 1];
strcpy(_s, "");
}
string(const char* s)
:_size(strlen(s))
,_capacity(_size)
{
_s = new char[_size + 1];
strcpy(_s, s);
}
string(const string& s)
:_size(strlen(s._s))
,_capacity(_size)
{
_s = new char[_size + 1];
strcpy(_s, s._s);
}
string& operator=(const string& s)
{
if (this == &s)
{
return *this;
}
delete[] _s;
_capacity = s._capacity;
_size = s._size;
_s = new char[_size +1];
strcpy(_s, s._s);
return *this;
}
const char* c_str()const
{
return _s;
}
char& operator[](size_t pos)
{
return _s[pos];
}
const char& operator[](size_t pos)const
{
return _s[pos];
}
typedef char* iterator;
typedef const char* const_iterator;
iterator begin()
{
return _s;
}
const_iterator begin() const
{
return _s;
}
iterator end()
{
return _s + _size;
}
const_iterator end() const
{
return _s + _size;
}
void reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n+1];
strcpy(tmp, _s);
delete[] _s;
_s = tmp;
_capacity = n;
}
}
void resize(size_t n,char ch='\0')
{
if (n > _capacity)
{
reserve(n);
}
if (n <= _size)
{
_s[n] = '\0';
}
else
{
for (size_t i = _size; i < n; i++)
{
_s[i] = ch;
}
_size = n;
_s[_size] = '\0';
}
}
//增
void push_back(char ch)
{
if (_size >= _capacity)
{
reserve(2 * _capacity + 1);
}
_s[_size] = ch;
++_size;
_s[_size] = '\0';
}
string& operator+=(char ch)
{
push_back(ch);
return *this;
}
void append(const char* s)
{
size_t len = strlen(s);
if (_capacity <= len + _size)
{
reserve(len + _size);
}
strcpy(_s + _size, s);
_size += len;
}
string operator+=(const char* s)
{
append(s);
return *this;
}
//插
string& insert(const char ch, size_t pos = 0)
{
assert(pos <= _size);
if (_size == _capacity)
{
reserve(2 * _size);
}
size_t end = _size + 1;
while (end > pos)
{
_s[end] = _s[end - 1];
end--;
}
_s[pos] = ch;
return *this;
}
string& insert(const char* str, size_t pos = 0)
{
size_t len = strlen(str);
if (len + _size > _capacity)
{
reserve(len + _size);
}
size_t end = _size + len;
while (end > pos + len - 1)
{
_s[end] = _s[end - len];
end--;
}
strncpy(_s + pos, str, len);
_size += len;
return *this;
}
string& erase(size_t pos = 0, size_t len = npos)
{
if (pos + len >= _size || len == npos)
{
_s[pos] = '\0';
}
else
{
size_t end = pos + len;
while (end <= _size)
{
_s[end - len] = _s[end];
end++;
}
}
_size -= len;
return *this;
}
//s1<s2
bool operator<(const string& s)
{
return strcmp(_s, s._s) < 0;
}
bool operator<=(const string& s)
{
return *this < s || *this == s;
}
bool operator>(const string& s)
{
return !(*this <= s);
}
bool operator>=(const string& s)
{
return !(*this < s);
}
bool operator==(const string& s)
{
return strcmp(_s, s._s) == 0;
}
bool operator!=(const string& s)
{
return !(*this == s);
}
//查
size_t find(char ch, size_t pos = 0)
{
for (; pos < _size; pos++)
{
if (_s[pos] == ch)
{
return pos;
}
}
return npos;
}
size_t find(const char* str, size_t pos =0)
{
char* ptr = strstr(pos + _s, str);
if (ptr == nullptr)
{
return npos;
}
else
{
return ptr - _s;
}
}
~string()
{
delete[] _s;
_size = 0;
_capacity = 0;
}
private:
char* _s;
size_t _size;
size_t _capacity;
static size_t npos;
};
size_t string::npos = -1;
ostream& operator<<(ostream& out, const string& s)
{
for (auto ch : s)
{
out << ch;
}
return out;
}
istream& operator>>(istream& in, string& s)
{
char ch;
ch = in.get();
while (ch != ' ' && ch != '\n')
{
s += ch;
ch = in.get();
}
return in;
}
}
原文鏈接:https://blog.csdn.net/zjq_love/article/details/125707284
- 上一篇:模擬實現vector
- 下一篇:初始化列表 static成員
相關推薦
- 2023-03-01 Shell?$[]對整數進行數學運算實現_linux shell
- 2022-06-13 Python自動化辦公之圖片轉PDF的實現_python
- 2022-10-07 C#如何實現調取釘釘考勤接口的功能_C#教程
- 2024-02-01 前端訪問一個地址 地址上帶/api, 如何通過nginx 配置, 讓訪問后臺的時候是/而不是/api
- 2023-11-12 ip link set eno2 down后centos無法聯網;centos7.0,二次啟動后無法
- 2022-06-01 Python正則表達式總結分享_python
- 2022-06-22 使用Git向GitHub上傳更新內容_其它綜合
- 2022-12-11 Go?模塊在下游服務抖動恢復后CPU占用無法恢復原因_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支