作业
#include <iostream>
#include <cstring>
using namespace std;
//在之前做的mystring类的基础上,将能够重载的运算符全部进行重载
class mystring
{
private:
char *str;
int size;
public:
//无参构造
mystring():size(10)
{
str = new char[size];
str[0] = '\0'; //初始化为空字符串
}
//有参构造
mystring(const char *s)
{
size = strlen(s)+1; //计算字符出大小,计入‘\0’
str = new char[size];
strcpy(str,s);
}
//有参构造 (前者为字符串长度,后者为字符内容(根据前者重复))
mystring(int n,char ch)
{
size = n+1; //字符串长度计入,字符没有‘\0’,转化成字符串后需要算上加入的‘\0’
str = new char[size];
for(int i=0;i<n;i++)
{
str[i] = ch;
}
str[n] = '\0';
}
//析构函数
~mystring()
{
delete []str; //释放str所指向的堆区空间
}
//拷贝构造函数
mystring(const mystring &other)
{
size = other.size;
str = new char[size];
strcpy(str,other.str);
}
//赋值构造函数
mystring& operator=(const mystring &other)
{
if(this!=&other) //避免自我赋值
{
delete[] str; //防止内存泄露
size = other.size;
str = new char[size];
strcpy(str,other.str);
}
return *this;
}
//判空函数
bool empty()const //函数常量化,防止修改成员
{
return str[0] = '\0';
}
//size函数
int getsize() const
{
return size-1; //不包括终止字符‘\0’
}
//c_str函数
const char * c_str() const
{
return str; //返回字符串
}
//at函数
char& at(int index)
{
if(index <0||index>=size-1)
{
cout<<"输入有误"<<endl;
}
return str[index]; //返回要查看的位置上的字符(内容)
}
//二倍扩容
void resize(int size1 = 2) //给定扩容大小,默认参数为二,使用函数时可自定义
{
int new_size = size1*size; //计算扩容后的大小
if(new_size>size)
{
char *new_str = new char [new_size]; //重新申请空间,用于扩大
strcpy(new_str,str); //内容拷贝
delete []str; //释放之前的空间
str = new_str; //指针重新指向
size = new_size; //大小重新赋值
}
}
//实现+=运算符重载
mystring& operator+=(const mystring &other) //两个字符串拼接
{
int new_size = size + other.size -1; //拼接后的大小
char *new_str = new char[new_size]; //拼接后信息所在的空间,防止大小不够,造成数据泄露
strcpy(new_str,str); //前一段信息拷贝
strcat(new_str,other.str); //后一段信息拼接
delete []str; //释放头之前所在的空间
str = new_str; //指针重命名
size = new_size;
return *this;
}
//取地址运算符重载
mystring * operator&()
{
return this;
}
//字符串赋值符重载 (就是上面的拷贝赋值函数)
// mystring& operator=(const mystring &other)
// {
// if(this!=&other) //避免自我赋值
// {
// delete[] str; //防止内存泄露
// size = other.size;
// str = new char[size];
// strcpy(str,other.str);
//}
// return *this;
//}
//访问指定运算符重载
char &operator[](int index)
{
if(index>= size||index<=0)
{
cout<<"输入错误"<<endl;
}
return str[index];
}
//字典序比较运算符重载
bool operator==(const mystring& other)const
{
return strcmp(str,other.str)==0;
}
bool operator!=(const mystring& other)const
{
return strcmp(str,other.str)!=0;
}
bool operator<(const mystring& other)const
{
return strcmp(str,other.str)<0;
}
bool operator>(const mystring& other)const
{
return strcmp(str,other.str)>0;
}
bool operator<=(const mystring& other)const
{
return strcmp(str,other.str)<=0;
}
bool operator>=(const mystring& other)const
{
return strcmp(str,other.str)>=0;
}
//输入运算符重载
friend istream& operator>>(istream& in,mystring& s)
{
char buff[1024];
in.getline(buff,1024);
delete[]s.str;
s.size = strlen(buff);
s.str = new char[s.size+1];
strcpy(s.str,buff);
return in;
}
//输出运算符重载
friend ostream& operator <<(ostream& out,const mystring &s)
{
char buff[1024];
out<<s.str;
return out;
}
};
int main()
{
cout << "Hello World!" << endl;
return 0;
}
笔记
思维导图