第一次实现一个String类,没有iterator迭代器或者内存分配器等等,只是实现了简单的字符串操作并把它们写入到一个类中,重载了常见运算符提供了最简单的功能,但我认为,如果你能实现下面功能,其他功能的实现都能做到,以下代码仅供参考,可能不是最优化写法,但能跑就行,立志于写出没有BUG的代码…
#include <iostream>
using namespace std;
namespace yadong
{
class MyString
{
private:
char *_str;
size_t _size;
public:
MyString(const char* str = nullptr);
MyString(MyString&);
public:
MyString& operator+(const MyString&);
MyString& operator+(const char *);
MyString& operator=(const MyString&);
MyString& operator=(const char *);
char& operator[](size_t n);
bool operator==(const MyString&);
friend bool operator==(const char*str, const MyString&);
friend istream& operator>>(istream& in, MyString& str);
friend ostream& operator<<(ostream& ou, const MyString& str);
public:
size_t size();
const char* c_str();
public:
bool compare(MyString& str);
bool empty();
char* substr(size_t index,size_t num = 0);
void swap(MyString& str);
};
MyString::MyString(const char* str)
{
if (!str)
{
_size = 0;
_str = nullptr;
//_str = new char[1];
//_str = "\0";
}
else
{
_size = strlen(str);
_str = new char[_size + 1];
strcpy(_str, str);
}
}
MyString::MyString(MyString& str)
{
_size = str.size();
_str = new char[_size + 1];
strcpy(_str, str.c_str());
}
inline size_t MyString::size()
{
return _size;
}
inline const char* MyString::c_str()
{
return _str;
}
inline char& MyString::operator[](size_t n)
{
if (n >= _size) return _str[_size - 1];
return _str[n];
}
ostream& operator<<(ostream& ou, const MyString& str)
{
ou << str._str;
return ou;
}
istream& operator>>(istream& in,MyString& str)
{
//这句释放原先内存判断是否是必须的?
if (str._str != nullptr) delete[] str._str;
char *temp = new char[1024];
in >> temp;
str._size = strlen(temp);
str._str = new char[str._size + 1];
strcpy(str._str, temp);
delete[] temp;
temp = nullptr;
return in;
}
MyString& MyString::operator+(const MyString& str)
{
_size = _size + str._size;
char *newStr = new char[_size + 1];
strcpy(newStr, _str);
if (_str != nullptr) delete[] _str;
_str = strcat(newStr, str._str);
return *this;
}
MyString& MyString::operator+(const char * str)
{
_size = _size + strlen(str);
char *newStr = new char[_size + 1];
strcpy(newStr, _str);
if (_str != nullptr) delete[] _str;
_str = strcat(newStr, str);
return *this;
}
MyString& MyString::operator=(const MyString& str)
{
_size = str._size;
if (_str != nullptr) delete[] _str;
_str = new char[_size + 1];
strcpy(_str, str._str);
return *this;
}
MyString& MyString::operator=(const char * str)
{
_size = strlen(str);
if (_str != nullptr) delete[] _str;
_str = new char[_size] + 1;
strcpy(_str, str);
return *this;
}
inline bool MyString::compare(MyString& str)
{
if (_size > str._size) return true;
if (_size < str._size) return false;
for (size_t n = 0; n < _size; n++)
{
if (_str[n] - str._str[n] < 0) return false;
if (_str[n] - str._str[n] > 0) return true;
}
return true;
}
bool MyString::operator==(const MyString& str)
{
if (_size != str._size) return false;
for (size_t n = 0; n < _size; n++)
{
if (_str[n] != str._str[n]) return false;
}
return true;
}
bool operator==(const char*str, const MyString& myStr)
{
size_t size = strlen(str);
if (size != myStr._size) return false;
for (size_t n = 0; n < size; n++)
{
if (str[n] != myStr._str[n]) return false;
}
return true;
}
inline bool MyString::empty()
{
if (_size == 0)return true;
else return false;
}
inline void MyString::swap(MyString& str)
{
char *tempP = _str;
size_t tempS = _size;
_size = str._size;
_str = str._str;
str._str = tempP;
str._size = tempS;
}
char* MyString::substr(size_t index, size_t num)
{
char *str;
if (num == 0 && num > _size - index)
num = _size - index;
str = new char[num + 1];
char* tempP = &_str[index];
tempP[num] = '\0';
strcpy(str, tempP);
return str;
}
}