ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++面试重难点 :自实现string类

2019-07-22 14:41:29  阅读:213  来源: 互联网

标签:const str return C++ char 重难点 another myString string


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;
}

在这里插入图片描述

标签:const,str,return,C++,char,重难点,another,myString,string
来源: https://blog.csdn.net/Bryan_QAQ/article/details/96841097

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有