String.h
#pragma once
#include<iostream>
using namespace std;
#include<assert.h>
class String
{
public:
String(char *str = "")//构造函数
{
_size = strlen(str);
_capa = _size;
_str = new char[_capa + 1];
strcpy(_str, str);
}
void Swap(String& s)
{
swap(_str, s._str);
swap(_size, s._size);
swap(_capa, s._capa);
}
String(const String& s)//拷贝构造
:_str(NULL)
, _size(0)
, _capa(0)
{
String tmp(s._str);
Swap(tmp);
}
~String()//析构
{
if (_str)
{
delete[] _str;
_str = NULL;
_size = _capa = 0;
}
}
String& operator=(String s)//赋值运算符=重载
{
Swap(s);
return *this;
}
char *GetStr()//获取字符串
{
return _str;
}
size_t size()//获取大小
{
return _size;
}
char& operator[](size_t pos)//[]重载
//1.出了作用域,_str还在,所以用&
//2.可读可写
{
assert(pos<_size);
return _str[pos];
}
void Expand(size_t n)//扩容
{
if (n > _capa)
{
_str = (char*)realloc(_str, n + 1);
assert(_str);
_capa = n;
}
}
void PushBack(char ch)//尾插一个字符
{
if (_size == _capa)
{
Expand(_capa*2);
}
_str[_size] = ch;
++_size;
_str[_size] = '\0';
}
void PushBack(const char* str)//尾插一个字符串
{
size_t len = strlen(str);
if (len + _size > _capa)
{
Expand(len + _size);
}
strcpy(_str+_size, str);
}
void PopBack()//尾删一个字符
{
assert(_size);
_str[--_size] = '\0';
}
void Insert(size_t pos, char ch)//指定位置插入一个字符
{
if (_size == _capa)
{
Expand(_capa * 2);
}
int end = _size;
while (end >= (int)pos)
{
_str[end + 1] = _str[end];
--end;
}
_str[pos] = ch;
++_size;
}
void Insert(size_t pos, const char *str)//指定位置插入一个字符串
{
size_t len = strlen(str);
if (len + _size > _capa)
{
Expand(len + _size);
}
int end = _size;
while (end >= (int)pos)
{
_str[end + len] = _str[end];
--end;
}
while (*str)
{
_str[pos++] = *str++;
}
_size += len;
}
void Erase(size_t pos, size_t count)//删除指定位置的count个字符
{
if (pos + count >= _size - 1)
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + count);
_size -= count;
}
}
int Find(char ch) const//查找一个字符
{
for (size_t i = 0; i < _size; i++)
{
if (_str[i] == ch)
{
return i;
}
}
return -1;
}
int Find(const char*str) const//查找n个字符
{
assert(str);
const char* srcstr = _str;//原字符串
const char* substr = str;//要寻找的子串
size_t srcindex = 0;
size_t subindex = 0;
size_t sublen = strlen(substr);
while (srcindex < _size)
{
size_t matchindex = srcindex;
while (srcstr[matchindex] == substr[subindex])
{
++matchindex;
++subindex;
if (subindex == sublen)
{
return srcindex;
}
}
subindex = 0;
srcindex++;
}
return -1;
}
//赋值运算符重载
inline bool operator<(const String& s) const
{
size_t i = 0;
for (; i < _size && i < s._size; i++)
{
if (_str[i] > s._str[i])
{
return false;
}
}
if (i == _size&&i < s._size)
{
return true;
}
else
{
return false;
}
}
inline bool operator==(const String& s) const
{
size_t i = 0;
for (; i < _size && i < s._size; i++)
{
if (_str[i] != s._str[i])
{
return false;
}
}
if (i == _size&&i == s._size)
{
return true;
}
else
{
return false;
}
}
inline bool operator<=(const String& s) const
{
return *this < s || *this == s;
}
inline bool operator>(const String& s) const
{
return !(*this <= s);
}
inline bool operator>=(const String& s) const
{
return *this > s || *this == s;
}
inline bool operator!=(const String& s) const
{
return !(*this == s);
}
private:
char *_str;
size_t _size;//大小
size_t _capa;//容量
};
void TestString()
{
String s1("hello");
String s2(s1);
s1.PushBack(' ');
s1.PushBack('w');
s1.PushBack('o');
s1.PushBack('r');
s1.PushBack('l');
s1.PushBack('d');
cout << s1.GetStr() << endl;
s2.PushBack(" world!");
cout << s2.GetStr() << endl;
String s3("hellworld?");
String s4(s3);
s3.Insert(4, ' ');
s3.Insert(4, 'o');
cout << s3.GetStr() << endl;
s4.Insert(4, ' ');
s4.Insert(4, 'o');
cout << s4.GetStr() << endl;
String s5("world");
s5.Insert(0, "hello ");
cout << s5.GetStr() << endl;
String s6("hello world?");
String s7(s6);
s6.Erase(5, 6);
cout << s6.GetStr() << endl;
s7.Erase(5, 20);
cout << s7.GetStr() << endl;
String s8("aabcdaabbbaa");
cout << s8.Find("aabb") << endl;
cout << s8.Find("aabx") << endl;
String s9("hello world!");
s9.PopBack();
cout << s9.GetStr() << endl;
}
测试部分
#include"String.h"
int main()
{
TestString();
system("pause");
}
程序测试结果: