模拟实现C++中string的常规操作
Posted L_add
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模拟实现C++中string的常规操作相关的知识,希望对你有一定的参考价值。
reserve
void reserve(size_t n)//开空间,扩展capacity
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
resize
void resize(size_t n,char val = '\\0')//开空间+初始化,扩展capacity,且初始化空间
{
//n < size
if (n < _size)
{
_size = n;
_str[_size] = '\\0';
}
else
{
//n > capacity
if (n > _capacity)
{
reserve(n);
}
// size < n < capacity
for (size_t i = _size; i < n; i++)
{
_str[i] = val;
}
_str[n] = '\\0';
_size = n;
}
}
push_back
void push_back(char ch)
{
/*if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
_str[_size] = ch;
_str[_size + 1] = '\\0';
_size++;*/
insert(_size, ch);
}
append
void append(const char* str)
{
/*size_t len = _size + strlen(str);
if (len > _capacity)
{
reserve(len);
}
strcpy(_str + _size, str);
_size = len;*/
insert(_size, str);
}
insert
string& insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size = _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
//不推荐
/*int end = _size;
while (end > (int)pos)
{
_str[end - 1] = _str[end];
end--;
}*/
/*size_t end = _size + 1;
while (end > pos)
{
_str[end] = _str[end - 1];
end--;
}*/
//还可以用指针
char* end = _str + _size;
while (end >= _str + pos)
{
*(end + 1) = *end;
end--;
}
_str[pos] = ch;
_size++;
return *this;
}
string& insert(size_t pos, const char *str)
{
assert(pos <= _size);
size_t len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
//挪动数据
char* end = _str + _size;
while (end >= _str + pos)
{
*(end + len) = *end;
end--;
}
strncpy(_str + pos, str, len);
_size += len;
return *this;
}
+=
string& operator += (char ch)
{
push_back(ch);
return *this;
}
string& operator += (const char *_str)
{
append(_str);
return *this;
}
[](改)
const char& operator[](size_t i) const// 读
{
assert(i < _size);
return _str[i];
}
char& operator[](size_t i)//可读可写
{
assert(i < _size);
return _str[i];
}
find
size_t find(char ch, size_t pos = 0)
{
assert(pos < _size);
for (size_t i = pos; i < _size; i++)
{
if (_str[i] == ch)
{
return i;
}
}
return npos;
}
size_t find(const char* str, size_t pos = 0)
{
assert(pos < _size);
const char* ret = strstr(_str + pos, str);
if (ret)
{
return ret - _str;
}
else
{
return npos;
}
}
erase
string& erase(size_t pos, size_t len = npos)//npos = -1
{
assert(pos < _size);
size_t leftlen = _size - pos;
//剩余的字符长度小于要删的长度
if (len >= leftlen)
{
_str[pos] = '\\0';
_size = pos;
}
else//剩余的字符长度大于要删的长度
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
return *this;
}
其他
size()
size_t size() const
{
return _size;
}
c_str()
const char* c_str() const
{
return _str;
}
其余代码
class string
{
public:
typedef char* iterator;
typedef const char* const_iterator;
iterator begin()
{
return _str;
}
const_iterator begin() const
{
return _str;
}
iterator end()
{
return _str + _size;
}
const_iterator end() const
{
return _str + _size;
}
string(const char* str = "")
{
_size = strlen(str);//_size最后一位留给\\0
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, str);
}
void swap(string& s)
{
::swap(_str, s._str);
::swap(_size, s._size);
::swap(_capacity, s._capacity);
}
string(const string& s)
:_str(nullptr)
, _size(0)
, _capacity(0)
{
string tmp(s._str);//构造
/* swap(_str, tmp._str);
swap(_size, tmp._size);
swap(_capacity, tmp._capacity);*/
//this->swap(tmp)
swap(tmp);
}
string& operator= (string s)
{
/*swap(_str, s._str);
swap(_size, s._size);
swap(_capacity, s._capacity);*/
swap(s);
return *this;
}
~string()
{
delete[] _str;
_str = nullptr;
_size = 0;
_capacity = 0;
}
比较
inline bool operator <(const string& s1, const string s2)
{
return strcmp(s1.c_str(),s2.c_str()) < 0;
}
inline bool operator ==(const string& s1, const string s2)
{
return strcmp(s1.c_str(), s2.c_str()) == 0;
}
inline bool operator <=(const string& s1, const string s2)
{
return s1 < s2 || s1 == s2;
}
inline bool operator >(const string& s1, const string s2)
{
return !(s1 <= s2);
}
inline bool operator >=(const string& s1, const string s2)
{
return !(s1 < s2);
}
inline bool operator !=(const string& s1, const string s2)
{
return !(s1 == s2);
}
<<
ostream& operator <<(ostream& out, const string&s)
{
for (auto ch : s)
{
out << ch;
}
return out;
}
>> / getline
istream& operator >>(istream&in, string& s)
{
s.clear();
char ch;
ch = in.get();
while(ch != ' '&& ch != '\\n')
{
s += ch;
ch = in.get();
}
return in;
}
istream& getline(istream& in, string& s)
{
s.clear();
char ch;
ch = in.get();
while ( ch != '\\n')
{
s += ch;
ch = in.get();
}
return in;
}
以上是关于模拟实现C++中string的常规操作的主要内容,如果未能解决你的问题,请参考以下文章
C++初阶:string类string类 | 浅拷贝和深拷贝(传统写法和现代写法) | string类的模拟实现
C++初阶:STL —— stringstring类 | 浅拷贝和深拷贝(传统写法和现代写法) | string类的模拟实现
C++初阶:STL —— stringstring类 | 浅拷贝和深拷贝(传统写法和现代写法) | string类的模拟实现