#pragma once
#include<assert.h>
#include <string>
#include<iostream>
using namespace std;
namespace MyString {
class String {
public:
typedef char* Iterator; //迭代器
Iterator begin() { //begin()
return _str;
}
Iterator end() { //end()
return _str + _size;
}
//构造函数
String(const char* str = "") {
//构造string类对象时,如果传递nullptr指针,
//认为程序非法,此处断言下
if (str == nullptr) {
assert(false);
return;
}
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, str);
}
//析构函数
~String() {
if(_str) {
delete[] _str;
_str = nullptr;
_size = 0;
_capacity = 0;
}
}
//拷贝构造
String(const String& s)
: _str(new char[s._capacity + 1]) , _size(0)
, _capacity(0)
{
strcpy(_str, s._str);
}
String& operator=(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
}
bool operator<(const String& s) { //<重载
return strcmp(_str, s._str) < 0;
}
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 strcmp(_str, s._str) == 0;
}
bool operator!=(const String& s) { //!=重载
return !(*this == s);
}
void Reserve(size_t n) //判断容量,并实现扩容
{
if (_capacity < n)
{
cout << "扩容" << endl;
char* str = new char[n+1];
strcpy(str, _str);
//释放原来的空间
delete[] _str;
_str = str;
_capacity = n;
}
}
void PushBack(char ch) //追加一个字符
{
if (_size == _capacity) {
Reserve(_capacity*2);
}
_str[_size++] = ch;
_str[_size] = '\0';
}
void Append(const char* str)
//追加一个字符串
{
for (size_t i = 0; i < strlen(str); i++) {
PushBack(str[i]);
}
}
String& operator+=(char ch) {
PushBack(ch);
return *this;
}
String& operator+=(const char* str) {
Append(str);
return *this;
}
void Clear() {
_size = 0;
_capacity = 0;
_str[_size] = '\0';
}
void Swap(String& s) {
swap(_str, s._str);
swap(_size, s._size);
swap(_capacity, s._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 0 == _size;
}
void Resize(size_t newSize, char c = char()){
if (newSize > _size) {
// 如果newSize大于底层空间大小,
//则需要重新开辟空间
if (newSize > _capacity) {
Reserve(newSize);
}
memset(_str + _size, c,
newSize - _size);
}
_size = newSize;
_str[newSize] = '\0';
}
void Insert(size_t pos, char ch) {
//插入一个字符
assert(pos <= _size);
Reserve(_size + 1);
size_t end = _size ;
while (end >= pos) {
_str[end + 1] = _str[end];
end--;
}
_str[pos] = ch;
_size++;
}
void Insert(size_t pos, const char* str) {
//插入一个字符串
assert(pos <= _size);
Reserve(_size + strlen(str) + 1);
for (size_t i = 0; i < strlen(str) - 1; i++)
{
Insert(pos, str[i]);
}
}
void Erase(size_t pos, size_t len)
//删除len长度的字符串
{
assert(pos <= _size);
size_t start = pos + len;
size_t end = _size;
while (start<=end)
{
_str[pos++] = _str[start++];
}
_size -= len;
}
char& operator[](size_t index)
{
assert(index < _size);
return _str[index];
}
size_t Find(char ch, size_t pos = 0)
//从pos开始,找一个字符
{
size_t i = 0;
while (_str[i])
{
if (_str[i] == ch)
return i;
i++;
}
return npos;
}
size_t Find(const char* str, size_t pos = 0) //从pos开始,找一个字符串
{
size_t begin = 0;
size_t len = strlen(str);
size_t end = _size - 1 - len;
size_t flg = 0;
if (_size < len)
return npos;
while (begin <= end)
{
size_t i = 0;
size_t f = Find(str[0], begin);
pos = f;
while (i < len)
{
if (_str[pos] == str[i])
{
pos++;
i++;
}
else
{
flg++;
break;
}
}
if (flg==0) return f;
begin+=f;
}
return npos;
}
private:
friend ostream& operator<<(ostream& _cout, const MyString::String& s);
private:
char* _str;
size_t _size;
size_t _capacity;
};
}
ostream& bit::operator<<(ostream& _cout, const bit::String& s){
cout << s._str;
return _cout;
}
///对自定义的string类进行测试
void TestMyStringString1() {
MyString::String s1;
MyString::String s2("hello MyString");
MyString::String s3(s2);
s1 = s3;
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl; }
void TestMyStringString2() {
MyString::String s1("hello");
s1.PushBack(' ');
s1.PushBack('b');
s1.Append(1, 'i');
s1 += 't';
cout << s1 << endl;
cout << s1.Size() << endl;
cout << s1.Capacity() << endl;
// 利用迭代器打印string中的元素
auto it = s1.Begin();
while (it != s1.End()) {
cout << *it++;
}
cout << endl;
MyString::String s2("hello world!!!");
s1.Swap(s2);
cout << s1 << endl;
cout << s2 << endl;
}
void TestMyStringString3() {
MyString::String s("hello");
cout << s << endl;
cout << s.Size() << endl;
cout << s.Capacity() << endl;
s.ReSize(10, 'a');
cout << s << endl;
cout << s.Size() << endl;
cout << s.Capacity() << endl;
s.ReSize(20);
cout << s << endl;
cout << s.Size() << endl;
cout << s.Capacity() << endl;
s.ReSize(5);
cout << s << endl;
cout << s.Size() << endl;
cout << s.Capacity() << endl;
s.Reserve(50);
cout << s << endl;
cout << s.Size() << endl;
cout << s.Capacity() << endl;
}