string类是我们平时经常使用的一种类,他是c++中的字符串。通过自己手动写代码实现string类可以有效促进我们对string类的理解。
初始化/销毁函数
string(const char* str = "");
string(const string& s);
string& operator=(const string& s);
~string();
string类的初始化销毁函数包括构造函数,析构函数,赋值重载函数以及拷贝构造函数。大部分的初始化函数都差不多,需要为_str申请空间,如果是原来_str内部有数据,那么申请空间之前应该先把旧空间给销毁了。此处有个点,就是申请空间的时候需要多申请一字节,这是因为字符串自己的属性,字符串必然是以\0结尾,在后续的数据添加函数也是同理。接着拷贝数据,将初始化的数据拷贝给该类。并计算原本的数据数量赋值给_size和_capacity。析构函数则要简单的多,直接析构即可。
string::string(const char* str )
{
assert(str);
_str = new char [strlen(str)+1];
_size = strlen(str);
strcpy(_str, str);
_capacity = _size;
}
string::string(const string& s)
:_str(nullptr),
_size(0),
_capacity(0)
{
delete[]_str;
_str = new char[s._capacity+1];
strcpy(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}
string& string::operator=(const string& s)
{
if (&s != this)
{
delete[]_str;
_str = new char[s._capacity];
strcpy(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}
return *this;
}
string::~string()
{
delete[]_str;
_capacity = _size = 0;
}
空间更改函数
void clear();
void resize(size_t n, char c = '\0');
void reserve(size_t n);
数据添加函数
void push_back(char c);
void append(const char* str);
string& operator+=(char c);
string& operator+=(const char* str);
void operator+=(string s);
数据添加函数包括push_back,append以及"+="运算符重载。其大部分也是大同小异,基本遵循:看空间够不够,不够就开辟新空间,接着就是把字符放进去。需要注意的是,在_size的后面一个位置应当放一个'\0'代表字符串结束。
void string::push_back(char x)
{
if (_size >= _capacity)
{
this->reserve(2 * _capacity+1);
}
_str[_size] = x;
_str[_size + 1] = '\0';
_size++;
}
void string::append(const char* x)
{
if (x == nullptr)
{
return;
}
string s(x);
size_t len = strlen(x);
if (_size + len >= _capacity)
{
this->reserve(_size + len > 2 * _capacity ? len : 2 * _capacity);
_capacity = _size + len > 2 * _capacity ? len : 2 * _capacity;
}
for (char ch : s)
{
_str[_size++] = ch;
}
string& string::operator+=(char c)
{
if (_size + 1 >= _capacity)
{
this->reserve(2 * _capacity);
_capacity = 2 * _capacity;
}
_str[_size] = c;
_str[_size + 1] = '\0';
return *this;
}
string& string::operator+=(const char* str)
{
this->append(str);
return *this;
}
void string::operator+=(string s)
{
if (s == nullptr)
return;
append(s.c_str());
return;
}
插入删除函数
string& insert(size_t pos, char c);
string& insert(size_t pos, const char* str);
string& erase(size_t pos, size_t len);
插入函数与数据添加函数类似,需要先判断空间是否足够。接着,插入函数需要挪数据,将指定位置的数据一个一个往后挪,接着再把数据插入。需要注意的是,当我们完成所有的数据插入后,千万不要忘记'\0'。 删除函数则是反过来,先判断位置是否越界,如果位置大于_size,那么就是尾删,在尾部删除数据。接着把从_size位置的数据一个一个往前面挪。然后调整_size和\0的位置。
string& string::insert(size_t pos, char c)
{
if (pos > _size)
{
pos = _size;
}
if (_size == _capacity)
{
this->reserve(_capacity == 0 ? 4: 2 * _capacity);
_capacity = 2 * _capacity;
}
size_t n = pos;
for (size_t i = _size; i > pos; --i)
{
_str[i] = _str[i - 1];
}
_str[pos] = c;
_size++;
_str[_size] = '\0';
return *this;
}
string& string::insert(size_t pos, const char* str)
{
if (pos > _size)
{
*this += str;
return *this;
}
size_t len = strlen(str);
if (_size == _capacity)
{
this->reserve(2 * _capacity > len ? 2*_capacity :len);
_capacity = 2 * _capacity;
}
size_t n = pos;
for (size_t i = _size; i > pos+len; --i)
{
_str[i] = _str[i - 1];
}
for (size_t i = pos; i < pos + len; i++)
{
_str[i] = str[i - pos];
}
return *this;
}
string& string::erase(size_t pos, size_t len)
{
if (pos > _size)
{
pos = _size;
len = 1;
}
size_t count = len;
while(count--)
for (size_t i = pos; i<_size; i++)
{
_str[i] = _str[i+1];
}
_size -= len;
_str[_size] = '\0';
return *this;
}
}
数据获取函数
const char* c_str()const;//获取_srt的指针
size_t size()const;//获取_size
size_t capacity()const;//获取_capacity
bool empty()const;//判断是否为空
char& operator[](size_t index);//获取并可以修改
const char& operator[](size_t index)const;//获取但是不可以修改
size_t find(char c, size_t pos = 0) const;//获取符号c第一次出现的位置
size_t find(const char* s, size_t pos = 0) const;//获取字符串s第一次出现的位置
数据获取函数,前面几个基本是简单的获取相关的数据,后续几个find函数也没什么难度。
size_t string::capacity()const
{
return _capacity;
}
size_t string::size() const
{
return _size;
}
bool string::empty() const
{
if (_size == 0 && _capacity == 0)
return true;
return false;
}
char& string::operator[](size_t n)
{
return _str[n];
}
const char& string::operator[](size_t n) const
{
return _str[n];
}
const char* string::c_str() const
{
return _str;
}
size_t string::find(char x, size_t pos) const
{
if (pos >= size()) {
return npos;
}
for (size_t i = pos; i < size(); ++i)
{
if (_str[i] == x) {
return i;
}
}
return npos;
}
size_t string::find(const char* s1, size_t pos) const
{
string s(s1);
char* ch = strstr(_str+pos, s._str);
return ch?ch-_str :-1;
}
迭代器与其他函数
iterator begin();
iterator end();
string substr(int pos, size_t len);
void clear();
void swap(string& s);
bool operator<(const string& s);
bool operator<=(const string& s);
bool operator>(const string& s);
bool operator>=(const string& s);
bool operator==(const string& s);
bool operator!=(const string& s);
这些其他函数里面大部分是比较函数,底层大部分都是用strcmp实现。swap函数就是交换,clear是将所有数据清除,但是不会缩小空间。substr函数可以切断从pos位置开始,往后len个的字符串,
string string::substr(int pos, size_t len)
{
if (pos < 0 || pos >= _size) return *this;
if (len > _size - pos)
{
len = _size - pos;
}
char* str = new char[len+1];
int count = 0;
strncpy(str, _str + pos, len);
string s1(str);
s1._str[len+1] = '\0';
delete[] str;
return s1;
}
bool string::operator<(const string& s)
{
if(strcmp(s.c_str(), _str) < 0)
return false;
return true;
}
bool string::operator>(const string& s)
{
if (strcmp(s.c_str(), _str) > 0)
return true;
return false;
}
bool string::operator==(const string& s)
{
if (strcmp(s.c_str(), _str) == 0)
return true;
return false;
}
bool string::operator!=(const string& s)
{
if (strcmp(s.c_str(), _str) != 0)
return true;
return false;
}
bool string::operator<=(const string& s)
{
if (strcmp(s.c_str(), _str) >= 0)
return true;
return false;
}
bool string::operator>=(const string& s)
{
if (strcmp(s.c_str(), _str) <= 0)
return true;
return false;
}
void string::swap(string& s)
{
if (this != &s)
{
std::swap(_str,s._str);
std::swap(_size, s._size);
std::swap(_capacity ,s._capacity);
}
}
void string::clear()
{
_size = 0;
_str[0] = '\0';
}
iterator string::begin()
{
return &_str[0];
}
iterator string::end()
{
return &_str[_size+1];
}