#pragma once
#include <string.h>
#include <iostream>
#include <cassert>
#include <cstdio>
using namespace std;
namespace xzw
{
class string
{
private:
char *_str;
size_t _size;
size_t _capacity;
static const size_t npos = -1;
public:
typedef char *iterator;
typedef const char *const_iterator;
const_iterator begin() const
{
return _str;
}
const_iterator end() const
{
return _str + _size;
}
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
string(const char *str = "")
: _size(strlen(str)), _capacity(_size)
{
_str = new char[_capacity + 1];
strcpy(_str, str);
}
const char *c_str()
{
return _str;
}
string(const string &s)
: _str(nullptr)
,
_size(0), _capacity(0)
{
string tmp(s._str);
swap(_str, tmp._str);
swap(_size, tmp._size);
swap(_capacity, tmp._capacity);
}
string operator=(string s)
{
swap(_str, s._str);
swap(_size, s._size);
swap(_capacity, s._capacity);
return *this;
}
string operator=(const char*s)
{
_str=nullptr;
string tmp(s);
swap(tmp._str,_str);
swap(_size,tmp._size);
swap(_capacity,tmp._capacity);
return *this;
}
~string()
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
size_t size() const
{
return _size;
}
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 reserve(size_t n)
{
if (n > _capacity)
{
char *tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
void push_back(char ch)
{
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
_str[_size] = ch;
_size++;
_str[_size] = '\0';
}
void resize(size_t n, char ch = '\0')
{
if (n < _size)
{
_str[n] = '\0';
_size = n;
}
else if (_size < n && _capacity > n)
{
int rest = n - _capacity;
while (rest--)
{
push_back(ch);
}
}
else if (_size < n && _capacity < n)
{
int rest = n - _capacity;
reserve(n);
while (rest--)
{
push_back(ch);
}
}
}
void append(const char *str)
{
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
strcpy(_str + _size, str);
_size += len;
}
string &operator+=(const char *str)
{
append(str);
return *this;
}
string &operator+=(const char ch)
{
this->push_back(ch);
return *this;
}
size_t find(char ch)
{
for (size_t i = 0; i < _size; i++)
{
if (_str[i] == ch)
{
return i;
}
}
return npos;
}
size_t find(const char *s, size_t pos = 0)
{
char *ptr = strstr(_str + pos, s);
if (ptr == nullptr)
{
return npos;
}
else
return ptr - _str;
}
string &insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
if (pos == 0)
{
string s;
s += ch;
s += _str;
swap(s._str, _str);
_size++;
return *this;
}
else
{
size_t end = _size + 1;
while (end > pos)
{
_str[end] = _str[end - 1];
end--;
}
_str[pos] = ch;
_size++;
_str[_size] = '\0';
return *this;
}
}
string &insert(size_t pos, const char *s)
{
size_t len = strlen(s);
if (len + _size > _capacity)
{
reserve(len + _capacity);
}
size_t end = _size + len;
while (end >= pos+len)
{
_str[end] = _str[end - len];
end--;
}
strncpy(_str+pos,s,len);
_size+=len;
return *this;
}
string& erase(size_t pos,size_t len=npos)
{
assert(pos<_size);
size_t rest=_size-pos;
if(len==npos||len>=rest)
{
_str[pos]='\0';
_size=pos;
}
else
{
strncpy(_str+pos,_str+pos+len,_size-pos-len);
_str[pos+len]='\0';
_size=pos+len;
}
return *this;
}
};
ostream& operator<<(ostream&out,const string& s)
{
for(auto ch:s)
{
out<<ch;
}
return out;
}
istream& operator>>(istream&in,string &s)
{
char ch=in.get();
while(ch !=' '||ch!='\n')
{
s+=ch;
ch=in.get();
}
return in;
}
void test_string1()
{
string s("hello ");
string s2(s);
string s3("hello ");
s = s3;
s = "alcd";
printf("sdahello,world a\n");
}
void test_string2()
{
string s("hello world");
string s1;
cout << s.c_str() << endl;
cout << s1.c_str() << endl;
s[0] = 'd';
s[2] = 'g';
for (size_t i = 0; i < s.size(); i++)
{
cout << s[i] << " ";
}
cout << endl;
}
void test_string3()
{
string s("hello world");
string::iterator it = s.begin();
while (it != s.end())
{
*it += 2;
it++;
}
it = s.begin();
while (it != s.end())
{
cout << *it << " ";
it++;
}
cout << endl;
for (auto e : s)
{
cout << e << " ";
}
cout << endl;
}
void test_string4()
{
string s("hello world");
s.push_back(' ');
s.push_back('@');
s.push_back('j');
s.append("hellllo worlldddd");
string s1;
s1 += 'f';
s1 += "hfih";
cout << s1.c_str() << endl;
}
void test_string5()
{
string s("abcdefg");
s.resize(8, 'b');
size_t pos = s.find("cd");
cout << pos << endl;
s.insert(0, 'l');
s.insert(0,"abdh");
cout << s.c_str() << endl;
s.resize(2);
cout << s.c_str() << endl;
}
void test_string6()
{
string s="hello";
s.insert(0,详情"ab");
cout<<s.c_str()<<endl;
string s2;
cin>>s2;
cout<<s2<<endl;
}
}
string模拟实现的代码详情
|