#pragma once
#include<assert.h>
#include<iostream>
using namespace std;
#pragma warning(disable:4996)
namespace my_string
{
class string
{
public:
string(const char* str = "")
{
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity+1];
strcpy(_str, str);
}
char* c_str() const
{
return _str;
}
/*string(const string& s)
{
_size = s._size;
_capacity = s._capacity;
_str = new char[_capacity + 1];
strcpy(_str, s._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(tmp);
}
/*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;
strcpy(_str, s._str);
}
return *this;
}*/
/*string& operator=(const string& s)
{
string tmp(s._str);
Swap(tmp);
return *this;
}*/
size_t size()const
{
return _size;
}
size_t capacity()
{
return _capacity;
}
string& operator=(string s)
{
Swap(s);
return *this;
}
char& operator[](size_t n)const
{
assert(n < _size);
return _str[n];
}
void reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n+1];
strcpy(tmp, _str);
delete[]_str;
_str = tmp;
_capacity = n;
}
}
void push_back(char ch)
{
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : 2 * _capacity);
}
_str[_size] = ch;
++_size;
_str[_size] = '\0';
}
void append(const char* str)
{
size_t len = strlen(str);
if (_size +len> _capacity)
{
reserve(_size + len);
}
strcpy(_str + _size, str);
_size += len;
}
void append(size_t n, char ch)
{
reserve(n + _size);
int i = 0;
for (i = 0; i < n; i++)
{
push_back(ch);
}
}
string& operator+=(char ch)
{
push_back(ch);
return *this;
}
string& operator+=(const char* str)
{
append(str);
return *this;
}
string& insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size == _capacity)
{
reserve(2 * _capacity);
}
size_t end = _size + 1;
while (end > pos)
{
_str[end] = _str[end - 1];
--end;
}
_str[pos] = ch;
++_size;
return *this;
}
string& insert(size_t pos, const char* str)
{
assert(pos <= _size);
size_t len = strlen(str);
reserve(len + _size);
size_t end = _size + len;
while (end > pos + len-1)
{
_str[end] = _str[end - len];
--end;
}
strncpy(_str + pos, str, len);
_size += len;
return *this;
}
void erase(size_t pos, size_t len = npos)
{
assert(pos < _size);
if (len >= _size - pos)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
}
~string()
{
delete[]_str;
_str = nullptr;
_capacity = _size = 0;
}
/*static void test_string()
{
string s1("12345");
string s2;
string s3;
s2 = s1;
s1 = s1;
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
s2.push_back('x');
cout << s2.c_str() << endl;
s2.append("hello");
cout << s2.c_str() << endl;
s2 += "word";
cout << s2.c_str() << endl;
s2.insert(5, '#');
cout << s2.c_str() << endl;
s2.insert(5, "bit");
cout << s2.c_str() << endl;
s2.erase(5, 3);
cout << s2.c_str() << endl;
s2.erase(5,100);
cout << s2.c_str() << endl;
}*/
private:
char* _str;
size_t _size;
size_t _capacity;
static size_t npos;
};
size_t string:: npos = -1;
ostream& operator<<(ostream& out, const string& s)
{
size_t i = 0;
for (i = 0; i < s.size(); i++)
{
out << s[i];
}
return out;
}
istream& operator>>(istream& in, string& s)
{
char ch;
ch = in.get();
while (ch != ' ' && ch != '\n')
{
s += ch;
ch = in.get();
}
return in;
}
void test_string()
{
string s1;
string s2;
cin >> s1 >> s2;
cout << s1 << s2 << endl;
}
}
c++之string类的模拟实现
最新推荐文章于 2024-10-08 12:54:42 发布
该文章展示了一个自定义的C++string类实现,包括构造函数、析构函数、成员方法如大小、容量查询,字符插入、删除、追加等操作,以及内存管理和拷贝控制。此外,还包含了与iostream的集成以便于输入输出。
摘要由CSDN通过智能技术生成