当前位置:网站首页>Implementation string mystring
Implementation string mystring
2022-06-27 20:34:00 【Eat oranges aaaaa】
#include <vector>
#include<iostream> //istream ,ostream
#include<assert.h>
#include<fstream>
using namespace std;
class MyString
{
private:
struct StrNode
{
int ref;// Reference count ( Indicates how many customers are using )
int len;// Character length
int capc;// Capacity
char data[0];// Flexible array ( Or to 0, Or give nothing )
};
enum{ALGIN=8};// enumeration
private:
StrNode* pstr;// Pointer to structure type
size_t round_up(size_t n)// The improvement of its way
{
return (n + ALGIN - 1) & ~(ALGIN - 1);
//return ((n - 1) / ALGIN + 1) * ALGIN; Such as :1,2,3,4,5,6,7,8=>8 ; 9,10,11,...,16=>16
}
static StrNode* M_Malloc(size_t size)//size Space for flexible arrays
{
// encapsulation malloc, And the tail information
StrNode* s = (StrNode*)malloc(sizeof(StrNode) + sizeof(char) * size);
if (nullptr == s)
{
exit(1);
}
return s;
}
static StrNode* M_Realloc(StrNode* p, size_t size)
{
StrNode* s = (StrNode*)realloc(p, sizeof(StrNode) + sizeof(char) * size);
if (nullptr == s)
{
exit(1);
}
s->capc = size-1;
return s;
}
static StrNode* cloneStrNode(StrNode* ps)// clone
{
StrNode* s = M_Malloc(ps->capc + 1);
memmove(s, ps, sizeof(StrNode) + ps->len + 1);
s->ref = 1;
s->len =ps->len;
s->capc = ps->capc;
return s;
}
public:
typedef char value_type;// The value type is set to char
typedef char* iterator; // The iterator is set to char
typedef const char* const_iterator;// Constant iterator , Can only point to you , Can't modify you
public:
iterator begin()
{
return pstr->data;
}
iterator end()
{
return pstr->data + pstr->len;
}
const iterator begin() const
{
return pstr->data;
}
const iterator end() const
{
return pstr->data + pstr->len;
}
public:
void clear() // Clearing is to reduce the reference count , When the reference count is 0 when , To be eligible for release
{
if (nullptr != pstr && --pstr->ref == 0)
{
free(pstr);
}
pstr = nullptr;
}
// Capacity function
size_t size() const // The number of strings
{
//assert(pstr != nullptr);
return (pstr != nullptr) ? pstr->len : 0;
}
size_t length() const { return size(); }
bool empty() const{ return size() == 0; }
size_t capacity() const
{
return (pstr != nullptr) ? pstr->capc : 0;
}
void reserve(size_t newsz);// Keep the storage function
// Element access function
char& at(const int index) // Copy while writing
{
return operator[](index);
}
const char& at(const int index) const
{
return operator[](index);
}
char& operator[](const int index)// Overload subscript operators
{
assert(pstr != nullptr && index >= 0 && index < pstr->len);
if (pstr->ref > 1)
{
pstr->ref -= 1;
pstr = cloneStrNode(pstr);
}
else
{
return pstr->data[index];
}
}
const char& operator[](const int index) const
{
assert(pstr != nullptr && index >= 0 && index < pstr->len);
return pstr->data[index];
}
char& front()// Access the first character
{
return at(0);
}
const char& front() const
{
return at(0);
}
char& back()// Access the last character
{
return at(pstr->len - 1);//return at(size()-1);
}
const char& back()const
{
return at(size() - 1);
}
const char* c_str() const
{
return (pstr == nullptr) ? nullptr : pstr->data;
}
const char* data() const
{
return (pstr == nullptr) ? nullptr : pstr->data;
}
char* data()
{
return (pstr == nullptr) ? nullptr : pstr->data;
}
public:
MyString(const char* p = nullptr) :pstr(nullptr)// Constructors , And initialize it
{
if (nullptr != p)
{
int n = strlen(p);
int total = round_up(2 * n);// Upgrade to 8 Multiple
//int total = 2* n;
pstr = M_Malloc(total);//total Is the size of the flexible array
pstr->ref = 1;
pstr->len = n;
pstr->capc = total - 1;
strcpy_s(pstr->data, total, p);
}
}
~MyString() // Destructor
{
/*if (nullptr != pstr && --pstr->ref == 0)// Indicates a structure that points to a flexible array , And the reference count is 0( This string is no longer used )
{
free(pstr);
}
pstr = nullptr;
*/
clear();
}
MyString(const MyString& s) :pstr(s.pstr)// Shallow copy constructors ( When the reference count is 0 when , Will die )
{
if (pstr != nullptr)
{
pstr->ref += 1;
}
}
MyString& operator=(const MyString& sc) // The mutator
{
if (this == &sc||this->pstr==sc.pstr) return *this;// Assign yourself a value , It doesn't make any sense
clear();//s1=s2, Before assignment , First the s1 eliminate
pstr = sc.pstr;
if (pstr != nullptr)
{
pstr->ref += 1;
}
return *this;
}
MyString(MyString&& sc):pstr(sc.pstr)// Mobile construction ( Give resources in heap space to another space , The reference count does not change )
{
sc.pstr = nullptr;
}
MyString& operator=(MyString&& sc)// Move assignment
{
if (this == &sc) return *this;
clear();// Release your own resources first
pstr = sc.pstr;
sc.pstr = nullptr;// After moving , Resource empty , Then empty it
return *this;
}//s1=std::move(s2);
ostream& operator<<(ostream& out) const // Output stream
{
if (pstr != nullptr)
{
out << pstr->data;
}
return out;
}
void Print() const
{
if (pstr != nullptr)
{
cout << pstr->data << endl;
}
}
MyString operator+=(const MyString& sc) ;// Add string objects
MyString operator+=(const char* p);// Add string constants
MyString& operator += (const char ch)// Append character
{
char str[2] = { ch,'\0' };
if (pstr == nullptr)
{
*this= MyString(str);
}
else if(pstr->ref==1)
{
if (pstr->capc - pstr->len >= 0)
{
pstr->data[pstr->len] = ch;
pstr->data[++pstr->len] = '\0';
}
else
{
pstr = M_Realloc(pstr, round_up(pstr->len * 2));// Capacity expansion
}
}
else
{
pstr->ref -= 1;
pstr = cloneStrNode(pstr);
if (pstr->capc - pstr->len >= 0)
{
pstr->data[pstr->len] = ch;
pstr->data[++pstr->len] = '\0';
}
else
{
pstr = M_Realloc(pstr, round_up(pstr->len * 2));// Capacity expansion
}
}
return *this;
}
};
MyString operator+(const char* p, const MyString& sc) ;// Character constants are added to string objects
ostream& operator<<(ostream& out, const MyString& s)// Global output function
{
s << out;//s.operator<<(out); operator<<(s,out);
return out;
}
int main()
{
MyString s1("yhping");
MyString::iterator it = s1.begin();
for (; it != s1.end(); ++it)
{
//cout << *it << endl;
}
for (char ch:s1)// Range for
{
cout << ch << endl;
}
size_t n = s1.size();
for (int i = 0; i<n; i++)
{
cout << s1.at(i) << " ";
//cout << s1[i] << " ";
}
s1.Print();
const char* str = s1.c_str();
if (str != nullptr)
{
cout << str << endl;
}
return 0;
}Running results :

边栏推荐
- Installation and configuration of grayog new generation log collection early warning system
- linux系统笑着玩Oracle数据库多表查询-连接查询
- DBeaver恢复和备份数据库的方式
- What is a stack?
- Type the URL to the web page display. What happened during this period?
- Mobile low code development theme month | visual development one click generation of professional source code
- CSDN skill tree experience and product analysis (1)
- UOS prompts for password to unlock your login key ring solution
- Explore gaussdb and listen to what customers and partners say
- OpenSSL client programming: SSL session failure caused by an obscure function
猜你喜欢

低代码开发平台是什么?为什么现在那么火?

Select auto increment or sequence for primary key selection?

难怪大家丢掉了postman而选择 Apifox

谈谈我写作生涯的画图技巧

At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions

muduo

云原生存储解决方案Rook-Ceph与Rainbond结合的实践

Oracle 架构汇总

Type the URL to the web page display. What happened during this period?

MongoDB简介及典型应用场景
随机推荐
pfSense Plus22.01中文定制版发布
一场分销裂变活动,不止是发发朋友圈这么简单
Grasp the detailed procedure of function call stack from instruction reading
As a software engineer, give advice to young people (Part 2)
润迈德医疗开启招股:未有基石投资者参与,亏损金额翻倍增长
NVIDIA三件套环境配置
蓄力中台,用友iuap筑牢社会级企业数智化新底座
Installation and configuration of grayog new generation log collection early warning system
刷题记录:Easy 数组(持续更新)
ABAP essays - interview memories hope that everyone's demand will not increase and the number of people will soar
数仓的字符截取三胞胎:substrb、substr、substring
A distribution fission activity is more than just a circle of friends
Accumulating power in the middle stage, UFIDA IUAP builds a new base for digital intelligence of social enterprises
字典树(复习)
Paste source layer and history layer of data warehouse system
[required reading for high-quality products] sub query of Oracle database in Linux system
谈谈我写作生涯的画图技巧
Graylog 新一代日志收集预警系统安装配置
Redis持久化
SQL Server - window function - solve the problem of filtering consecutive n records