构造/拷贝构造/赋值运算符重载/析构函数
关于如何使用深拷贝和深赋值来实现:构造/拷贝构造/赋值运算符重载/析构函数,请移步string类的深浅拷贝问题,这里详细介绍了深浅拷贝的问题,并且实现了对应的代码。
相关接口
关于string类有哪些常用接口,请移步STL标准库之string,这里详细介绍了string有哪些常用的接口:大致可以分为五类:构造,容量,元素访问,修改,特殊的非成员函数。
构造
构造方法中可以有几种不同的构造方法:
namespace bite
{
class string
{
public:
typedef char* iterator;
typedef const char* const_iterator;
public:
///
//构造/
//字符串构造
string(const char* str = "")
{
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, str);
}
//字符个数构造
string(size_t n, char ch = '\0')
:_str(new char[n+1])
,_capacity(n)
,_size(n)
{
memset(_str, ch, _size);
_str[_size] = '\0';
}
//迭代器构造
template<class InputIterator>
string(InputIterator begin, InputIterator end)
{
InputIterator it = begin;
size_t count = 0;
while (it != end)
{
++it;
++count;
}
_str = new char[count + 1];
size_t i = 0;
while (begin != end)
{
_str[i] = *begin;
++i;
++begin;
}
_size = count;
_capacity = count;
_str[_size] = '\0';
}
///
//拷贝构造//
string(const string& s)
:_str(nullptr)
,_capacity(0)
,_size(0)
{
string tmp(s._str);
this->swap(tmp);
}
//
//赋值运算符重载///
string& operator=(string s)
{
this->swap(s);
return *this;
}
/
/析构函数/
~string()
{
if (_str)
{
delete[] _str;
_str = nullptr;
_size = 0;
_capacity = 0;
}
}
}
迭代器
//迭代器
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
const const_iterator begin()const
{
return _str;
}
const const_iterator end()const
{
return _str + _size;
}
容量
size_t size()const
{
return _size;
}
size_t length()const
{
return _size;
}
size_t capacity()const
{
return _capacity;
}
bool empty()const
{
return _size == 0;
}
void resize(size_t newsize, char c)
{
size_t oldsize = size();
if(newsize > oldsize)
{
//扩容到newsize
if (newsize > _capacity)
{
reserve(newsize);
}
memset(_str, c, newsize - oldsize);
}
_size = newsize;
_str[_size] = '\0';
}
void resize(size_t n)
{
resize(n, '\0');
}
void reserve(size_t newcapacity = 0)
{
size_t oldcapacity = capacity();
if (newcapacity > oldcapacity)
{
//开辟新空间
char* tmp = new char[newcapacity+1];
//拷贝元素
strcpy(tmp, _str);
//释放旧空间
delete[] _str;
_str = tmp;
_capacity = newcapacity;
}
}
void clear()
{
_size = 0;
}
元素访问
char& operator[](size_t pos)
{
assert(pos < size());
return _str[pos];
}
const char& operator[](size_t pos)const
{
assert(pos < size());
return _str[pos];
}
char& at(size_t pos)
{
if (pos >= size())
{
throw out_of_range("string at index out of range!");
}
return _str[pos];
}
const char& at(size_t pos)const
{
if (pos >= size())
{
throw out_of_range("string at index out of range!");
}
return _str[pos];
}
修改
string& operator+=(const string& s)
{
*this += s.c_str();
return *this;
}
string& operator+=(const char* s)
{
size_t len = strlen(s);
char* tmp = new char[len + _size + 1];
strcpy(tmp, _str);
strcat(tmp, s);
delete[] _str;
_str = tmp;
_size = len + _size;
_capacity = _size;
return *this;
}
string& operator+=(char c)
{
push_back(c);
return *this;
}
void push_back(char c)
{
if (size() >= capacity())
{
reserve((double)capacity() * 1.5);
}
_str[_size] = 'c';
_str[++_size] = '\0';
}
string& append(const string& str)
{
*this += str;
return *this;
}
string& append(const char* s)
{
*this += s;
return *this;
}
string& append(size_t n, char c)
{
for (size_t i = 0; i < n; ++i)
{
push_back(c);
}
_size += n;
_str[_size] = '\0';
return *this;
}
iterator insert(iterator pos, char c)
{
if (pos < begin() || pos > end())
{
return end();
}
if (size() >= capacity())
{
reserve(capacity() * 1.5);
}
//it是要搬移的最后一个元素的位置
iterator it = end() - 1;
while (it >= pos)
{
*(it + 1) = *it;
--it;
}
_size++;
*pos = c;
_str[_size] = '\0';
return pos;
}
iterator erase(iterator pos)
{
if (pos < begin() || pos > end())
{
return end();
}
iterator it = pos + 1;
while (it < end())
{
*(it-1) = *it;
++it;
}
--_size;
_str[_size] = '\0';
return pos;
}
void swap(string& s)
{
std::swap(_capacity, s._capacity);
std::swap(_size, s._size);
std::swap(_str, s._str);
}
特殊的非成员函数
const char* c_str()const
{
return _str;
}
size_t find(char ch, size_t pos = 0)
{
while (pos < size())
{
if (_str[pos] == ch)
{
return pos;
}
++pos;
}
return npos;
}
size_t rfind(char ch, size_t pos = npos)
{
int index = pos;
//当pos没有提供时,需要将pos设置为最后一个元素的下标
if (pos == npos)
index = size() - 1;
while (index >= 0)
{
if (_str[index] == ch)
{
return index;
}
--index;
}
return npos;
}
string substr(size_t pos = 0, size_t n = npos)
{
if (pos >= _size)
{
return "";
}
if (pos + n >= _size)
{
n = _size - pos;
}
return string(_str + pos, _str + n + pos);
}
成员变量
private:
char* _str;
size_t _size;
size_t _capacity;
private:
const static size_t npos = -1;
friend ostream& operator<<(ostream& _cout, const string& s)
{
for (size_t i = 0; i < s.size(); ++i)
{
_cout << s[i];
}
return _cout;
}
};