namespace zwj
{
class string
{
public:
// 迭代器
typedef char* iterator;
typedef const char* const_iterator;
// 迭代器
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
const_iterator begin() const
{
return _str;
}
const_iterator end() const
{
return _str + _size;
}
// 默认构造
string(const char* str = "")
{
int n = strlen(str);
_str = new char[n + 1];
strcpy(_str, str);
_size = _capacity = n;
}
// 拷贝构造
string(const string& str)
: _str(nullptr)
, _size(0)
, _capacity(0)
{
string temp(str.c_str());
swap(temp);
}
// 析构函数
~string()
{
delete[] _str;
_size = _capacity = 0;
}
// 赋值,传统写法
//string& operator=(const string& s)
//{
// if (this != &s)
// {
// //string tmp(s);
// //swap(tmp);
// char* temp = new char[s.capacity() + 1]; // 一定要记得多开一个空间,不然会报错
// strcpy(temp, s._str);
// ::swap(temp, _str);
// _size = s.size();
// _capacity = s.capacity();
// delete[] temp;
// }
// return *this;
//}
赋值,现代写法
//string& operator=(const string& s)
//{
// if (this != &s)
// {
// string tmp(s);
// swap(tmp);
// }
// return *this;
//}
string& operator=(string s)
{
swap(s);
return *this;
}
// 扩容
void reserve(size_t n = 0)
{
if (n < _size)
{
_size = n;
_str[_size] = '\0';
}
else
{
char* temp = new char[n + 1];
strcpy(temp, _str);
::swap(temp, _str);
delete[] temp;
_capacity = n;
}
}
// 重载+=
string& operator+= (const char* s)
{
int len = strlen(s);
if (_size + len > _capacity)
{
reserve(_size + len);
}
strcpy(_str + _size, s);
_size += len;
return *this;
}
string& operator+= (const char ch)
{
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : 2 * _capacity);
}
_str[_size++] = ch;
_str[_size] = '\0';
return *this;
}
// append追加
string& append(const char* s)
{
*this += s;
return *this;
}
string& append(const char s)
{
*this += s;
return *this;
}
// 尾插
void push_back(char c)
{
*this += c;
}
// 任意位置删除
string& erase(size_t pos = 0, size_t len = npos)
{
assert(pos >= 0 && pos < _size);
assert(pos != npos);
if (len == npos || len > _size - pos)
{
_str[pos] = '\0';
_size = pos;
}
else
{
size_t end = pos + len;
while (end <= _size)
{
_str[end - len] = _str[end];
end++;
}
_size -= len;
}
return *this;
}
// 任意位置插入字符串
string& insert(size_t pos, const char* s)
{
int len = strlen(s);
if (_size + len > _capacity)
{
reserve(_size + len);
}
size_t end = _size;
while (end >= pos)
{
_str[end + len] = _str[end];
end--;
if (end == -1) break;
}
strncpy(_str + pos, s, len);
_size += len;
return *this;
}
string& insert(size_t pos, const char ch)
{
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : 2 * _capacity);
}
size_t end = _size;
while (end >= pos)
{
_str[end + 1] = _str[end];
end--;
if (end == -1) break;
}
_str[pos] = ch;
_size++;
return *this;
}
// 交换
void swap(string& str)
{
::swap(_str, str._str);
::swap(_size, str._size);
::swap(_capacity, str._capacity);
}
// []重载
char operator[](size_t pos) const
{
assert(pos >= 0 && pos < _size);
return _str[pos];
}
// 返回C形式的字符串
char* c_str() const
{
return _str;
}
// 容量
size_t capacity() const
{
return _capacity;
}
// 大小
size_t size() const
{
return _size;
}
// 查找字符
size_t find(char ch, size_t pos = 0) const
{
assert(pos < _size);
for (size_t i = pos; i < _size; i++)
if (ch == _str[i]) return i;
return npos;
}
// 查找字符串
size_t find(const char* sub, size_t pos = 0) const
{
assert(pos < _size);
char* ptr = strstr(_str, sub);
if (ptr == nullptr)
return npos;
return ptr - _str;
}
// 反向查找字符
size_t rfind(char c, size_t pos = npos) const
{
size_t end = pos;
if (pos > _size)
{
end = _size;
}
while (end >= 0)
{
if (_str[end] == c) return end;
end--;
if (end == -1) break;
}
return npos;
}
// 反向查找字符串
size_t rfind(const char* s, size_t pos = npos) const
{
size_t end = pos;
if (end > _size)
end = _size;
int len = strlen(s);
size_t idx = 0;
while (idx < end)
{
char* ptr = strstr(_str + idx, s);
if (idx == 0 && ptr == nullptr)
return npos;
if (ptr != nullptr)
{
idx = ptr - _str;
if (idx + len == _size)
return idx;
}
else
return idx;
}
return npos;
}
//比较运算符
bool operator>(const string& s) const
{
return strcmp(_str, s._str) > 0;
}
bool operator==(const string& s) const
{
return strcmp(_str, s._str) == 0;
}
bool operator>=(const string& s) const
{
return *this > s || *this == s;
}
bool operator<=(const string& s) const
{
return !(*this > s);
}
bool operator<(const string& s) const
{
return !(*this >= s);
}
bool operator!=(const string& s) const
{
return !(*this == s);
}
// 生成子串
string substr(size_t pos = 0, size_t len = npos) const
{
assert(pos < _size);
if (len == npos || pos + len >= _size)
return _str + pos;
string s;
for (size_t i = pos; i <= pos + len; i++)
s += _str[i];
return s;
}
private:
char* _str;
size_t _size;
size_t _capacity;
const static size_t npos = -1;
};
// 重载流插入
ostream& operator<<(ostream& out, const string& s)
{
for (auto e : s)
out << e;
return out;
}
// 重载流提取
istream& operator>>(istream& in, string& s)
{
char ch;
in.get(ch);
while (ch != ' ' && ch != '\n')
{
s += ch;
in.get(ch);
}
return in;
}
}