文章目录
- 一、string类的模拟实现
- 1、构造、拷贝构造、赋值运算符重载以及析构函数
- 2、迭代器类
- 3、增删查改类
- 4、io类
- 5、扩容类
- 6、完整代码
一、string类的模拟实现
1、构造、拷贝构造、赋值运算符重载以及析构函数
2、迭代器类
3、增删查改类
4、io类
5、扩容类
6、完整代码
namespace bit
{
class string
{
public:
typedef char* iterator;
typedef const char* const_iterator;
const_iterator beagin() const
{
return _str;
}
const_iterator end() const
{
return _str + _size;;
}
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
void insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity *= 2);
}
size_t end = _size + 1;
while (end >pos)
{
_str[end] = _str[end-1];
end--;
}
_str[pos] = ch;
_size++;
}
void insert(size_t pos, const char* ch)
{
assert(pos <= _size);
size_t len = strlen(ch);
if (_size + len > _capacity)
{
reserve(_size + len);
}
size_t end = _size;
while (end > pos)
{
_str[end + len] = _str[end];
--end;
}
strncpy(_str + pos, ch, len);
_size += len;
}
void erase(size_t pos, size_t len = npos)
{
assert(pos < _size);
if (len == npos || pos + len > _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
size_t begin = _size + len;
while (begin <= _size)
{
_str[pos++] = _str[begin++];
}
_size -= len;
}
}
void push_back(char ch)
{
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
_str[_size] = ch;
++_size;
_str[_size] = '\0';
}
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+=(char ch)
{
push_back(ch);
return *this;
}
string& operator+=(const char* ch)
{
append(ch);
return *this;
}
string(const char* str = "")
:_size(strlen(str))
,_capacity(_size)
{
_str = new char[_size + 1];
strcpy(_str, str);
}
~string()
{
delete[] _str;
_str = nullptr;
_size = 0;
_capacity = 0;
}
string(const string& s)
:_str(nullptr)
, _size(0)
, _capacity(0)
{
string tmp(s._str);
swap(tmp);
}
string& operator=(string s)
{
swap(s);
return *this;
}
void reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
_str = tmp;
_capacity = n;
}
}
void resize(size_t n,char ch='\0')
{
if (n <= _size)
{
_str[n] = ch;
_size = n;
}
else
{
reserve(n);
while (_size < n)
{
_str[_size++] = ch;
}
_str[_size] = '\0';
}
}
void clear()
{
_str[0] = '\0';
_size = 0;
}
void swap(string& s)
{
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
size_t find(char ch, size_t pos = 0)
{
for (size_t i = pos; i < _size; i++)
{
if (_str[i] == ch)
{
return i;
}
}
return npos;
}
size_t find(const char* sub, size_t pos=0)
{
const char* p = strstr(_str + pos, sub);
if (p)
{
return p - _str;
}
else
{
return npos;
}
}
string substr(size_t pos, size_t len = npos)
{
string s;
size_t end = pos + len;
if (len == npos || pos + len >= _size)
{
len = _size - pos;
end = _size;
}
s.reserve(len);
for (size_t i = pos; i <= end; i++)
{
s += _str[i];
}
return s;
}
private:
char* _str;
size_t _size;
size_t _capacity;
const static size_t npos;
};
const size_t string::npos = -1;
ostream& operator<< (ostream& out, string& s)
{
for (auto ch : s)
{
out << ch;
}
return out;
}
istream& operator>> (istream& in, string& s)
{
s.clear();
char buff[129];
size_t i = 0;
char ch;
ch = in.get();
while (ch != ' ' && ch != '\0')
{
buff[i++] = ch;
if (i == 128)
{
buff[i] = '\0';
s += buff;
i = 0;
}
s += ch;
ch = in.get();
}
if (i != 0)
{
buff[i] = '\0';
s += buff;
}
return in;
}
}