string模拟实现代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
namespace xcj
{
class string
{
public:
typedef char* iterator;
iterator begin()//起始位置
{
return _str;
}
iterator end()// \0位置
{
return _str+_size;
}
string(const char* str = "")//构造函数
:_size(strlen(str))
,_capacity(_size)
{
_str = new char[_capacity + 1];
strcpy(_str, str);
}
//拷贝构造
string(const string& s)
{
_str = new char[s._capacity+1];
strcpy(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}
//赋值
string& operator=(const string& s)
{
if (this != &s)
{
char* tmp = new char[s._capacity + 1];
strcpy(tmp, s._str);
delete[] _str;
_str = tmp;
_size = s._size;
_capacity = s._capacity;
}
return *this;
}
~string()//析构函数
{
delete[]_str;
_str = nullptr;
_size = _capacity = 0;
}
const char* c_str()
{
return _str;
}
char& operator[](size_t pos)//重定义[]
{
//读写
assert(pos < _size);
return _str[pos];
}
const char& operator[](size_t pos) const //重定义[]
{
//只读不写
assert(pos < _size);
return _str[pos];
}
size_t capacity()const
{
return _capacity;
}
size_t size()const
{
return _size;
}
void reserve(size_t n)//增容
{
if (n > _capacity)
{
//开辟新的更大的空间 再把_str拷贝过去
char* tmp = new char[n + 1];
strcpy(tmp, _str);
//释放原有_str
delete[]_str;
_str = tmp;
_capacity = n;
}
}
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)//从pos位置取长度为len的字符串
{
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;
}
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* str)//重定义+=
{
append(str);
return *this;
}
void insert(size_t pos, char ch) //在pos位置插ch
{
assert(pos <= _size);
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : (_capacity * 2));
}
//当前这种方法不能进行头插end会变成-1,进行类型提升后会死循环
/*size_t end = _size;
while (end >= pos)
{
_str[end + 1] = _str[end];
--end;
}*/
//解决方法1
/*int end = _size;
while (end >= (int)pos)
{
_str[end + 1] = _str[end];
--end;
}*/
//解决方法2
size_t end = _size;
while (end > pos)
{
_str[end] = _str[end-1];
--end;
}
_str[pos] = ch;
_size++;
}
void insert(size_t pos, const char* str)//在pos位置插入str字符串
{
assert(pos <= _size);
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
int end = _size;
while (end >= (int)pos)
{
_str[end + len] = _str[end];
--end;
}
strncpy(_str + pos, str,len);
_size += len;
}
void erase(size_t pos, size_t len=npos)//在pos位置删除长度为npos的字符
{
assert(pos <= _size);
if (len == npos || len+pos>=_size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
size_t begin = pos + len;
while (begin <= _size)
{
_str[pos] = _str[begin];
++begin;
++pos;
}
_size -= len;
}
}
void resize(size_t n, char ch='\0')
{
if (n <= _size)//小于_size时是删除
{
_str[n] = '\0';
_size = n;
}
else
{
//大于_size时是尾插
reserve(n);
while (_size < n)
{
_str[_size] = ch;
++_size;
}
_str[_size] = '\0';
}
}
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);
}
void clear()
{
_str[0] = '\0';
_size = 0;
}
private:
char* _str;
size_t _size;
size_t _capacity;
public:
//只针对const静态整型可以在类中 定义初始化
//const static size_t npos=-1;//特例
const static size_t npos;
};
const size_t string:: npos = -1;
ostream& operator<<(ostream& out, const string& s)
{
for (size_t i = 0; i < s.size(); i++)
{
out << s[i];
}
return out;
}
istream& operator>>(istream& in, string& s)
{
s.clear();
char buff[129];
size_t i = 0;
char ch;
//in >> ch; 会拿不到空格或者换行
ch = in.get();
while (ch != ' ' && ch != '\n')
{
buff[i++] = ch;
if (i == 128)
{
buff[i] = '\0';
s += buff;
i = 0;
}
//in >> ch;
ch = in.get();
}
if (i != 0)
{
buff[i] = '\0';
s += buff;
}
return in;
}
}