string类常用接口说明在链接中的博客中:https://blog.csdn.net/Damn_Yang/article/details/84108864
代码实现:
String.h
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<assert.h>
#include<string>
using namespace std;
namespace zdy
{
class String
{
public:
// 迭代器
typedef char* iterator;
typedef const char* const_iterator;
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
void Swap(String& s)
{
swap(_str, s._str);
swap(_size, s._size);
swap(_capacity, s._capacity);
}
//在类里面实现,默认是inline;
String(const char* str = "")
{
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, str);
}
String& operator=(const String& s)
{
if (this != &s)
{
char* pStr = new char[s._capacity + 1];
strcpy(pStr, s._str);
delete[] _str;
_str = pStr;
_size = s._size;
_capacity = s._capacity;
}
return *this;
}
~String()
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
String(const String& s)
: _str(nullptr)
, _size(0)
, _capacity(0)
{
String tmp(s._str);
Swap(tmp);
}
String &operator+=(String& s)
{
Swap(s);
return *this;
}
char &operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
const char& operator[](size_t pos) const
{
assert(pos < _size);
return _str[pos];
}
size_t Size() const
{
return _size;
}
size_t Capacity() const
{
return _capacity;
}
const char* c_str()
{
return _str;
}
//扩容
void Reserve(size_t n)
{
if (n > _capacity)
{
char* newstr = new char[n + 1];
strcpy(newstr, _str);
delete[] _str;
_str = newstr;
_capacity = n;
}
}
//改变字符串大小
void Resize(size_t n, char ch = '\0')
{
if (n < _size)
{
_str[n] = '\0';
_size = n;
}
else
{
if (n>_capacity)
{
Reserve(n);
}
size_t pos = _size;
while (pos < n)
{
_str[pos] = ch;
pos++;
}
_size = n;
_str[n] = '\0';
}
}
//尾插一个字符
void PushBack(char ch)
{
Insert(_size, ch);
}
//尾插一个字符串
void Append(const char* str)
{
Insert(_size, str);
}
String& operator+=(char ch)
{
PushBack(ch);
return *this;
}
String& operator+=(const char* str)
{
Append(str);
return *this;
}
//返回字符ch在string中第一次出现的位置
size_t Find(char ch, size_t pos)
{
int end = _size - 1;
if (pos != npos)
{
assert(pos < _size);
end = pos;
}
while (end >= 0)
{
if (_str[end] == ch)
{
return end;
}
--end;
}
return npos;
}
//返回字符串str在string中第一次出现的位置
size_t Find(const char* str, size_t pos)
{
assert(pos < _size);
char* pmatch = strstr(_str + pos, str);
if (pmatch == nullptr)
{
return npos;
}
else
{
return pmatch - _str;
}
}
//在pos位置上插入字符ch;
void Insert(size_t pos, char ch)
{
if (_size == _capacity)
{
Reserve(_capacity * 2);
}
size_t end = _size;
while (end >= pos)
{
_str[end + 1] = _str[end];
--end;
}
_str[pos] = ch;
_size++;
}
//在pos位置上插入字符串str;
void Insert(size_t pos, const char* str)
{
size_t len = strlen(str);
if (_size + len >_capacity)
{
Reserve(_size + len);
}
size_t end = _size;
while (end >= pos)
{
_str[end + len] = _str[end];
--end;
}
while (*str)
{
_str[pos++] = *str++;
}
_size += len;
}
//删除pos位置上的长度为len的元素
void Erase(size_t pos, size_t len)
{
if (len == npos || pos + len >= _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
for (size_t i = 0; i < len; ++i)
{
_str[pos] = _str[pos + len];
pos++;
}
_size -= len;
_str[_size] = '\0';
}
}
//截取从pos开始len个字符
String Substr(size_t pos, size_t len)
{
if (_size - pos < len)
{
len = _size - pos;
}
String sub;
sub.Reserve(len);
for (size_t i = pos; i < pos + len; ++i)
{
sub += _str[i];
}
return sub;
}
// 扩展学习
//operator<<()
//operator>>()
//operator> 比较大小
//operator<
private:
friend ostream& operator<<(ostream& _cout, const zdy::String& s);
private:
char* _str;
size_t _size;
size_t _capacity;
public:
static const size_t npos;
};
}
测试函数(main.cpp)
#include"String.H"
using namespace zdy;
ostream& zdy::operator<<(ostream& _cout, const zdy::String& s)
{
cout << s._str;
return _cout;
}
void Test1()
{
String s1;
String s2("hello word");
String s3(s2);
s1 = s3;
cout << s1 << endl;
cout << s2 << endl;
cout << s2 << endl;
}
void Test2()
{
String s1("hello");
s1.PushBack(' ');
s1.PushBack('w');
s1.PushBack('o');
s1 += 'r';
s1.Append("d");
cout << s1 << endl;
}
void Test3()
{
String s("1111122222333334444455555");
s.Resize(5);
for (auto e : s)
{
cout << e << " ";
}
cout << endl;
s.Resize(10);
for (auto e : s)
{
cout << e << " ";
}
cout << endl;
s.Resize(15, '6');
for (auto e : s)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
Test1();
Test2();
Test3();
return 0;
}