1 Star 0 Fork 0

丁旭升/cpp代码

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
string.cpp 5.37 KB
一键复制 编辑 原始数据 按行查看 历史
丁旭升 提交于 2022-08-13 23:00 . 模拟实现string
#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;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/ding-xushengyun/code.git
[email protected]:ding-xushengyun/code.git
ding-xushengyun
code
cpp代码
master

搜索帮助