news 2026/6/10 19:38:51

【C++STL】新手也能会:string 模拟实现保姆级指南!

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【C++STL】新手也能会:string 模拟实现保姆级指南!
构造函数

代码语言:javascript

AI代码解释

//无参的默认构造 string::string() :_str(new char[1]{'\0'}) ,_size(0) ,_capacity(0) {} //带参的构造 string::string(const char* str) :_size(strlen(str)) //,_size(strlen(str)) //,_capacity(strlen(str)) { cout << "构造" << endl; _capacity = _size; _str = new char[_size+1];//开跟str一样大的空间 还要加1 给\0预留空间 memcpy(_str, str, _size+1); }

带参的构造中有一个细节:不要使用三个strlen 因为strlen效率不高。因为初始化列表有一个按照声明顺序初始化的逻辑,所以初始化列表与声明顺序是有关联关系的,如果实在不好在初始化列表初始化那就在函数体内部初始化。

1.2析构函数

代码语言:javascript

AI代码解释

//析构函数 string::~string() { delete[] _str; _str = nullptr; _size = 0; _capacity = 0; }
1.3拷贝构造

代码语言:javascript

AI代码解释

//拷贝构造 string::string(const string& s) { //开辟新空间 _str = new char[s._capacity+1]; //拷贝旧数据 memcpy(_str, s._str, s._size+1); _size = s._size; _capacity = s._capacity; }

拷贝构造要注意开辟空间和拷贝数据的时候一定不要忘记给\0预留一个空间,因为**string**内部的**_size(有效数据个数)**它指向的是最后一个数据的下一个位置的数据也就是**\0**,而且字符串一般以**\0**作为结束的标识符。

1.4赋值重载

代码语言:javascript

AI代码解释

string& string::operator=(string& s) { //防止给自己赋值 if (this != &s) { //开新空间拷贝旧数据 char* tmp = new char[s._size + 1]; memcpy(tmp, s._str, s._size + 1); delete[] _str; _str = tmp; _size = s._size; _capacity = s._capacity; } return *this; }

对于拷贝构造和赋值重载还有一些现代写法,现代写法相比于传统的写法更加简洁。内部的逻辑其实就是调用std里面的swap交换底层的成员变量来实现的,如果想了解请点击:

1.5再探深浅拷贝问题

之前在类和对象时我们说过如果一个有资源的类我们在没有显示写拷贝构造和赋值重载的时候,使用编译器默认生成的拷贝构造那么完成的就是浅拷贝:

www.dongchedi.com/article/7592326493200482841
www.dongchedi.com/article/7592329049750995481
www.dongchedi.com/article/7592327070701961753
www.dongchedi.com/article/7592322906135413273
www.dongchedi.com/article/7592308231180026392
www.dongchedi.com/article/7592331190716498457
www.dongchedi.com/article/7592320610865676825
www.dongchedi.com/article/7592326493201007129
www.dongchedi.com/article/7592322714795688510
www.dongchedi.com/article/7592327392942178841
www.dongchedi.com/article/7592319617927283225
www.dongchedi.com/article/7592327476282638873
www.dongchedi.com/article/7592319094112846360
www.dongchedi.com/article/7592326854393152025
www.dongchedi.com/article/7592319386741244441
www.dongchedi.com/article/7592321316729504281
www.dongchedi.com/article/7592319724022039102
www.dongchedi.com/article/7592327140348379672
www.dongchedi.com/article/7592319412238418457
www.dongchedi.com/article/7592319724022137406
www.dongchedi.com/article/7592319070578917950
www.dongchedi.com/article/7592317677180486168
www.dongchedi.com/article/7592331430680953368
www.dongchedi.com/article/7592317943112057368
www.dongchedi.com/article/7592312482736685593
www.dongchedi.com/article/7592326787452011070
www.dongchedi.com/article/7592320171805934142
www.dongchedi.com/article/7592312175293891097
www.dongchedi.com/article/7592326095602221593
www.dongchedi.com/article/7592313460353466942
www.dongchedi.com/article/7592315921234985497
www.dongchedi.com/article/7592318584517493310
www.dongchedi.com/article/7592313903104164377
www.dongchedi.com/article/7592314981853905432
www.dongchedi.com/article/7592327419139654169
www.dongchedi.com/article/7592312482736620057
www.dongchedi.com/article/7592327392941589017
www.dongchedi.com/article/7592313382368297497
www.dongchedi.com/article/7592317900531548734
www.dongchedi.com/article/7592315372943327769
www.dongchedi.com/article/7592311873639432729
www.dongchedi.com/article/7592316979160121918
www.dongchedi.com/article/7592312719127511577
www.dongchedi.com/article/7592316786557616664
www.dongchedi.com/article/7592327116290032153
www.dongchedi.com/article/7592326416835002904
www.dongchedi.com/article/7592313347400270360

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 12:39:15

Drools性能优化终极指南:7大核心技巧提升规则引擎效率

Drools性能优化终极指南&#xff1a;7大核心技巧提升规则引擎效率 【免费下载链接】incubator-kie-drools Drools is a rule engine, DMN engine and complex event processing (CEP) engine for Java. 项目地址: https://gitcode.com/gh_mirrors/in/incubator-kie-drools …

作者头像 李华
网站建设 2026/6/10 12:25:57

零代码AI编程革命:机器学习入门全攻略

零代码AI编程革命&#xff1a;机器学习入门全攻略 【免费下载链接】ml2scratch 機械学習 x スクラッチ(Connect Machine Learning with Scratch) 项目地址: https://gitcode.com/gh_mirrors/ml/ml2scratch 在人工智能技术快速发展的今天&#xff0c;ML2Scratch项目为初学…

作者头像 李华
网站建设 2026/6/10 15:04:02

Keil5添加文件深度剖析:源文件与头文件处理机制

Keil5文件管理深度揭秘&#xff1a;从“添加文件”到工程架构的底层逻辑在嵌入式开发的世界里&#xff0c;几乎每位工程师都曾经历过这样一个瞬间——点击“Build”后&#xff0c;编译器冷冰冰地抛出一句&#xff1a;fatal error: xxx.h: No such file or directory于是开始翻目…

作者头像 李华
网站建设 2026/6/10 16:50:01

UnattendedWinstall终极指南:从零开始掌握Windows自动化安装

UnattendedWinstall终极指南&#xff1a;从零开始掌握Windows自动化安装 【免费下载链接】UnattendedWinstall Personalized Unattended Answer File that helps automatically debloat and customize Windows 10 & 11 during the installation process. 项目地址: https…

作者头像 李华
网站建设 2026/6/10 12:28:12

ms-swift框架下能源消耗预测与优化模型开发

ms-swift框架下能源消耗预测与优化模型开发 在工业智能化浪潮中&#xff0c;一个现实挑战正日益凸显&#xff1a;如何让动辄数十GB的大模型真正“跑得动、用得起、控得住”&#xff1f;尤其是在能源管理系统这类对实时性、成本和可靠性要求极高的场景里&#xff0c;传统AI工程流…

作者头像 李华