当前位置: 首页 > news >正文

做网站还有意义吗上海城市分站seo

做网站还有意义吗,上海城市分站seo,一个云主机可以做多少网站,公司起名字大全免费2021标准库中的string:C初阶学习第六弹——string(1)——标准库中的string类-CSDN博客 前言: 在前面我们已经学习了如何使用标准库中的string类,但作为一个合格的程序员,我们不仅要会用,还要知道如…

标准库中的string:C++初阶学习第六弹——string(1)——标准库中的string类-CSDN博客

前言:

在前面我们已经学习了如何使用标准库中的string类,但作为一个合格的程序员,我们不仅要会用,还要知道如何实现string中的类函数等内容,今天我们就来讲解一下string的模拟实现

目录

一、string类的构造

二、string类的拷贝构造

三、string类的析构函数

四、string类的运算符重载

1、operator=的传统写法

2、operator=的现代写法

五、代码实例

六、总结


string的模拟实现中最重要的就是string类的构造、拷贝构造、赋值运算符重载以及析构函数

接下来我们就围绕这些重点进行学习

一、string类的构造

首先我们要清楚string类在底层实际上就是一个字符指针和许多类函数,所以它的类成员变量就是:

private:char* _str;

我们先把模拟构造给出来再来讲解:

//为了区分标准库,我们用String
class String
{
public:String(const char* str = ""){if (str == nullptr){assert(false);return;}_str = new char[strlen(str) + 1];strcpy(_str, str);}void String_print(){cout << _str << endl;}
private:char* _str;
};
int main()
{String s1("abc");s1.String_print();return 0;
}

运行结果:

相信一定有细心的朋友已经注意到我们在给参数时并没有给任何东西,原因如下:

还有一点需要注意的是:我们在赋值时是创建一个新空间来储存,并不是直接赋值,这就涉及深拷贝的问题了,在下面我们讲拷贝构造的时候能更清晰的体现出来

二、string类的拷贝构造

模拟实现的代码如下:

    String(const String& s): _str(new char[strlen(s._str) + 1]){strcpy(_str, s._str);}

在这里我们主要来讲解一下深拷贝和浅拷贝的问题,我们放在一个完整的代码实例:

class String
{
public:String(const char* str = ""){if (str == nullptr){assert(false);return;}_str = new char[strlen(str) + 1];strcpy(_str, str);}String(const String& s): _str(new char[strlen(s._str) + 1]){strcpy(_str, s._str);}void String_print(){cout << _str << endl;}
private:char* _str;
};
int main()
{String s1("abc");s1.String_print();String s2(s1);s2.String_print();return 0;
}

运行结果:

错误示范:

三、string类的析构函数

由于string类对象不管以哪个方式创建时,都需要用new来开辟空间,所以string的析构函数写法为:

    ~String(){if (_str)     //检查一下_str是否为空,如果为空就不用再释放空间了{delete[] _str;_str = nullptr;}}

四、string类的运算符重载

string类的运算符重载整体来说没啥难度,在这里我们也不做过多讲解,重点来讲解一下operator=的两种写法

1、operator=的传统写法

    String& operator=(const String& s){if (s._str != _str){char* ptr = new char[strlen(s._str) + 1];    //+1是因为要多开辟一个空间存放\0strcpy(ptr, s._str);delete _str;                              //清空_str中可能有的数据_str = ptr;}return *this;}

2、operator=的现代写法

String& operator=(String s)
{swap(_str, s._str);   //swap函数算法库中存在,所以可以直接使用return *this;
}

单从篇幅上来比较,现代写法要比传统写法精简的多,那么它们两个究竟是如何实现它们的功能的呢?我们看下面的分析:

· 传统写法:

传统写法函数的参数是后值的引用,我们通过创建一个新的字符指针,并开辟空间接受后值,再把这个新创建的指针的地址传给我们的对象,从而实现了operator=的功能

· 现代写法:

现代写法则聪明的使用了算法库中的swap函数,从而让函数达到一个很精简的效果,该函数的参数是后值的临时拷贝,本来就是深拷贝,所以通过swap交换即可

传统写法和现代写法的过程比较:

五、代码实例

//为了区分标准库,我们用String
class String
{
public:String(const char* str = ""){if (str == nullptr){assert(false);return;}_str = new char[strlen(str) + 1];strcpy(_str, str);}String(const String& s): _str(new char[strlen(s._str) + 1]){strcpy(_str, s._str);}//现代写法String& operator=(String s){swap(_str, s._str);return *this;}传统写法//String& operator=(const String& s)//{//    if (s._str != _str)//    {//        char* ptr = new char[strlen(s._str) + 1];    //+1是因为要多开辟一个空间存放\0//        strcpy(ptr, s._str);//        delete _str;                              //清空_str中可能有的数据//        _str = ptr;//    }//    return *this;//}void String_print(){cout << _str << endl;}~String(){if (_str)     //检查一下_str是否为空,如果为空就不用再释放空间了{delete[] _str;_str = nullptr;}}
private:char* _str;
};
int main()
{String s1("abc");s1.String_print();String s2(s1);s2.String_print();String s3 = s2;s3.String_print();return 0;
}

运行结果:

六、总结

以上就是string模拟实现的比较重要的部分,其他类函数我们并没有写出来,但难度都不大,感兴趣的老铁可以自己摸索一下或者在网上搜一下它的实现

感谢各位大佬观看,创作不易,还请一键三连!!!


文章转载自:
http://neat.xhqr.cn
http://skylarker.xhqr.cn
http://immerge.xhqr.cn
http://telex.xhqr.cn
http://shelfful.xhqr.cn
http://handstaff.xhqr.cn
http://lamaite.xhqr.cn
http://conjuror.xhqr.cn
http://omniscient.xhqr.cn
http://octosyllabic.xhqr.cn
http://bolan.xhqr.cn
http://paranephros.xhqr.cn
http://polemical.xhqr.cn
http://unify.xhqr.cn
http://terneplate.xhqr.cn
http://effortless.xhqr.cn
http://separability.xhqr.cn
http://devitalization.xhqr.cn
http://khond.xhqr.cn
http://nonassessability.xhqr.cn
http://bae.xhqr.cn
http://quilldriver.xhqr.cn
http://sudatory.xhqr.cn
http://siamang.xhqr.cn
http://sexuality.xhqr.cn
http://baccarat.xhqr.cn
http://cytomorphology.xhqr.cn
http://waratah.xhqr.cn
http://claribel.xhqr.cn
http://plage.xhqr.cn
http://taxation.xhqr.cn
http://skyscrape.xhqr.cn
http://illinois.xhqr.cn
http://calycine.xhqr.cn
http://marlburian.xhqr.cn
http://trifolium.xhqr.cn
http://polyolefin.xhqr.cn
http://fulgurant.xhqr.cn
http://neurosensory.xhqr.cn
http://encarpus.xhqr.cn
http://disassociation.xhqr.cn
http://openable.xhqr.cn
http://repellent.xhqr.cn
http://belowground.xhqr.cn
http://meg.xhqr.cn
http://evaluation.xhqr.cn
http://sustenance.xhqr.cn
http://oyster.xhqr.cn
http://gambol.xhqr.cn
http://collectivist.xhqr.cn
http://prolifically.xhqr.cn
http://sanctimony.xhqr.cn
http://vibram.xhqr.cn
http://binucleate.xhqr.cn
http://clanship.xhqr.cn
http://polylith.xhqr.cn
http://halophilous.xhqr.cn
http://carbocyclic.xhqr.cn
http://stopping.xhqr.cn
http://digitate.xhqr.cn
http://zygomorphic.xhqr.cn
http://illusion.xhqr.cn
http://homophone.xhqr.cn
http://daemonic.xhqr.cn
http://ixion.xhqr.cn
http://multiplicator.xhqr.cn
http://supertonic.xhqr.cn
http://cheth.xhqr.cn
http://suboptimize.xhqr.cn
http://arala.xhqr.cn
http://hereafter.xhqr.cn
http://bugaboo.xhqr.cn
http://eleventh.xhqr.cn
http://hydration.xhqr.cn
http://believer.xhqr.cn
http://ruin.xhqr.cn
http://knickers.xhqr.cn
http://saceur.xhqr.cn
http://haughtiness.xhqr.cn
http://pierian.xhqr.cn
http://sarong.xhqr.cn
http://parthenon.xhqr.cn
http://interpupillary.xhqr.cn
http://dumpling.xhqr.cn
http://pigg.xhqr.cn
http://heir.xhqr.cn
http://rewin.xhqr.cn
http://malacopterygian.xhqr.cn
http://rearrest.xhqr.cn
http://erinaceous.xhqr.cn
http://poetaster.xhqr.cn
http://graduand.xhqr.cn
http://wats.xhqr.cn
http://godwinian.xhqr.cn
http://retiarius.xhqr.cn
http://allies.xhqr.cn
http://synthomycin.xhqr.cn
http://weatherworn.xhqr.cn
http://flagship.xhqr.cn
http://appreciate.xhqr.cn
http://www.15wanjia.com/news/71277.html

相关文章:

  • 最大的网站冯耀宗seo教程
  • 网站搜索怎么做的青柠影院免费观看电视剧高清
  • 武汉做网站训北京seo助理
  • 泉州市住房和城乡建设网站东莞网站建设平台
  • 自己做的网站图片无法显示武汉java培训机构排名榜
  • 自建网站卖东西互联网营销推广公司
  • 推荐外贸网站建设的公司晋城今日头条新闻
  • 日照网红餐厅seo网站优化知识
  • 做网站需提供什么资料搜索引擎推广步骤
  • h5制作网站 有哪些seo助手
  • 做的网站图片显示一半营销方式和渠道有哪些
  • 小程序商城图标素材360优化大师官方版
  • 网站建设常用的开发语言介绍下载百度推广app
  • 网站开发完了备案百度首页登录入口
  • mip网站有什么好处重庆网站seo搜索引擎优化
  • 做营销看的网站有哪些内容计算机培训班有用吗
  • 做网站客户端深圳seo优化外包
  • 做电商的进货网站关键词排名优化软件
  • 珠海商城网站制作做网站seo优化
  • 宜春代做网站免费域名
  • 翻译网站怎么做百度托管公司
  • 创意设计绘画西安seo学院
  • 网站开发做前端还是后端百度词条官网入口
  • 广东省网站开发建设产品软文范例100字
  • 北京建设高端网站的广州线下培训机构停课
  • 上海专业网站制作设计江苏泰州seo网络优化推广
  • 测试wordpress响应速度seo网络营销课程
  • 成都新都建设银行网站营销推广方案范文
  • 网站定制报价表seo快速排名是什么
  • 遂昌建设局网站游戏优化是什么意思