Implement the string class and various interfaces
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
using namespace std;
class String
{
public:
String()
:_str(new char[16])
, _size(0)
, _capacity(0)
{
_str[_size] = '\0';
_capacity = 15;
}
String(const char* str)
{
_size = strlen(str);
_str = new char[_size + 1];
strcpy(_str, str);
_capacity = _size;
}
const char* c_str() const
{
return _str;
}
String(const String& str)
:_str(new char[str._capacity + 1])
, _size(str._size)
, _capacity(str._capacity)
{
strcpy(_str, str._str);
}
String& operator=(const String& str)
{
if (this != &str)
{
char* tmp = new char[str._capacity + 1];
strcpy(tmp, str._str);
delete[] _str;
_str = tmp;
_size = str._size;
_capacity = str._capacity;
}
return *this;
}
void pushBack(const char& ch)
{
if (_size == _capacity)
{
size_t newcapacity = _capacity == 0 ? 15 : 2 * _capacity;
reserve(newcapacity);
}
_str[_size] = ch;
_size++;
_str[_size] = '\0';
}
void reserve(size_t newcapacity)
{
if (newcapacity > _capacity)
{
char* tmp = new char[newcapacity + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = newcapacity;
}
}
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;
}
char& operator[](size_t pos)
{
if (pos < _size)
return _str[pos];
}
const char& operator[](size_t pos) const
{
if (pos < _size)
return _str[pos];
}
size_t size() const
{
return _size;
}
void Append(const char* str)
{
int len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
}
strcpy(_str + _size, str);
_size += len;
}
String& operator+=(const char& ch)
{
pushBack(ch);
return *this;
}
String& operator+=(const char* str)
{
Append(str);
return *this;
}
void insert(size_t pos, const char& ch)
{
if (pos > _size)
return;
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)
{
if (pos > _size)
return;
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;
}
for (int i = 0; i < len; i++)
{
_str[i + pos] = str[i];
}
_size += len;
}
void resize(size_t n, const char& ch = '\0')
{
if (n > _capacity)
reserve(n);
if (n > _size)
memset(_str + _size, ch, n - _size);
_size = n;
_str[_size] = '\0';
}
void popBack()
{
erase(_size - 1, 1);
}
void erase(size_t pos, size_t len)
{
if (pos < _size)
{
if (pos + len >= _size)
{
_size = pos;
_str[_size] = '\0';
}
else
{
for (int i = pos + len; i <= _size; i++)
{
_str[pos++] = _str[i];
}
_size -= len;
}
}
}
size_t find(const char* str)
{
char* ptr = strstr(_str, str);
if (ptr)
return ptr - _str;
else
return npos;
}
~String()
{
if (_str)
{
delete[] _str;
_size = _capacity = 0;
_str = nullptr;
}
}
private:
char* _str;
size_t _size;
size_t _capacity;
public:
static const size_t npos;
};
const size_t String::npos = -1;
String operator+(const String& s, const char& ch)
{
String tmp(s);
tmp += ch;
return tmp;
}
String operator+(const String& s, const char* str)
{
String tmp(s);
tmp += str;
return tmp;
}
String operator+(const String& s, const String& str)
{
String tmp(s);
tmp += str.c_str();
return tmp;
}
ostream& operator<<(ostream& cout, const String& str)
{
for (const auto& ch : str)
{
cout << ch;
}
for (int i = 0; i < str.size(); i++)
{
cout << str[i];
}
String::const_iterator it = str.begin();
while (it != str.end())
{
cout << *it;
it++;
}
return cout;
}
void printString(const String& str)
{
String::const_iterator it = str.begin();
while (it != str.end())
{
cout << *it << " ";
it++;
}
cout << endl;
}
void test1()
{
String s = "123";
String::iterator it = s.begin();
while (it != s.end())
{
cout << *it << " ";
*it = 'a';
it++;
}
cout << endl;
it = s.begin();
while (it != s.end())
{
cout << *it << " ";
it++;
}
cout << endl;
}
void test2()
{
String s = "123";
for (const auto& ch : s)
{
cout << ch << " ";
}
cout << endl;
for (auto& ch : s)
{
cout << ch << " ";
ch = 'a';
}
cout << endl;
for (auto ch : s)
{
cout << ch << " ";
}
cout << endl;
}
void test3()
{
String s("01234567");
size_t pos = s.find("02");
if (pos == String::npos)
{
cout << "not find" << endl;
}
}
int main()
{
String s("01234");
return 0;
}