#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<assert.h>
using namespace std;
namespace mystring
{
class string
{
public:
//static int npos;
typedef char* iterator;
typedef const char* const_iterator;
string(const char*s="")
{
_size = strlen(s);
_capcity = _size;
_ch= new char[_size+1];
strcpy(_ch, s);
}
iterator begin()
{
return _ch;
}
iterator end()
{
return _ch + _size;
}
const_iterator cbegin() const
{
return _ch;
}
const_iterator cend() const
{
return _ch + _size;
}
void reserve(int pos)
{
if (pos <= _size)
{
_size = pos;
_ch[_size] = '\0';
}
else
{
char* tmp = new char[pos + 1];
strcpy(tmp, _ch);
_ch = tmp;
_capcity = pos + 1;
}
}
void resize(int pos, char s)
{
if (pos <= _size)
{
_size = pos;
_ch[_size] = '\0';
}
else if(pos>_capcity)
{
reserve(pos);
memset(_ch + _size, s, pos - _size);
_size = pos;
_ch[_size] = '\0';
}
}
void Swap(string& s)
{
std::swap(_ch, s._ch);
std::swap(_size, s._size);
std::swap(_capcity, s._capcity);
}
//s1(s2)使用构造函数
string(string& s)
:_ch(nullptr)
,_size(0)
,_capcity(0)
{
string tmp(s._ch);
Swap(tmp);
}
//s1=s2;
//使用构造函数
string& operator=(string& s)
{
Swap(s);
return *this;
}
void push_back(char s)
{
if (_size == _capcity)
{
int newcapcity = _capcity == 0 ? 4: _capcity * 2;
reserve(newcapcity);
}
_ch[_size] = s;
_size++;
_ch[_size] = '\0';
}
void pop_back()
{
_size--;
_ch[_size] = '\0';
}
char& operator[](int pos)
{
assert(pos <= _size);
return _ch[pos];
}
string& append(const char* s,int pos)
{
int len = strlen(s);
if (_size+len >=_capcity)
{
int newcapcity = _capcity == 0 ? _size + len :len + _capcity;
reserve(newcapcity);
}
int end = _size;
while (end >= pos)
{
_ch[end + len] = _ch[end];
if (end > 0)
{
end--;
}
}
strncpy(_ch + pos, s, len);
return *this;
}
string& insert(int pos,char s)
{
assert(pos < _size);
if (_size+1 == _capcity)
{
int newcapcity = _capcity == 0 ? _size + 1 : 1 + _capcity;
reserve(newcapcity);
}
int end = _size;
while (end >=pos)
{
_ch[end + 1] = _ch[end];
if (end > 0)
{
end--;
}
}
_ch[pos] = s;
_size++;
return *this;
}
string& erase(int pos = 0, int len =npos)
{
assert(pos < _size);
if (pos==npos||pos+len>=_size)
{
_ch[pos] = '\0';
_size = pos;
}
else
{
strncpy(_ch + pos, _ch + pos + len,len);
_size -= len;
}
return *this;
}
const char* c_str()const //前const 表示返回常量,后面的const 表示返回值不能修改
{
return this->_ch;
}
~string()
{
delete[] _ch;
_ch = nullptr;
_size = 0;
_capcity = 0;
}
private:
char* _ch;
int _size;
int _capcity;
static int npos;
};
int mystring::string::npos = -1;
void test()
{
/*mystring::string s("laskdf");
cout << s.c_str() << endl;
mystring::string::iterator it = s.begin();
while (it != s.end())
{
cout << *it<< endl;
it++;
}
mystring::string s1(s);
cout << s1.c_str();
mystring::string s2("123");
mystring::string s3("456");
s2 = s3;
cout << s2.c_str();*/
string s4;
s4.push_back('a');
s4.push_back('a');
s4.push_back('a');
s4.push_back('a');
//s4.append("love you",1);
// cout << s4.c_str();
s4.insert(1,'i');
cout << s4.c_str();
}
}
int main()
{
mystring::test();
return 0;
}