代码拉取完成,页面将自动刷新
#include"string.h"
size_t bit::string::npos = -1;
bit::string::iterator bit::string::begin()
{
return _str;
}
bit::string::iterator bit::string::end()
{
return _str + _size;
}
bit::string::const_iterator bit::string::begin() const
{
return _str;
}
bit::string::const_iterator bit::string::end() const
{
return _str + _size;
}
/*bit::string& bit::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;
}*/
bit::string& bit::string::operator=(string s)
{
swap(s);
return *this;
}
bit::string& bit::string::operator=(char c)
{
*this += c;
return *this;
}
//modify
void bit::string::reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
void bit::string::push_back(char c)
{
/*if(_size==_capacity)
{
size_t capacity= _capacity==0? 4 : 2*_capacity;
reserve(capacity);
}
_str[_size]=c;
++_size;
_str[_size]='\0';
*/
insert(_size, c);
}
bit::string& bit::string::operator+=(char c)
{
push_back(c);
return *this;
}
void bit::string::append(const char* str)
{
/* size_t len = strlen(str);
if(_size + len > _capacity)
{
reserve(_size + len);
}
strcpy(_str+_size, str);
_size = _size + len;
_str[_size] = '\0';
*/
insert(_size, str);
}
bit::string& bit::string::operator+=(const string& str)
{
append(str._str);
return *this;
}
bit::string& bit::string::operator+=(const char* str)
{
append(str);
return *this;
}
void bit::string::clear()
{
_str[0] = '\0';
_size = 0;
}
const char* bit::string::c_str() const
{
return _str;
}
size_t bit::string::size() const
{
return _size;
}
size_t bit::string::capacity() const
{
return _capacity;
}
bool bit::string::empty() const
{
return _capacity == 0;
}
char& bit::string::operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
const char& bit::string::operator[](size_t pos) const
{
assert(pos < _size);
return _str[pos];
}
void bit::string::resize(size_t n, char c)
{
reserve(n);
if (n > _size)
{
//插入
for (size_t i = _size; i < n; i++)
{
_str[i] = c;
}
_str[n] = '\0';
_size = n;
}
else
{
_str[n] = '\0';
_size = n;
}
}
bit::string& bit::string::insert(size_t pos, char c)
{
assert(pos <= _size);
//扩容
if (_size == _capacity)
{
size_t capacity = _capacity == 0 ? 4 : 2 * _capacity;
reserve(capacity);
}
size_t end = _size + 1;
while (end > pos)
{
_str[end] = _str[end - 1];
--end;
}
_str[pos] = c;
++_size;
return *this;
}
bit::string& bit::string::insert(size_t pos, const char* str)
{
assert(pos <= _size);
size_t len = strlen(str);
//扩容
if (_size + len > _capacity)
{
reserve(_size + len);
}
size_t end = _size + len;
while (end > pos)
{
_str[end] = _str[end - 1];
--end;
}
strcpy(_str + pos, str);
_size += len;
return *this;
}
bit::string& bit::string::erase(size_t pos, size_t len)
{
assert(pos < _size);
if (len == npos || len + pos >= _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
return *this;
}
//
bool bit::string::operator<(const string& s)
{
return !(*this >= s);
}
bool bit::string::operator<=(const string& s)
{
return strcmp(_str, s._str) <= 0;
}
bool bit::string::operator>(const string& s)
{
return !(*this <= s);
}
bool bit::string::operator>=(const string& s)
{
return strcmp(_str, s._str) >= 0;
}
bool bit::string::operator==(const string& s)
{
return strcmp(_str, s._str) == 0;
}
bool bit::string::operator!=(const string& s)
{
return !(*this == s);
}
// 返回c在string中第一次出现的位置
size_t bit::string::find(char c, size_t pos) const
{
assert(pos < _size);
for (size_t i = pos; i < pos; ++i)
{
if (_str[i] == c)
{
return i;
}
}
return npos;
}
// 返回子串s在string中第一次出现的位置
size_t bit::string::find(const char* s, size_t pos) const
{
assert(pos < _size);
assert(s);
const char* str = strstr(_str + pos, s);
if (str == nullptr)
{
return npos;
}
else
{
return str - _str;
}
}
// 返回字字符串
bit::string bit::string::substr(size_t pos, size_t len) const
{
assert(pos < _size);
size_t reallen = len;
if (len == npos || pos + len > _size)
{
reallen = _size - pos;
}
else
{
reallen -= pos;
}
string sub;
for (size_t i = pos; i < reallen; ++i)
{
sub += _str + i;
}
return sub;
}
ostream& bit::operator<<(ostream& out, const string& s)
{
for (size_t i = 0; i < s.size(); ++i)
{
out << s[i];
}
return out;
}
istream& bit::operator>>(istream& in, string& s)
{
//当字符串很长的时候,会不断扩容,效率底。
char ch;
ch = in.get();
while ((ch != ' ') || (ch != '\n'))
{
s += ch;
ch = in.get();
}
return in;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。