热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

代码分析c++中string类分享!

一:回顾(1)c++中的string类是在面试中和笔试中经常考的题目;工程代码免费下载string类的自行实现(2)c++中的string类和fstream类合起来是处理外部数据的

一:回顾

(1)c++中的string类是在面试中和笔试中经常考的题目; 工程代码免费下载 string类的自行实现

(2)c++中的string类和fstream类合起来是处理外部数据的利器;

(3)string类经常用到find find_first_of find_first_not_of find_last_of find_last_not_of substr replace等,以及联合使用来达到java中的split和trim

(4) 使用friend 仅仅是在类中进行声明的非内部 却可以访问内部成员的外部函数,而且在外部不再需要friend关键字;它与成员函数的区别是,friend和外部函数不含有this对象指针;本文用到了const 定义的全局最大值最小值变量(代替#define)

(5) 有些函数返回的是MyString& 、Char& 等(引用),MyString、Char 等(传值)这得看你返回的对象是函数的局部变量还是全局变量(或者类当前对象成员变量);前者只能返回一个MyString、Char 等;后者强烈建议返回MyString& 、Char& 等(引用);

(6)有些函数的参数是const MyString& ,有些是MyString& (引用);这是为什么?前者是把外部值传提到子函数内部,且不允许改变;后者是作为函数的返回值传递进去的,返回的结果为函数的处理结果(而不用函数自身返回值了)。

二:下面是简单的实现了一下string类,参照的是STL源码,但是自己理解的还是不够深,难免有一些错误,请各位指教

(1)MyString.h文件

  #ifndef MYSTRING_H  #define MYSTRING_H  #include "MyExcept.h"  #include   #include   const int INI_MAX = 0x7fffffff;//2^32npos  const int INI_MIN = 0x80000000;// -2^32  const int npos = 0xffffffff;// npos  using namespace std;    class MyString  {    public:    // constructor    MyString();//    MyString(const MyString &);//    MyString(const char *);    MyString(const size_t,const char);    // destructor    ~MyString();    // attributes      size_t length();// 字符串长度    bool isEmpty();// 返回字符串是否为空    const char* c_str();// 返回c风格的trr的指针    // friend funs    // read writer operations    friend ostream& operator<<(ostream&, const MyString&);    friend istream& operator>> (istream&, MyString&);    //add operation    friend MyString operator+(const MyString&,const MyString&);    // compare operations    friend bool operator==(const MyString&,const MyString&);    friend bool operator!=(const MyString&,const MyString&);    friend bool operator<(const MyString&,const MyString&);    friend bool operator<=(const MyString&,const MyString&);    friend bool operator>(const MyString&,const MyString&);    friend bool operator>=(const MyString&,const MyString&);    // 成员函数实现运算符重载,其实一般需要返回自身对象的,成员函数运算符重载会好一些    // index operation    char& operator[](const size_t);    const char& operator[](const size_t)const;    // =    MyString& operator=(const MyString&);    // +=    MyString& operator+=(const MyString&);    // +=    //MyString operator+=(const MyString&); cannot be overloaded    // 成员操作函数    // substr    MyString substr(size_t pos,const size_t n);    // append    MyString& append(const MyString&);    //insert    MyString& insert(size_t,const MyString&);    //assign 替换    MyString& assign(MyString&,size_t,size_t);    // erase 删除    MyString& erase(size_t,size_t);    //find_first_of 查找某一个字符 size_t 是非符号数的,重载    // 查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。    //搜索从index开始,如果没找到就返回string::npos    int find_first_of(const char* str,size_t index=0);    int find_first_of(const char ch,size_t index=0);    int find_first_of(const MyString &,size_t index=0);    // 在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops    int find_first_not_of(const char* str,size_t index=0);    int find_first_not_of(const char ch,size_t index=0);    int find_first_not_of(const MyString&,size_t index=0);    // swap    void swap(MyString& lhs,MyString& rhs);    // replace_all    MyString& replace_all(const char oldc,const char newc=NULL);    MyString& replace(size_t index,size_t num1,size_t num2,const char ch);    //find    int find(const char* str,size_t index=0);    int find(const MyString& str,size_t index=0);    int find(const char ch,size_t index=0);        //private    private:    char *p_str;    size_t strLength;  };  #endif // MYSTRING_H

(2)MyString.cpp文件

  #include "MyString.h"  #include     // constructor    MyString::MyString():p_str(NULL),strLength(0){}      MyString::MyString(const MyString &str)//    {      if(NULL == str.p_str)      {        return;      }      strLength = str.strLength;      p_str = new char[strLength+1];      strcpy(p_str,str.p_str);    }    MyString::MyString(const char *str)    {      if(NULL == str)      {        return;      }      strLength = strlen(str);      p_str = new char[strLength+1];      strcpy(p_str,str);    }    MyString::MyString(const size_t len,const char ch)    {      if(NULL == ch)      {        return;      }      strLength = len;      p_str = new char[strLength+1];      for(size_t i=0;i> (istream& in, MyString& str)    {      char tmp[100];// 临时字符串      if(in>>tmp)      {        delete[] str.p_str;        str.strLength = strlen(tmp);        str.p_str = new char[str.strLength+1];        strcpy(str.p_str,tmp);      }      return in;    }    // + 加    MyString operator+(const MyString& lhs,const MyString& rhs)    {      MyString ret;      ret.strLength = lhs.strLength + rhs.strLength;      ret.p_str = new char[ret.strLength+1];      strcpy(ret.p_str,lhs.p_str);      strcat(ret.p_str,rhs.p_str);      return ret;    }    // compare operations    bool operator==(const MyString& lhs,const MyString& rhs)    {      return strcmp(lhs.p_str,rhs.p_str)==0?true:false;    }    bool operator!=(const MyString& lhs,const MyString& rhs)    {      return strcmp(lhs.p_str,rhs.p_str)!=0?true:false;    }    bool operator<(const MyString& lhs,const MyString& rhs)    {      return strcmp(lhs.p_str,rhs.p_str)<0?true:false;    }    bool operator<=(const MyString& lhs,const MyString& rhs)    {      return strcmp(lhs.p_str,rhs.p_str)<=0?true:false;    }    bool operator>(const MyString& lhs,const MyString& rhs)    {      return strcmp(lhs.p_str,rhs.p_str)>0?true:false;    }    bool operator>=(const MyString& lhs,const MyString& rhs)    {      return strcmp(lhs.p_str,rhs.p_str)>=0?true:false;    }    // 成员函数实现运算符重载    // index operation    char& MyString::operator[](const size_t index)    {      if(index<0 || index>=strLength)      {        throw Outofbond() ;      }      return p_str[index];    }    const char& MyString::operator[](const size_t index)const    {      if(index<0 || index>=strLength)      {        throw Outofbond();      }      return p_str[index];    }     // = 赋值构造函数(判断是否是自身) 为什么要这样删除呢?    MyString& MyString::operator=(const MyString& other)    {      if(this != &other)      {        if(strLength=strLength)      {        throw Outofbond();      }      MyString ret;      ret.strLength = n;      //ret.p_str = new char[n+1];      ret.p_str = new char[ret.strLength+1]; //也可以      for(size_t i=0;i=strLength)      {        throw Outofbond();      }      char *p_old = p_str;      strLength += other.strLength;      p_str = new char[strLength+1];      for(size_t i=0;i=strLength)  //    {  //      throw Outofbond();  //    }      assert(pos>0 && posstrLength)  //    {  //      throw Outofbond();  //    }  //    size_t index = pos + n;  //    while(*(p_str+index)!='')  //    {  //      *(p_str+index-n) = *(p_str+index);  //      ++index;  //    }  //    *(p_str+index-n) = '';  //    return *this;  //  }    // erase 删除 从pos开始的n个字符    MyString& MyString::erase(size_t pos,size_t n)    {      if((pos+n)>strLength)      {        throw Outofbond();      }      char *p_old = p_str;      strLength -= n;      p_str = new char[strLength+1];      for(size_t i=0;i=strLength)        return npos;      int tmp_len = strlen(str),j;      size_t flag,min_index = INI_MAX;      for(j=0;j=strLength)        return npos;      int j;      size_t flag = npos;      for(size_t i=index;i=strLength)        return npos;      int j;      size_t flag,min_index = INI_MAX;      for(j=0;j=strLength)        return npos;      size_t i=0,j=0;      size_t tmp_len = strlen(str);      for(i=index;i=strLength)        return npos;      size_t i=0,j=0;      for(i=index;i=strLength)        return npos;      size_t i=0;      for(i=index;i

  • 1
  • 2

推荐阅读
  • 纠正网上的错误:自定义一个类叫java.lang.System/String的方法
    本文纠正了网上关于自定义一个类叫java.lang.System/String的错误答案,并详细解释了为什么这种方法是错误的。作者指出,虽然双亲委托机制确实可以阻止自定义的System类被加载,但通过自定义一个特殊的类加载器,可以绕过双亲委托机制,达到自定义System类的目的。作者呼吁读者对网上的内容持怀疑态度,并带着问题来阅读文章。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 摘要: 在测试数据中,生成中文姓名是一个常见的需求。本文介绍了使用C#编写的随机生成中文姓名的方法,并分享了相关代码。作者欢迎读者提出意见和建议。 ... [详细]
  • 怎么在PHP项目中实现一个HTTP断点续传功能发布时间:2021-01-1916:26:06来源:亿速云阅读:96作者:Le ... [详细]
  • Python爬虫中使用正则表达式的方法和注意事项
    本文介绍了在Python爬虫中使用正则表达式的方法和注意事项。首先解释了爬虫的四个主要步骤,并强调了正则表达式在数据处理中的重要性。然后详细介绍了正则表达式的概念和用法,包括检索、替换和过滤文本的功能。同时提到了re模块是Python内置的用于处理正则表达式的模块,并给出了使用正则表达式时需要注意的特殊字符转义和原始字符串的用法。通过本文的学习,读者可以掌握在Python爬虫中使用正则表达式的技巧和方法。 ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
  • 本文介绍了在Cpp中将字符串形式的数值转换为int或float等数值类型的方法,主要使用了strtol、strtod和strtoul函数。这些函数可以将以null结尾的字符串转换为long int、double或unsigned long类型的数值,且支持任意进制的字符串转换。相比之下,atoi函数只能转换十进制数值且没有错误返回。 ... [详细]
  • 本文介绍了解决java开源项目apache commons email简单使用报错的方法,包括使用正确的JAR包和正确的代码配置,以及相关参数的设置。详细介绍了如何使用apache commons email发送邮件。 ... [详细]
  • 本文介绍了关于Java异常的八大常见问题,包括异常管理的最佳做法、在try块中定义的变量不能用于catch或finally的原因以及为什么Double.parseDouble(null)和Integer.parseInt(null)会抛出不同的异常。同时指出这些问题是由于不同的开发人员开发所导致的,不值得过多思考。 ... [详细]
  • 流数据流和IO流的使用及应用
    本文介绍了流数据流和IO流的基本概念和用法,包括输入流、输出流、字节流、字符流、缓冲区等。同时还介绍了异常处理和常用的流类,如FileReader、FileWriter、FileInputStream、FileOutputStream、OutputStreamWriter、InputStreamReader、BufferedReader、BufferedWriter等。此外,还介绍了系统流和标准流的使用。 ... [详细]
  • 本文介绍了使用C++Builder实现获取USB优盘序列号的方法,包括相关的代码和说明。通过该方法,可以获取指定盘符的USB优盘序列号,并将其存放在缓冲中。该方法可以在Windows系统中有效地获取USB优盘序列号,并且适用于C++Builder开发环境。 ... [详细]
  • JVM 学习总结(三)——对象存活判定算法的两种实现
    本文介绍了垃圾收集器在回收堆内存前确定对象存活的两种算法:引用计数算法和可达性分析算法。引用计数算法通过计数器判定对象是否存活,虽然简单高效,但无法解决循环引用的问题;可达性分析算法通过判断对象是否可达来确定存活对象,是主流的Java虚拟机内存管理算法。 ... [详细]
  • Inno Setup区段之Components篇相关知识详解
    本文详细介绍了Inno Setup区段之Components篇相关的知识,包括Components和Types的使用方式以及各个参数的说明,希望对读者有一定的参考价值。内容涵盖了ComponentsName、Description、Types、ExtraDiskSpaceRequired、ExtraDiskSpaceRequiredFlags等多个关键词,帮助读者更好地理解和应用Inno Setup区段之Components篇的知识。 ... [详细]
  • 使用freemaker生成Java代码的步骤及示例代码
    本文介绍了使用freemaker这个jar包生成Java代码的步骤,通过提前编辑好的模板,可以避免写重复代码。首先需要在springboot的pom.xml文件中加入freemaker的依赖包。然后编写模板,定义要生成的Java类的属性和方法。最后编写生成代码的类,通过加载模板文件和数据模型,生成Java代码文件。本文提供了示例代码,并展示了文件目录结构。 ... [详细]
author-avatar
詹詹洋zy_431
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有