news 2026/4/16 16:16:29

C++ string类模拟实现指南:构造、遍历、修改与常用接口

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C++ string类模拟实现指南:构造、遍历、修改与常用接口
string的构造

string的默认构造是不能直接给空指针来进行初始化的

在这里插入图片描述

这种初始化在输出的时候程序就会崩溃,由于还没有写流提取,暂时用c_str输出(其返回的是底层的const char* ,自定义类型不支持,但内置类型支持)

在这里插入图片描述

库里面的string就不会崩溃

在这里插入图片描述

代码语言:javascript

AI代码解释

const char* c_str() { return __str; // 如果 __str == nullptr,返回的就是空指针 } // 使用时: cout << s1.c_str() << endl;

执行流程:s1.c_str() 返回 nullptr(地址 0),cout 的 << 操作符接收到一个 const char* 参数,<< 操作符内部会尝试读取该指针指向的内存内容【ostream::operator<<(const char*) 内部会调用类似 strlen() 的函数来确定字符串长度,strlen(nullptr) 尝试从地址 0 开始逐个字节读取】,直到遇到 \0 结束符。但由于指针是 nullptr,程序尝试访问地址 0 的内存。地址 0 是受保护的:操作系统通常将虚拟地址空间的低地址区域(包括地址 0)标记为不可访问。当程序尝试访问受保护的内存区域时,操作系统会立即终止程序。

所以string的默认构造的对象不能给空指针初始化,库里面的string没有字符也至少有一个’\0’

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

该代码还可以优化,strlen是一个ON的接口,strlen是接收到一个串后,将串中的字符遍历算一遍,带参构造此处strlen算了三遍,就效率不高了

于是就有了下面的写法,但是这样写程序会崩溃

在这里插入图片描述

初始化列表初始化的顺序是按声明的顺序走的,初始化的时候依旧是先初始化_str,此时size是随机值或0,如果给0,0+1=1。就只开了一个字节,之后strcpy就会越界

解决方法就是让_str和_capacity在函数体内初始化,补充一下内置类型即使不在初始化列表显示初始化,其他私有成员变量也会走一遍初始化列表(这种就是隐式初始化了),_str和_capacity两个内置类型会先在初始化列表被初始化(可能是随机值),再到函数体内被赋值

map.fvblxbj.cn/Blog/595331.shtml
map.fvblxbj.cn/Blog/957375.shtml
map.fvblxbj.cn/Blog/339171.shtml
map.fvblxbj.cn/Blog/119355.shtml
map.fvblxbj.cn/Blog/713757.shtml
map.fvblxbj.cn/Blog/711177.shtml
map.fvblxbj.cn/Blog/937375.shtml
map.fvblxbj.cn/Blog/971199.shtml
map.fvblxbj.cn/Blog/357553.shtml
map.fvblxbj.cn/Blog/373537.shtml
map.fvblxbj.cn/Blog/511113.shtml
map.fvblxbj.cn/Blog/511711.shtml
map.fvblxbj.cn/Blog/171737.shtml
map.fvblxbj.cn/Blog/804286.shtml
map.fvblxbj.cn/Blog/157931.shtml
map.fvblxbj.cn/Blog/133197.shtml
map.fvblxbj.cn/Blog/751591.shtml
map.fvblxbj.cn/Blog/173977.shtml
map.fvblxbj.cn/Blog/915511.shtml
map.fvblxbj.cn/Blog/519993.shtml
map.fvblxbj.cn/Blog/133717.shtml
map.fvblxbj.cn/Blog/333997.shtml
map.fvblxbj.cn/Blog/737113.shtml
map.fvblxbj.cn/Blog/338278.shtml
map.fvblxbj.cn/Blog/711593.shtml
map.fvblxbj.cn/Blog/119735.shtml
map.fvblxbj.cn/Blog/935931.shtml
map.fvblxbj.cn/Blog/313571.shtml
map.fvblxbj.cn/Blog/771979.shtml
map.fvblxbj.cn/Blog/575919.shtml
map.fvblxbj.cn/Blog/862224.shtml
map.fvblxbj.cn/Blog/886224.shtml
map.fvblxbj.cn/Blog/042664.shtml
map.fvblxbj.cn/Blog/191993.shtml
map.fvblxbj.cn/Blog/020402.shtml
map.fvblxbj.cn/Blog/648620.shtml
map.fvblxbj.cn/Blog/620482.shtml
map.fvblxbj.cn/Blog/880246.shtml
map.fvblxbj.cn/Blog/862301.shtml
map.fvblxbj.cn/Blog/913513.shtml
map.fvblxbj.cn/Blog/719799.shtml
map.fvblxbj.cn/Blog/911319.shtml
map.fvblxbj.cn/Blog/402200.shtml
map.fvblxbj.cn/Blog/800804.shtml
map.fvblxbj.cn/Blog/462046.shtml
map.fvblxbj.cn/Blog/062840.shtml
map.fvblxbj.cn/Blog/600888.shtml
map.fvblxbj.cn/Blog/759595.shtml
map.fvblxbj.cn/Blog/739519.shtml
map.fvblxbj.cn/Blog/995371.shtml
map.fvblxbj.cn/Blog/371719.shtml
map.fvblxbj.cn/Blog/406280.shtml
map.fvblxbj.cn/Blog/939775.shtml
map.fvblxbj.cn/Blog/991331.shtml
map.fvblxbj.cn/Blog/535911.shtml
map.fvblxbj.cn/Blog/779933.shtml
map.fvblxbj.cn/Blog/393593.shtml
map.fvblxbj.cn/Blog/191379.shtml
map.fvblxbj.cn/Blog/208486.shtml
map.fvblxbj.cn/Blog/222882.shtml
map.fvblxbj.cn/Blog/664206.shtml
map.fvblxbj.cn/Blog/280064.shtml
map.fvblxbj.cn/Blog/793177.shtml
map.fvblxbj.cn/Blog/775959.shtml
map.fvblxbj.cn/Blog/428044.shtml
map.fvblxbj.cn/Blog/137139.shtml
map.fvblxbj.cn/Blog/157935.shtml
map.fvblxbj.cn/Blog/973153.shtml
map.fvblxbj.cn/Blog/488404.shtml
map.fvblxbj.cn/Blog/957517.shtml
map.fvblxbj.cn/Blog/379579.shtml
map.fvblxbj.cn/Blog/573579.shtml
map.fvblxbj.cn/Blog/357511.shtml
map.fvblxbj.cn/Blog/648800.shtml
map.fvblxbj.cn/Blog/153959.shtml
map.fvblxbj.cn/Blog/688084.shtml
map.fvblxbj.cn/Blog/648646.shtml
map.fvblxbj.cn/Blog/666686.shtml
map.fvblxbj.cn/Blog/680084.shtml
map.fvblxbj.cn/Blog/200844.shtml
map.fvblxbj.cn/Blog/755133.shtml
map.fvblxbj.cn/Blog/353371.shtml
map.fvblxbj.cn/Blog/353751.shtml
map.fvblxbj.cn/Blog/266264.shtml
map.fvblxbj.cn/Blog/468206.shtml
map.fvblxbj.cn/Blog/553755.shtml
map.fvblxbj.cn/Blog/351777.shtml
map.fvblxbj.cn/Blog/757735.shtml
map.fvblxbj.cn/Blog/551315.shtml
map.fvblxbj.cn/Blog/177957.shtml
map.fvblxbj.cn/Blog/551573.shtml
map.fvblxbj.cn/Blog/484848.shtml
map.fvblxbj.cn/Blog/222064.shtml
map.fvblxbj.cn/Blog/604228.shtml
map.fvblxbj.cn/Blog/351731.shtml
map.fvblxbj.cn/Blog/195999.shtml
map.fvblxbj.cn/Blog/353575.shtml
map.fvblxbj.cn/Blog/179313.shtml
map.fvblxbj.cn/Blog/753791.shtml
map.fvblxbj.cn/Blog/151797.shtml
map.fvblxbj.cn/Blog/339779.shtml
map.fvblxbj.cn/Blog/393711.shtml
map.fvblxbj.cn/Blog/795135.shtml
map.fvblxbj.cn/Blog/715519.shtml
map.fvblxbj.cn/Blog/195919.shtml
map.fvblxbj.cn/Blog/915157.shtml
map.fvblxbj.cn/Blog/735939.shtml
map.fvblxbj.cn/Blog/533571.shtml
map.fvblxbj.cn/Blog/515599.shtml
map.fvblxbj.cn/Blog/959535.shtml
map.fvblxbj.cn/Blog/779717.shtml
map.fvblxbj.cn/Blog/466446.shtml
map.fvblxbj.cn/Blog/337119.shtml
map.fvblxbj.cn/Blog/713797.shtml
map.fvblxbj.cn/Blog/177351.shtml
map.fvblxbj.cn/Blog/519177.shtml
map.fvblxbj.cn/Blog/551979.shtml
map.fvblxbj.cn/Blog/335795.shtml
map.fvblxbj.cn/Blog/737399.shtml
map.fvblxbj.cn/Blog/137599.shtml
map.fvblxbj.cn/Blog/791999.shtml
map.fvblxbj.cn/Blog/559973.shtml
map.fvblxbj.cn/Blog/157599.shtml
map.fvblxbj.cn/Blog/313135.shtml
map.fvblxbj.cn/Blog/319797.shtml
map.fvblxbj.cn/Blog/979997.shtml
map.fvblxbj.cn/Blog/355353.shtml
map.fvblxbj.cn/Blog/391573.shtml
map.fvblxbj.cn/Blog/533311.shtml
map.fvblxbj.cn/Blog/313133.shtml
map.fvblxbj.cn/Blog/733759.shtml
map.fvblxbj.cn/Blog/197157.shtml
map.fvblxbj.cn/Blog/379733.shtml
map.fvblxbj.cn/Blog/573753.shtml
map.fvblxbj.cn/Blog/137551.shtml
map.fvblxbj.cn/Blog/179733.shtml
map.fvblxbj.cn/Blog/377335.shtml
map.fvblxbj.cn/Blog/191917.shtml
map.fvblxbj.cn/Blog/113113.shtml
map.fvblxbj.cn/Blog/797555.shtml
map.fvblxbj.cn/Blog/335355.shtml
map.fvblxbj.cn/Blog/971971.shtml
map.fvblxbj.cn/Blog/753719.shtml
map.fvblxbj.cn/Blog/555179.shtml
map.fvblxbj.cn/Blog/319557.shtml
map.fvblxbj.cn/Blog/719191.shtml
map.fvblxbj.cn/Blog/155991.shtml
map.fvblxbj.cn/Blog/773979.shtml
map.fvblxbj.cn/Blog/719195.shtml
map.fvblxbj.cn/Blog/199771.shtml
map.fvblxbj.cn/Blog/953755.shtml
map.fvblxbj.cn/Blog/975591.shtml
map.fvblxbj.cn/Blog/955319.shtml
map.fvblxbj.cn/Blog/155511.shtml
map.fvblxbj.cn/Blog/533739.shtml
map.fvblxbj.cn/Blog/193339.shtml
map.fvblxbj.cn/Blog/311973.shtml
map.fvblxbj.cn/Blog/371973.shtml
map.fvblxbj.cn/Blog/911997.shtml
map.fvblxbj.cn/Blog/555199.shtml
map.fvblxbj.cn/Blog/755753.shtml
map.fvblxbj.cn/Blog/551177.shtml
map.fvblxbj.cn/Blog/713999.shtml
map.fvblxbj.cn/Blog/379179.shtml
map.fvblxbj.cn/Blog/359171.shtml
map.fvblxbj.cn/Blog/711337.shtml
map.fvblxbj.cn/Blog/917735.shtml
map.fvblxbj.cn/Blog/519535.shtml
map.fvblxbj.cn/Blog/551355.shtml
map.fvblxbj.cn/Blog/759939.shtml
map.fvblxbj.cn/Blog/951957.shtml
map.fvblxbj.cn/Blog/559393.shtml
map.fvblxbj.cn/Blog/175153.shtml
map.fvblxbj.cn/Blog/575779.shtml
map.fvblxbj.cn/Blog/133559.shtml
map.fvblxbj.cn/Blog/751371.shtml
map.fvblxbj.cn/Blog/513775.shtml
map.fvblxbj.cn/Blog/511173.shtml
map.fvblxbj.cn/Blog/157355.shtml
map.fvblxbj.cn/Blog/999379.shtml
map.fvblxbj.cn/Blog/915539.shtml
map.fvblxbj.cn/Blog/535715.shtml
map.fvblxbj.cn/Blog/557551.shtml
map.fvblxbj.cn/Blog/339993.shtml
map.fvblxbj.cn/Blog/315791.shtml
map.fvblxbj.cn/Blog/373177.shtml
map.fvblxbj.cn/Blog/593735.shtml
map.fvblxbj.cn/Blog/137939.shtml
map.fvblxbj.cn/Blog/733715.shtml
map.fvblxbj.cn/Blog/339771.shtml
map.fvblxbj.cn/Blog/933335.shtml
map.fvblxbj.cn/Blog/319519.shtml
map.fvblxbj.cn/Blog/393513.shtml
map.fvblxbj.cn/Blog/513511.shtml
map.fvblxbj.cn/Blog/317311.shtml
map.fvblxbj.cn/Blog/557391.shtml
map.fvblxbj.cn/Blog/519379.shtml
map.fvblxbj.cn/Blog/353739.shtml
map.fvblxbj.cn/Blog/753999.shtml
map.fvblxbj.cn/Blog/513373.shtml
map.fvblxbj.cn/Blog/135995.shtml
map.fvblxbj.cn/Blog/973331.shtml
map.fvblxbj.cn/Blog/793317.shtml
map.fvblxbj.cn/Blog/197535.shtml
map.fvblxbj.cn/Blog/395137.shtml
map.fvblxbj.cn/Blog/313973.shtml
map.fvblxbj.cn/Blog/977133.shtml
map.fvblxbj.cn/Blog/731717.shtml
map.fvblxbj.cn/Blog/335779.shtml
map.fvblxbj.cn/Blog/597951.shtml
map.fvblxbj.cn/Blog/335573.shtml
map.fvblxbj.cn/Blog/919751.shtml
map.fvblxbj.cn/Blog/795575.shtml
map.fvblxbj.cn/Blog/959573.shtml
map.fvblxbj.cn/Blog/131173.shtml
map.fvblxbj.cn/Blog/711373.shtml
map.fvblxbj.cn/Blog/197159.shtml
map.fvblxbj.cn/Blog/591577.shtml
map.fvblxbj.cn/Blog/913597.shtml
map.fvblxbj.cn/Blog/997513.shtml
map.fvblxbj.cn/Blog/737979.shtml
map.fvblxbj.cn/Blog/399157.shtml
map.fvblxbj.cn/Blog/737959.shtml
map.fvblxbj.cn/Blog/537793.shtml
map.fvblxbj.cn/Blog/733733.shtml
map.fvblxbj.cn/Blog/315593.shtml
map.fvblxbj.cn/Blog/137319.shtml
map.fvblxbj.cn/Blog/739575.shtml
map.fvblxbj.cn/Blog/531179.shtml
map.fvblxbj.cn/Blog/751711.shtml
map.fvblxbj.cn/Blog/575755.shtml
map.fvblxbj.cn/Blog/757333.shtml
map.fvblxbj.cn/Blog/373373.shtml
map.fvblxbj.cn/Blog/333919.shtml
map.fvblxbj.cn/Blog/791959.shtml
map.fvblxbj.cn/Blog/339971.shtml
map.fvblxbj.cn/Blog/153399.shtml
map.fvblxbj.cn/Blog/604268.shtml
map.fvblxbj.cn/Blog/999395.shtml
map.fvblxbj.cn/Blog/953173.shtml
map.fvblxbj.cn/Blog/955577.shtml
map.fvblxbj.cn/Blog/000684.shtml
map.fvblxbj.cn/Blog/991573.shtml
map.fvblxbj.cn/Blog/753539.shtml
map.fvblxbj.cn/Blog/733139.shtml
map.fvblxbj.cn/Blog/911111.shtml
map.fvblxbj.cn/Blog/993135.shtml
map.fvblxbj.cn/Blog/337139.shtml
map.fvblxbj.cn/Blog/357359.shtml
map.fvblxbj.cn/Blog/715193.shtml
map.fvblxbj.cn/Blog/179179.shtml
map.fvblxbj.cn/Blog/951711.shtml
map.fvblxbj.cn/Blog/313797.shtml
map.fvblxbj.cn/Blog/559173.shtml
map.fvblxbj.cn/Blog/179757.shtml
map.fvblxbj.cn/Blog/155579.shtml
map.fvblxbj.cn/Blog/319951.shtml
map.fvblxbj.cn/Blog/111177.shtml
map.fvblxbj.cn/Blog/733553.shtml
map.fvblxbj.cn/Blog/599599.shtml
map.fvblxbj.cn/Blog/957131.shtml
map.fvblxbj.cn/Blog/393355.shtml
map.fvblxbj.cn/Blog/719935.shtml
map.fvblxbj.cn/Blog/399915.shtml
map.fvblxbj.cn/Blog/260884.shtml
map.fvblxbj.cn/Blog/571759.shtml
map.fvblxbj.cn/Blog/955355.shtml
map.fvblxbj.cn/Blog/713933.shtml
map.fvblxbj.cn/Blog/153353.shtml
map.fvblxbj.cn/Blog/593799.shtml
map.fvblxbj.cn/Blog/533911.shtml
map.fvblxbj.cn/Blog/599355.shtml
map.fvblxbj.cn/Blog/397379.shtml
map.fvblxbj.cn/Blog/311333.shtml
map.fvblxbj.cn/Blog/195753.shtml
map.fvblxbj.cn/Blog/517759.shtml
map.fvblxbj.cn/Blog/191195.shtml
map.fvblxbj.cn/Blog/917933.shtml
map.fvblxbj.cn/Blog/159753.shtml
map.fvblxbj.cn/Blog/551373.shtml
map.fvblxbj.cn/Blog/959115.shtml
map.fvblxbj.cn/Blog/353191.shtml
map.fvblxbj.cn/Blog/577773.shtml
map.fvblxbj.cn/Blog/193573.shtml
map.fvblxbj.cn/Blog/579151.shtml
map.fvblxbj.cn/Blog/935515.shtml
map.fvblxbj.cn/Blog/935313.shtml
map.fvblxbj.cn/Blog/919117.shtml
map.fvblxbj.cn/Blog/935771.shtml
map.fvblxbj.cn/Blog/955973.shtml

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

C/C++内存管理详解:从基础原理到自定义内存池原理

、C/C内存管理1.1 C/C内存分布在我们日常的代码编写过程中&#xff0c;不同类型的数据往往会被存储在不同的内存区域。编写完成的代码经过编译后生成可执行程序&#xff0c;当程序运行时&#xff0c;操作系统会将其加载为一个进程。那么&#xff0c;究竟什么是进程&#xff1f;…

作者头像 李华
网站建设 2026/4/16 5:24:22

1小时打造MQTT物联网原型:智能农业监控Demo

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 开发一个智能农业监控系统原型&#xff1a;1) 模拟3个土壤传感器节点(Python)&#xff0c;定期发布湿度数据&#xff1b;2) 中央处理器(Node.js)接收数据并判断是否需要灌溉&#…

作者头像 李华
网站建设 2026/4/16 10:23:49

从手动到自动:Rancher如何将K8s管理效率提升300%

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 构建一个Rancher效率对比工具&#xff0c;功能包括&#xff1a;1. 传统K8s管理流程模拟器&#xff1b;2. Rancher自动化操作演示&#xff1b;3. 时间成本计算器&#xff1b;4. 效率…

作者头像 李华
网站建设 2026/4/16 14:22:00

1小时验证创意:TRANSLUCENTTB原型开发实录

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 快速开发TRANSLUCENTTB概念验证原型&#xff0c;要求&#xff1a;1.基础透明度调节功能 2.简易UI界面 3.包含基础错误处理 4.支持Win10/11 5.生成可立即测试的exe文件。代码不超过…

作者头像 李华
网站建设 2026/4/16 15:47:12

命令行恐惧症?CLAUDE CODE CLI新手友好指南

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 开发一个交互式CLI学习工具&#xff0c;通过渐进式教学帮助用户掌握CLAUDE CODE CLI。包含以下功能&#xff1a;1) 基础命令交互练习 2) 常见任务向导模式 3) 智能错误纠正 4) 个性…

作者头像 李华
网站建设 2026/4/16 15:56:12

如何用AI自动解决Hibernate同步注册错误

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个Java Spring Boot项目&#xff0c;演示如何解决Hibernate的WAS NOT REGISTERED FOR SYNCHRONIZATION BECAUSE SYNCHRONIZATION IS NOT ACTIVE错误。项目应包含&#xff1a…

作者头像 李华