网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

代码分析c++中string类-创新互联

一:回顾

创新互联公司从2013年开始,是专业互联网技术服务公司,拥有项目成都网站设计、成都网站制作、外贸网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元嵩明做网站,已为上家服务,为嵩明各地企业和个人服务,联系电话:18982081108

(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

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


名称栏目:代码分析c++中string类-创新互联
文章分享:http://bjjierui.cn/article/hcscg.html

其他资讯