網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
#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
- 上一篇:模擬實(shí)現(xiàn)vector
- 下一篇:初始化列表 static成員
相關(guān)推薦
- 2022-11-28 Kotlin的Collection與Sequence操作異同點(diǎn)詳解_Android
- 2022-05-10 設(shè)備像素比devicePixelRatio
- 2022-07-04 聯(lián)邦學(xué)習(xí)FedAvg中模型聚合過(guò)程的理解分析_其它綜合
- 2022-07-27 flask實(shí)現(xiàn)python方法轉(zhuǎn)換服務(wù)的方法_python
- 2022-07-28 PyTorch實(shí)現(xiàn)手寫(xiě)數(shù)字識(shí)別的示例代碼_python
- 2022-05-27 Python?內(nèi)置函數(shù)sorted()的用法_python
- 2022-04-14 k8s安裝過(guò)程遇到的問(wèn)題
- 2023-05-23 Django?事務(wù)回滾的具體實(shí)現(xiàn)_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支