//头文件
#ifndef MYSTRING_H
#define MYSTRING_H
#include
#include
using namespace std;
const int stringLnegth = 100;
class StringIndexOutOfBounds{};
class String{
private:
char* buffer;//存储字符
int bufferLength;//字符数组的最大长度,即缓冲容量
int size;//记录字符串的长度
public:
String(){
this->buffer = NULL;
}
String(const char* cstring);
String(const String& str);
~String();
//在定义复制函数是要考虑自检测,便面相同地址在进行一次复制
const String& operator=(const String &str);
const String& operator+=(const String &str);
String Substr(int index, int count);
const char *c_str()const;//返回首字母
int Length()const;
char operator[](int pos)const;//返回pos处的值
char& operator[](int pos);
friend ostream& operator<<(ostream& out, const String& str);
friend istream& operator>>(istream& in, String& str);
friend bool operator==(const String& str, const String& ptr);
friend bool operator!=(const String& str, const String& ptr);
friend bool operator<(const String& str, const String& ptr);
friend bool operator<=(const String& str, const String& ptr);
friend bool operator>(const String& str, const String& ptr);
friend bool operator>=(const String& str, const String& ptr);
};
String::String(const char* cstring)
{
if (cstring == NULL)
cstring = "";
this->bufferLength = strlen(cstring)+1;
try{
this->buffer = new char[this->bufferLength];
}
catch (exception e){
cout << "空间未开辟成功!" << endl;
}
strcpy_s(this->buffer,this->bufferLength,cstring);
size = strlen(this->buffer);
}
String::String(const String& str)
{
this->bufferLength = str.Length() + 1;
try{
this->buffer = new char[this->bufferLength];
}
catch (exception e){
cout << "空间为开辟成功!" << endl;
}
strcpy_s(this->buffer,this->bufferLength,str.buffer);
size = strlen(this->buffer);
}
String::~String()
{
delete[]this->buffer;
}
const String& String::operator=(const String& str)
{
//进行自检测,如果两地址不相同,则可以进行以下过程,否则返回*this
if (this != &str)
{
if (this->bufferLength < str.bufferLength)
{
delete[]this->buffer;
this->bufferLength = str.bufferLength + 1;
this->buffer = new char[this->bufferLength];
}
strcpy_s(this->buffer,this->bufferLength,str.buffer);
}
size = strlen(this->buffer);
return *this;
}
const String& String::operator+=(const String& str)
{
//自检测,是否地址相同
if (this == &str)
{
String copy(str);
//调用+=运算符,实现字符串的链接
return *this += copy;
}
//因为要合并连个字符串,所以先得到两个字符串的长度和
int newLength = this->Length() + str.Length();
if (newLength > this->bufferLength)
{
//如果空间现有空间要小于两个字符串合并后的空间,则重新开辟以是newLength+1长度2倍的空间,来存放两个字符串
this->bufferLength = (newLength + 1) * 2;
char *oldBuffer = this->buffer;
this->buffer = new char[this->bufferLength];
strcpy_s(this->buffer,this->bufferLength, oldBuffer);
delete[]oldBuffer;
}
strcpy_s(this->buffer + Length(),str.bufferLength, str.buffer);
size = strlen(this->buffer);
return *this;
}
String String::Substr(int index, int count)
{
//从下标为index处开始,抽取count个字符
int left = size - index;//从index开始到字符串尾部共有left个字符
String temp;
char *p, *q;
if (index >= size)
{
//如果抽取字符串的位置超过的字符的长度,则返回空字符串
return temp;
}
if (count > left)
{
//如果抽取的字符串长度大于从index开始到结束字符之间的长度,则count变小
count = left;
}
delete[]temp.buffer;
temp.buffer = new char[count + 1];
p = temp.buffer;
q = &this->buffer[index];
for (int i = 0; i < count; i++)
{
*p++ = *q++;
}
*p = '\0';
temp.size = count;
return temp;
}
const char* String::c_str()const
{
//返回首字母的地址
return this->buffer;
}
int String::Length()const
{
return this->size;
}
char String::operator[](int pos)const
{
if (pos<0 && pos>size)
throw StringIndexOutOfBounds();
return this->buffer[pos];
}
char & String::operator[](int pos)
{
if (pos<0 && pos>size)
throw StringIndexOutOfBounds();
return this->buffer[pos];
}
ostream& operator<<(ostream& out, const String& str)
{
out << str.buffer;
return out;
}
istream& operator>>(istream& in, String& str)
{
char buf[stringLnegth + 1];
in.getline(buf,stringLnegth);
if (!in.fail())
{
str.buffer = buf;
}
return in;
}
bool operator==(const String& str, const String& ptr)
{
if (strcmp(str.buffer, ptr.buffer) == 0)
return true;
return false;
}
bool operator!=(const String& str, const String& ptr)
{
if (strcmp(str.buffer, ptr.buffer) == 0)
return false;
return true;
}
bool operator<(const String& str, const String& ptr)
{
if (strcmp(str.buffer, ptr.buffer) < 0)
{
return true;
}
return false;
}
bool operator<=(const String& str, const String& ptr)
{
if (strcmp(str.buffer, ptr.buffer) <= 0)
{
return true;
}
return false;
}
bool operator>(const String& str, const String& ptr)
{
if (strcmp(str.buffer, ptr.buffer) > 0)
{
return true;
}
return false;
}
bool operator>=(const String& str, const String& ptr)
{
if (strcmp(str.buffer, ptr.buffer) >= 0)
{
return true;
}
return false;
}
#endif
//主函数
#include"MyStringh.h"
int main(int argc, char argv[])
{
String temp = "uhfvorgueiwhbgrfiuhdafiu";
String str = "fuihuriehvhdfuih";
cout << temp << endl;
cout << str << endl;
if (str < temp)
{
cout << "str
<< endl;
}
else
cout << "str>temp" << endl;
for (int i = 0; i < temp.Length(); i++)
{
cout << temp[i] << " ";
}
cout << endl;
return 0;
}
字符串的操作
最新推荐文章于 2023-09-09 20:20:14 发布