自用,String类的各种实现,析构、构造、赋值。。。
#include<bits/stdtr1c++.h>
using namespace std;
class String
{
public:
String(); //默认构造函数
String(int n, char c); //普通构造函数
String(const char *source); //普通构造函数
String(const String &s); //复制构造函数
String & operator = (char *s); //重载=,实现字符串赋值
String & operator = (const String &s); //重载=,实现对象赋值
char & operator [] (int i); //重载[]实现数组运算
const char & operator [] (int i) const; //重载[],实现数组运算(对象为常量)
String & operator += (const String &s); //重载+=,实现对象相加
String & operator += (const char *s); //重载+=,实现字符串相加
String & operator +(String &str);
friend ostream & operator << (ostream &out ,String &s); //重载<< 实现输出流
friend istream & operator >> (istream &in, String &s); //重载>> 实现输入流
friend bool operator < (const String &left,const String &right); //重载<
friend bool operator > (const String &left,const String &right); //重载>
friend bool operator == (const String &left,const String &right); //重载==
friend bool operator != (const String &left,const String &right); //重载!=
virtual void test(){} //这个只是用来测试带有虚函数表的时候 整个类的大小
~String(); //析构函数
int length();
private:
int size; //data表示的字符串长度
char *data; //指向字符串数据
char *getData; //获取data指针
/*经过测试。String 类的大小位32 int为4 char* 为8 自动补齐 在加上有虚函数表 sizeof(String) =32 */
};
String::String()
{
data =new char[1]; //空字符串只含有一个'\0'
*data = '\0';
size = 0;
}
String::String(int n, char c)
{
//创建一个含有n个相同字符的string
data = new char[n+1];
size = n;
char *temp = data; //利用临时的变量来操作data
while(n--)
{
*temp = c;
temp++;
}
*temp = '\0';
}
String::String(const char *source)
{
//创建一个指定的字符串
if(source == nullptr)
{
data = new char[1];
size = 0;
*data = '\0';
}
else
{
size = strlen(source);
data = new char[size + 1];
strcpy(data, source); //将指定的字符串复制到data中
}
}
String::String(const String &s)
{
//复制构造函数
data = new char [s.size +1]; //先分配空间
strcpy(this->data, s.data); //再将s的data复制给this.data
this->size = s.size;
}
String &String::operator=(char *s)
{
if(s != nullptr)
{
delete []data;
}
size = strlen(s);
data = new char[size + 1];
strcpy(data,s);
return *this;
}
String &String::operator=(const String &s)
{
if(this == &s) //该对象就是自己
{
return *this;
}
if(data !=nullptr)
{
delete []data;
}
size = strlen(data);
data = new char [size + 1];
strcpy(this->data, s.data);
return *this;
}
char& String::operator[](int i)
{
return data[i];
}
const char& String::operator[](int i) const
{
return data[i];
}
String& String::operator+=(const String &s)
{
//将连个对象拼接到一起
int len = size + s.size + 1;
char *temp = data;
size = len - 1;
data = new char[len];
strcpy(data,temp);
strcat(data,s.data);
delete [] temp;
return *this;
}
String & String::operator+=(const char *s)
{
if(s == nullptr)
{
return *this;
}
int len = size + strlen(s) + 1;
char *temp = data;
data = new char[len];
size = len - 1;
strcpy(data,temp);
strcat(data,s);
delete []temp;
return *this;
}
String &String::operator +(String &str)
{
//我需要重载+ 将传递进来的str接在后面
// //改变被加的对象
// char *temp = data;
// data = new char[strlen(temp) + strlen(str.data)+1];
// strcpy(data,temp);
// delete []temp; //将上面的temp释放掉
// strcat(data,str.data);
// return *this;
//不改变被加的对象
String *pstr = new String("");
pstr->data = new char[strlen(data) + strlen(str.data)+1];
strcpy(pstr->data,data);
strcat(pstr->data,str.data);
return *pstr;
}
int String::length()
{
return size;
}
ostream & operator << (ostream &out ,String &s)
{
for (int i =0; i < s.length(); i++)
{
out <<s[i] <<" "; //输出字符串中的每个字符
}
return out;
}
istream & operator >> (istream &in ,String &s)
{
char p[50];
in.getline(p,50); //从输入流中接受最多50个字符
s = p;
return in;
}
bool operator < (const String &left,const String &right)
{
int i = 0;
while (left[i] == right[i] && left[i] != 0 && right[i] != 0)
{
i++;
}
return left[i] - right[i] < 0 ? true : false;
}
String::~String()
{
if(data != nullptr)
{
delete []data;
data = nullptr;
size = 0;
}
}
int main()
{
String str;
cout<< sizeof (str)<<endl;
return 0;
}