用Sublime写的C++自定义String类,重点在重构!
-----------------------String.h-----------------------------
#ifndef __STRING_H__
#define __STRING_H__
class MyString{
public:
MyString();
~MyString();
MyString(int len);//开辟len个长度的字符串 全部设置为0
MyString(const MyString &another);//拷贝构造函数
MyString& operator=(const MyString &another);//显示的=号构造符
MyString(const char *str);//根据str字符串初始化MyString
char& operator[](int index);//重载[]操作符
MyString & operator+(MyString &another);
bool operator ==(const MyString &another);
bool operator !=(const MyString &another);
bool operator >(const MyString &another);
bool operator >(const MyString &another);
friend ostream & operator <<(ostream &os,MyString &s);
friend istream & operator >>(istream &is,MyString &s);//warning
int getLen(){
return this->len;
}
private:
char *str;//指定在堆上开辟字符串空间首地址
int len;//当前字符串的长度
}
#endif
-----------------------String.cpp-----------------------------
MyString::MyString()
{
this->len = 0;
this->str = NULL;
}
MyString::MyString(int len)
{
if (len == 0)
{
this->len = 0;
this->str = new char[len + 1];
strcpy(this->str,"");
}
else
{
this->len = len;
this->str = new char[len + 1];
memset(this->str,0,len + 1);
}
}
//拷贝构造函数
//MyString str1 = str2; MyString str1(str2);
MyString::MyString(const MyString &another)
{
this->len = another.len;
this->str = new char[another.len + 1]
strcpy(this->str,another.str);
}
//MyString str1 = "1234455";
//MyString str1("1213214");
MyString::MyString(const char *str)
{
if(str == NULL){
this->len = 0;
this->str = new char[len + 1];
strcpy(this->str,"");
}
else{
this->len = strlen(str);
this->str = new char[len + 1];//已经在堆上开辟了内存
/*for (int i = 0; i < this->len; ++i)
{
this->str[i] = str[i];
}*/
strcpy(this->str,str);//将str的内容拷贝到this->str堆空间中
}
}
MyString& MyString::operator=(const MyString &another)
{
this->len = 0;
if (this->str != NULL)
{
delete[] this->str;
this->str = NULL;
}
this->len = another.len;
this->str = (char*)malloc(this->len + 1);
strcpy(this->str,another.str);
return *this;
}
char& MyString::operator[](int index)
{
return this->str[index];
}
bool MyString::operator ==(const MyString &another)
{
if (this->len != another.len)return false;
if (strcmpy(this->str,another.str) != 0/*=0为真*/)return false;
return true;
}
bool MyString::operator !=(const MyString &another)
{
if (*this == another)return false;
if (this->len == another.len)return false;
if (strcmpy(this->str,another.str) == 0/*=0为真*/)return false;
return true;
}
bool MyString::operator >(const MyString &another)
{
int ret = strcmp(this->str,another.str);
if (ret > 0)return true;
else return false;
}
bool MyString::operator >(const MyString &another)
{
int ret = strcmp(this->str,another.str);
if (ret < 0)return true;
else return false;
}
ostream & operator <<(ostream &os,MyString &s)
{
os << s.str;//char*直接输出
return os;
}
istream & operator >>(istream &is,MyString &s)
{
//is>>s.str;//很危险!!!溢出就崩了!
char buf[1024] = {0};
char *temp = buf;
cout<<"请输入一个字符串str:"<<endl;
is>>temp;
delete[] s.str;
s.str = new char[strlen(temp)+1];
strcpy(s.str,temp);
s.len = strlen(temp);
return is;
}
MyString & MyString::operator+(MyString &another)
{
int len = this->len + another.len;
char temp[this->len+1] = {0};
strcpy(temp,this->str);
delete[] this->str;
this->str = new char[len+1];
this->len = len;
strcpy(this->str,temp);
strncat(this->str,another.str,len - strlen(this->str) + 1);
return *this;
}
MyString:: ~MyString()
{
this->len = 0;
if (this->str != NULL)
{
delete[] this->str;
this->str = NULL;
}
}
-----------------------main.cpp-----------------------------
#include <iostream>
#include "MyString.h"
int main(void){
MyString str1(10);
MyString str2("abcdef");
MyString str3 = str1;
MyString str4;
str4 = str2;
str4[0] = '1';//重构[]
cout<<str4<<endl;//提供一个友元函数重载<<(cout,MyString);
return 0;
}
网友评论