使用动态数组法,不会限制输入时的大小
可以在此基础上添加其他功能,或者简化此版本
#include<iostream>
#define DefalutStringSize 100
#define Increasement 50
class myString
{
public:
myString(); //默认构造函数
myString(const char* src); //通过字符串构造
myString(const myString& src); //通过myString构造
~myString() { delete[]myStr; }; //析构函数
//运算符重载
bool operator==(const char* src);
bool operator==(const myString& src);
bool operator!=(const char* src);
bool operator!=(const myString& src);
myString& operator=(const char* src);
myString& operator=(const myString& src);
friend std::ostream& operator<<(std::ostream& os, const myString& src);
friend std::istream& operator>>(std::istream& is, myString& src);
private:
char* myStr; //数组指针
int curSize; //当前大小
int maxSize; //最大大小
void overflowProcess(); //大小超上限时的操作
};
myString::myString()
{
curSize = 1;
maxSize = DefalutStringSize;
myStr = new char[maxSize];
if (myStr == NULL) {
std::cout << "内存分配出错" << std::endl;
exit(-1);
}
myStr[0] = '\0';
}
myString::myString(const char* src)
{
curSize = sizeof(src);
maxSize = DefalutStringSize;
while (1) {
if (sizeof(src) <= maxSize)
break;
else
maxSize += Increasement;
}
myStr = new char[maxSize];
if (myStr == NULL) {
std::cout << "内存分配出错" << std::endl;
exit(-1);
}
for (int i = 0; i < sizeof(src); i++)
myStr[i] = src[i];
}
myString::myString(const myString& src)
{
curSize = src.curSize;
maxSize = src.maxSize;
myStr = new char[maxSize];
if (myStr == NULL) {
std::cout << "内存分配出错" << std::endl;
exit(-1);
}
for (int i = 0; i < curSize; i++)
myStr[i] = src.myStr[i];
}
bool myString::operator==(const char* src)//和字符串判断是否相等
{
if (curSize != sizeof(src))
return false;
for (int i = 0; i < curSize; i++)
if (myStr[i] != src[i])
return false;
return true;
}
inline bool myString::operator==(const myString& src)//和myString判断是否相等
{
if (curSize != src.curSize)
return false;
for (int i = 0; i < curSize; i++)
if (myStr[i] != src.myStr[i])
return false;
return true;
}
bool myString::operator!=(const char* src)
{
if (curSize != sizeof(src))
return true;
for (int i = 0; i < curSize; i++)
if (myStr[i] != src[i])
return true;
return false;
}
inline bool myString::operator!=(const myString& src)
{
if (curSize != src.curSize)
return true;
for (int i = 0; i < curSize; i++)
if (myStr[i] != src.myStr[i])
return true;
return false;
}
myString& myString::operator=(const char* src)
{
delete[] myStr;
curSize = sizeof(src);
myStr = new char[curSize];
for (int i = 0; i < curSize; i++)
myStr[i] = src[i];
return *this;
}
inline myString& myString::operator=(const myString& src)
{
if (this == &src)
return *this;
else {
delete[]myStr;
curSize = src.curSize;
maxSize = src.maxSize;
myStr = new char[maxSize];
for (int i = 0; i < curSize; i++)
myStr[i] = src.myStr[i];
return *this;
}
}
void myString::overflowProcess()
{
maxSize += Increasement;
char* temp = new char[maxSize];
if (temp == NULL) {
std::cout << "内存分配错误" << std::endl;
exit(-1);
}
for (int i = 0; i < curSize - 1; i++)
temp[i] = myStr[i];
temp[curSize - 1] = '\0';
delete myStr;
myStr = temp;
}
std::ostream& operator<<(std::ostream& os, const myString& src)
{
os << src.myStr;
return os;
}
std::istream& operator>>(std::istream& is, myString& src)
{
char c;
c = getchar();
while (c == ' ')
c = getchar();
src.curSize = 1;
src.myStr[0] = '\0';
while (c != ' ' && c != '\n') {
if (src.curSize >= src.maxSize)
src.overflowProcess();
src.myStr[src.curSize - 1] = c;
src.curSize++;
c = getchar();
}
src.myStr[src.curSize - 1] = '\0';
return is;
}