代码如下:
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <assert.h>
#include <cstring>
using namespace std;
class String
{
public:
String(const char *str = "")
{
assert(str != nullptr);
_size = strlen(str);
_str = new char[_size + 1];
_capacity = _size;
strcpy(_str, str);
}
String(const String &s) :_str(nullptr), _size(0), _capacity(0)
{
String tmp(s._str);
Swap(tmp);
}
void Swap(String &s)
{
swap(_str, s._str);
swap(_size, s._size);
swap(_capacity, s._capacity);
}
String &operator=(String s)
{
if (this != &s)
{
Swap(s);
}
return *this;
}
~String()
{
if (_str != nullptr)
{
delete[]_str;
_str = nullptr;
}
}
size_t size() const
{
return _size;
}
size_t capacity()const
{
return _capacity;
}
char &operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
const char &operator[](size_t pos) const
{
assert(pos < _size);
return _str[pos];
}
typedef char *iterator;
typedef const char *const_iterator;
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
const_iterator begin()const
{
return _str;
}
const_iterator end()const
{
return _str + _size;
}
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(const char &ch)
{
if (_size == _capacity)
{
size_t newCapcity = _capacity == 0 ? 15 : 2 * _capacity;
reserve(newCapcity);
}
_str[_size++] = ch;
_str[_size] = '\0';
}
void resize(size_t n, const char &ch = '\0')
{
if (n > _capacity)
{
reserve(n);
}
if (n > _size)
memset(_str + _size, ch, (n - _size) * sizeof(char));
_size = n;
_str[_size] = '\0';
}
void append(const char *str)
{
int len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
memcpy(_str + _size, str, sizeof(char)*len);
_size += len;
_str[_size] = '\0';
}
String &operator+=(const String &str)
{
append(str._str);
return *this;
}
String &operator+=(const char *str)
{
append(str);
return *this;
}
String &operator+=(const char ch)
{
push_back(ch);
return *this;
}
void insert(size_t pos, const char &ch)
{
assert(pos <= _size);
if (_size == _capacity)
{
size_t newCapacity = _capacity == 0 ? 15 : 2 * _capacity;
reserve(newCapacity);
}
size_t end = _size + 1;
while (end > pos)
{
_str[end] = _str[end - 1];
--end;
}
_str[pos] = ch;
++_size;
}
void insert(size_t pos, const char *str)
{
assert(pos <= _size);
int len = strlen(str);
if (_size + len >= _capacity)
{
reserve(_size + len);
}
size_t end = _size + len;
while (end > pos + len - 1)
{
_str[end] = _str[end - len];
--end;
}
memcpy(_str + pos, str, sizeof(char)*len);
_size += len;
}
void erase(size_t pos, size_t len)
{
assert(pos < _size);
if (pos + len >= _size)
{
_size = pos;
_str[_size] = '\0';
}
else
{
size_t start = pos + len;
while (start <= _size)
{
_str[pos++] = _str[start];
++start;
}
_size -= len;
}
}
size_t find(const char &ch, size_t pos = 0)
{
assert(pos < _size);
for (size_t i = pos; i < _size; i++)
{
if (_str[i] == ch)
{
return i;
}
}
return npos;
}
size_t find(const char *str, size_t pos = 0)
{
char *ptr = strstr(_str, str);
if (ptr != nullptr)
{
return ptr - _str;
}
return npos;
}
private:
char *_str;
size_t _size;
size_t _capacity;
static const size_t npos;
};
const size_t String::npos = -1;
String operator+(const String &str1, const String &str2)
{
String tmp = str1;
tmp += str2;
return tmp;
}
String operator+(const String &str1, const char *str2)
{
String tmp = str1;
tmp += str2;
return tmp;
}
String operator+(const char &ch, const String &str1)
{
String tmp = str1;
tmp += ch;
return tmp;
}
ostream &operator<<(ostream &out, const String &str)
{
for (const auto &ch : str)
{
cout << ch;
}
return out;
}
istream &operator>>(istream &in, String &str)
{
char ch;
while (ch = getchar())
{
if (ch == ' ' || ch == '\n' || ch == '\t')
{
break;
}
str += ch;
}
return in;
}
测试代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "String.h"
using namespace std;
int main()
{
String str = ("Hello Wecccccccc");
cout << str << endl;
String str1 = str;
cout << str1 << endl;
for (int i = 0; i < str1.size(); i++)
{
cout << str[i] << " ";
}
cout << endl;
for (const auto&ch : str)
{
cout << ch << " ";
}
cout << endl;
String::iterator it = str.begin();
while (it != str.end())
{
cout << *it << " ";
it++;
}
cout << endl;
cout << "str size = " << str.size() << " " << "str capacity = " << str.capacity() << endl;
str.reserve(20);
cout << "str size = " << str.size() << " " << "str capacity = " << str.capacity() << endl;
str.push_back('N');
str.push_back('N');
str.push_back('N');
str.push_back('N');
str.push_back('N');
cout << str << endl;
String str3 = "Weccccc";
str3.resize(3);
cout << str3 << endl;
str3.resize(5, 'a');
cout << str3 << endl;
str3.resize(10, 'b');
cout << str3 << endl;
str3.append(" hello");
cout << str3 << endl;
String str4 = "We";
String str5 = "cccccccc";
str4 += str5;
cout << str4 << endl;
cout << str4 + str5 << endl;
String str6;
str6 = 'W' + str4+"123";
cout << str6 << endl;
String str7 = "abc";
str7.insert(0, '1');
cout << str7 << endl;
str7.insert(1, '2');
cout << str7 << endl;
str7.insert(5, "35");
cout << str7 << endl;
str7.erase(0, 2);
cout << str7 << endl;
str7.erase(4, 1);
cout << str7 << endl;
str7.erase(1, 2);
cout << str7 << endl;
int pos = str7.find('3');
cout << pos << endl;
pos = str7.find('b');
cout << pos << endl;
return 0;
}
测试结果: