C++面试重难点 :自实现string类
一、myString
class myString
{
public:
myString(const char * str=nullptr);
~myString();
myString(const myString & another);
//*****************运算符重载*****************//
myString & operator= (const myString & another);
bool operator== (const myString & another);
bool operator> (const myString & another);
bool operator< (const myString & another);
myString operator+ (const myString & another);
myString & operator+= (const myString & another);
char operator[] (int i);
//********************************************//
char at(int i);
char * c_str();
private:
char * str_;
};
二、实现
(1)构造器(构造函数)
myString(const char * str=nullptr)
{
if(nullptr==str)//空字符串
{
str_=new char[1];
*str_='\0';
}
else
{
str_=new char[strlen(str)+1];//非空字符串
strcpy(str_,str);
}
}
(2)析构器(析构函数)
~myString()
{
delete []str_;
}
(3)拷贝构造
myString(const myString & another)
{
str_=new char[strlen(another.str_)+1];
strcpy(str_,another.str_);
}
(4)运算符重载
myString & operator= (const myString & another)
{
if(this==&another)
return *this;
delete []this->str_;
str_=new char[strlen(another.str_)+1];
strcpy(str_,another.str_);
return *this;
}
bool operator== (const myString & another)
{
return strcmp(this->str_,another.str_)==0;
}
bool operator> (const myString & another)
{
return strcmp(this->str_,another.str_)>0;
}
bool operator< (const myString & another)
{
return strcmp(this->str_,another.str_)<0;
}
myString operator+ (const myString & another)
{
myString ms;
delete []ms.str_;
ms.str_=new char[strlen(this->str_)+strlen(another.str_)+1]{0};
strcat(strcat(ms.str_,this->str_),another.str_);
return ms;
}
myString & operator+= (const myString & another)
{
int catLen=strlen(this->str_);
int srcLen=strlen(another.str_);
this->str_=static_cast<char*>(realloc(this->str_,catLen+srcLen+1));
memset(this->str_+catLen,0,srcLen+1);
strcat(this->str_,another.str_);
return *this;
}
char operator[] (int i)
{
return str_[i];
}
(5)其它
char at(int i)
{
return str_[i];
}
char * c_str()
{
return str_;
}
三、测试
#include <iostream>
#include<string.h>
using namespace std;
class myString
{
public:
myString(const char * str=nullptr)//构造
{
if(nullptr==str)
{
str_=new char[1];
*str_='\0';
}
else
{
str_=new char[strlen(str)+1];
strcpy(str_,str);
}
}
~myString()//析构
{
delete []str_;
}
myString(const myString & another)//拷贝构造
{
str_=new char[strlen(another.str_)+1];
strcpy(str_,another.str_);
}
//*****************运算符重载*****************//
myString & operator= (const myString & another)
{
if(this==&another)
return *this;
delete []this->str_;
str_=new char[strlen(another.str_)+1];
strcpy(str_,another.str_);
return *this;
}
bool operator== (const myString & another)
{
return strcmp(this->str_,another.str_)==0;
}
bool operator> (const myString & another)
{
return strcmp(this->str_,another.str_)>0;
}
bool operator< (const myString & another)
{
return strcmp(this->str_,another.str_)<0;
}
myString operator+ (const myString & another)
{
myString ms;
delete []ms.str_;
ms.str_=new char[strlen(this->str_)+strlen(another.str_)+1]{0};
strcat(strcat(ms.str_,this->str_),another.str_);
return ms;
}
myString & operator+= (const myString & another)
{
int catLen=strlen(this->str_);
int srcLen=strlen(another.str_);
this->str_=static_cast<char*>(realloc(this->str_,catLen+srcLen+1));
memset(this->str_+catLen,0,srcLen+1);
strcat(this->str_,another.str_);
return *this;
}
char operator[] (int i)
{
return str_[i];
}
//********************************************//
char at(int i)
{
return str_[i];
}
char * c_str()
{
return str_;
}
void dis()
{
cout<<"myString:"<<str_<<endl;
}
private:
char * str_;
};
int main()
{
string s;
myString ms;
cout <<"string: "<< s<<" ";
ms.dis();
string s1("china");
myString ms1("china");
cout <<"string: "<< s1<<" ";
ms1.dis();
string s2(s1);
myString ms2(ms1);
cout <<"string: "<< s2<<" ";
ms2.dis();
string s3;
s3=s2;
myString ms3;
ms3=ms2;
cout <<"string: "<< s3<<" ";
ms3.dis();
string s4("china");
if(s1<s4)
cout<<"<"<<endl;
else if(s1>s4)
cout<<">"<<endl;
else
cout<<"=="<<endl;
myString ms4("china");
if(ms1<ms4)
cout<<"<"<<endl;
else if(ms1>ms4)
cout<<">"<<endl;
else
cout<<"=="<<endl;
string s5=" is powerful!";
string s6=s3+s5;
cout <<"string: "<< s6<<" "
" ";
myString ms5=" is powerful!";
myString ms6=ms3+ms5;
ms6.dis();
string s7("beautiful!");
s6+=s7;
cout <<"string: "<< s6<<" ";
myString ms7("beautiful!");
ms6+=ms7;
ms6.dis();
cout <<"string: "<< s6[3]<<" ";
cout <<"myString:"<<ms6[3]<<endl;
cout <<"string: "<< s6.at(9)<<" ";
cout <<"myString:"<<ms6.at(9)<<endl;
cout <<"string: "<< s6.c_str()<<" ";
cout <<"myString:"<<ms6.c_str()<<endl;
return 0;
}