1 Star 0 Fork 0

哈哈 / C语言

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
string.h 7.93 KB
一键复制 编辑 原始数据 按行查看 历史
哈哈 提交于 2023-10-30 18:50 . string练习
#pragma once
#include<assert.h>
namespace wdz
{
class string
{
friend ostream& operator<<(ostream& _cout, const wdz::string& s);
friend istream& operator>>(istream& _cin, wdz::string& s);
public:
typedef char* iterator;
public:
string(const char* str = "")
:_str(nullptr)
, _capacity(strlen(str))
, _size(strlen(str))
{
_str = new char[_capacity + 1];
strcpy(_str, str);
}
string(const string& s)
:_str(nullptr)
, _capacity(0)
, _size(0)
{
string tmp(s._str);
swap(tmp);
}
string& operator=(const string& s)
{
if (this != &s)
{
string tmp(s);
swap(tmp);
}
return *this;
}
~string()
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
//////////////////////////////////////////////////////////////
// iterator
const iterator begin()const
{
return _str;
}
iterator begin()
{
return _str;
}
const iterator end()const
{
return _str + _size;
}
iterator end()
{
return _str + _size;
}
/////////////////////////////////////////////////////////////
// modify
void push_back(char c)
{
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
_str[_size++] = c;
_str[_size] = '\0';
}
string& operator+=(char c)
{
push_back(c);
return *this;
}
void append(const char* str)
{
int len = strlen(str);
if (len + _size > _capacity)
{
reserve(len + _size);
}
strncpy(_str + _size, str, len);
_size += len;
_str[_size] = '\0';
}
string& operator+=(const char* str)
{
append(str);
return *this;
}
void clear()
{
_size = 0;
}
void swap(string& s)
{
std::swap(s._str, _str);
std::swap(s._size, _size);
std::swap(s._capacity, _capacity);
}
const char* c_str()const
{
return _str;
}
/////////////////////////////////////////////////////////////
// capacity
size_t size()const
{
return _size;
}
size_t capacity()const
{
return _capacity;
}
bool empty()const
{
return _size == 0;
}
void resize(size_t n, char c = '\0')
{
if (n <= _size)
{
_str[_size] = '\0';
_size = n;
}
else
{
if (n > _capacity)
{
reserve(n);
}
while (_size < n)
{
_str[_size++] = c;
}
_str[_size] = '\0';
}
}
void reserve(size_t n)
{
if (n > _capacity)
{
char* ptr = new char[n + 1];
strcpy(ptr, _str);
delete[] _str;
_str = ptr;
_capacity = n;
}
}
/////////////////////////////////////////////////////////////
// access
char& operator[](size_t index)
{
assert(index < _size);
return _str[index];
}
const char& operator[](size_t index)const
{
assert(index < _size);
return _str[index];
}
/////////////////////////////////////////////////////////////
//relational operators
bool operator<(const string& s)
{
return strcmp(_str, s._str) < 0;
}
bool operator<=(const string& s)
{
return *this < s || *this == s;
}
bool operator>(const string& s)
{
return !(*this <= s);
}
bool operator>=(const string& s)
{
return !(*this < s);
}
bool operator==(const string& s)
{
return strcmp(_str, s._str) == 0;
}
bool operator!=(const string& s)
{
return !(*this == s);
}
// 返回c在string中第一次出现的位置
size_t find(char c, size_t pos = 0) const
{
for (size_t i = 0 + pos; i < _size; i++)
{
if (_str[i] == c)
{
return i;
}
}
return npos;
}
// 返回子串s在string中第一次出现的位置
size_t find(const char* s, size_t pos = 0) const
{
char* ptr = strstr(_str + pos, s);
if (ptr == nullptr)
{
return npos;
}
else
{
return ptr - _str;
}
}
// 在pos位置上插入字符c/字符串str,并返回该字符的位置
string& insert(size_t pos, char c)
{
assert(pos <= _size);
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : 2 * _capacity);
}
int end = _size;
while (end >= (int)pos)
{
_str[end + 1] = _str[end];
end--;
}
_size++;
_str[pos] = c;
}
string& insert(size_t pos, const char* str)
{
assert(pos <= _size);
int len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
int end = _size;
while (end >= (int)pos)
{
_str[end + len] = _str[end];
end--;
}
_size += len;
strncpy(_str + pos, str, len);
return *this;
}
// 删除pos位置上的元素,并返回该元素的下一个位置
string& erase(size_t pos, size_t len = npos)
{
assert(pos < _size);
if (len == npos || len >= _capacity - _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
}
public:
static const size_t npos = -1;
private:
char* _str;
size_t _capacity;
size_t _size;
};
ostream& operator<<(ostream& _cout, const wdz::string& s)
{
for (auto& ch : s)
{
_cout << ch ;
}
return _cout;
}
istream& operator>>(istream& _cin, wdz::string& s)
{
s.clear();
char buff[129] = { 0 };
size_t i = 0;
char ch = _cin.get();
while (ch != '\n' && ch != ' ')
{
buff[i++] = ch;
if (i == 128)
{
buff[i] = '\0';
s += buff;
i = 0;
}
ch = _cin.get();
}
if (i != 0)
{
buff[i] = '\0';
s += buff;
}
return _cin;
}
void test1()
{
cout << wdz::string::npos << endl;
wdz::string s;
cin >> s;
cout << s << endl;
}
void test2()
{
string s1("hello world");
s1.insert(0, "wdz");
cout << s1 << endl;
s1.insert(14, "wdz");
cout << s1 << endl;
}
};
C
1
https://gitee.com/bigbog/c-language.git
git@gitee.com:bigbog/c-language.git
bigbog
c-language
C语言
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891