news 2026/2/17 5:16:52

【C++STL】一文掌握 String 核心接口:从基础到实用!

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【C++STL】一文掌握 String 核心接口:从基础到实用!

,STL简介

1.1 什么是STL?

STL的概念:STL(Standard Template Library)是C++标准库的核心组成部分,提供了一套通用的模板类和函数,用于实现常见的数据结构和算法。

1.2 STL的六大组件

STL的六大组件是它的核心我们在学习STL的时候也是围绕着这六大组件来进行学习,下面看看六大组件都有哪些:

在这里插入图片描述

1.3 如何学习STL

STL提供了一套通用的模板和函数,用于实现常见的数据结构,那么这些函数和数据结构我们首先要学会使用,其实就是了解一下这些数据结构的底层是如何实现的。 所以学习STL的步骤就是1.学会使用 2.了解底层 (后面的STL学习都会按照这两个步骤来学习)

声明

二,第一个STL容器—string

2.1 string的4个默认构造的使用

函数名称

功能说明

string()

构造空的string类对象,即空字符串(重点)

string(const char* s)

用C风格字符串(C-string)构造string类对象(重点)

string(size_t n, char c)

构造包含n个字符c的string类对象

string(const string& s)

通过拷贝另一个string类对象构造新对象(重点)

我们先来看看库里面的一些函数重载:

在这里插入图片描述

在这里插入图片描述

代码语言:javascript

AI代码解释

void TestString1() { string s1;//创建一个空字符串对象 string s2("hello world");//构造 string s3(s2);//拷贝构造 cout << s3 << endl; string s4(s2, 1, 8);//相当于memcopy //string s4(s1, 1, 60); cout << s4 << endl; string s6(s2, 1);//不写长度那就从1开始拷贝 直到s2被拷贝完 cout << s6 << endl; const char* str1 = "hello word"; string s7(str1,5); //使用char* 的字符串构造 cout << s7 << endl; string s8(100, '*');//将s8初始化成100个* cout << s8 << endl; //赋值运算符重载 s1.operator=() s1 = s2; s1 = "*******"; cout << s1 << endl; }

还有一个析构函数用于释放string内部的资源,在模拟实现部分会详细说明。

三,初识迭代器

3.1 使用迭代器遍历string

学习一个数据结构就要学会它的遍历,因为在使用数据结构的时候我们常常会修改数据结构内部的值,如果我们要一一进行修改那么就一定会用到遍历。在学习C语言的时候常见的遍历方式是使用for循环,while循环等,借助循环来遍历。而C++搞了一个新的遍历方式叫迭代器,它是所有的主流遍历方式,下面我们重点讲解它。

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/2/8 17:43:27

RLPR-Qwen2.5:无需验证器,推理能力狂飙!

RLPR-Qwen2.5&#xff1a;无需验证器&#xff0c;推理能力狂飙&#xff01; 【免费下载链接】RLPR-Qwen2.5-7B-Base 项目地址: https://ai.gitcode.com/OpenBMB/RLPR-Qwen2.5-7B-Base 导语&#xff1a;OpenBMB团队推出的RLPR-Qwen2.5-7B-Base模型&#xff0c;通过创新的…

作者头像 李华
网站建设 2026/2/12 13:14:52

如何通过ms-swift实现突发事件响应预案?

如何通过 ms-swift 实现突发事件响应预案&#xff1f; 在城市应急管理的实战中&#xff0c;时间就是生命。一场突发火灾、一次地质灾害或公共卫生事件&#xff0c;往往留给决策者的时间窗口仅有几分钟到几小时。传统的应急系统依赖人工研判和固定流程&#xff0c;面对海量异构信…

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

本地AI代码文档自动化:技术演进与实践探索

本地AI代码文档自动化&#xff1a;技术演进与实践探索 【免费下载链接】deepwiki-open Open Source DeepWiki: AI-Powered Wiki Generator for GitHub Repositories 项目地址: https://gitcode.com/gh_mirrors/de/deepwiki-open 在软件开发领域&#xff0c;代码文档的缺…

作者头像 李华
网站建设 2026/2/15 5:12:37

Harmony深度解析:.NET运行时动态方法修补实战指南

Harmony深度解析&#xff1a;.NET运行时动态方法修补实战指南 【免费下载链接】Harmony A library for patching, replacing and decorating .NET and Mono methods during runtime 项目地址: https://gitcode.com/gh_mirrors/ha/Harmony 技术原理与架构设计 Harmony库…

作者头像 李华
网站建设 2026/2/6 14:55:04

使用ms-swift进行短视频内容审核模型训练

使用 ms-swift 构建短视频内容审核系统&#xff1a;从模型训练到生产落地 在抖音、快手、TikTok 等平台日均处理数十亿条视频的今天&#xff0c;内容安全早已不是“人工关键词过滤”能应对的挑战。一个用户上传的15秒短视频&#xff0c;可能包含衣着暴露的画面、隐喻低俗的文案…

作者头像 李华