news 2026/4/17 9:44:00

【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/4/16 23:38:55

基于ms-swift的城市建筑三维重建模型

基于 ms-swift 的城市建筑三维重建模型 在智慧城市和数字孪生的浪潮中&#xff0c;如何快速、精准地构建大规模城市级三维模型&#xff0c;正成为制约产业落地的关键瓶颈。传统依赖激光雷达扫描或人工建模的方式&#xff0c;不仅成本高昂、周期漫长&#xff0c;更难以应对城市动…

作者头像 李华
网站建设 2026/4/15 18:45:02

C++ 波澜壮阔 40 年:从基础I/O到函数重载与引用的完整构建

、梦的出发点&#xff1a;C输入&&输出 1.1 一览&#xff1a;入门C要知道的 核心组件 < iostream >标准库&#xff08;一个头文件&#xff09;&#xff0c;是Input Out Stream的缩写&#xff0c;它是标准的输入、输出流库&#xff0c;定义标准的输入、输出对象…

作者头像 李华
网站建设 2026/4/15 18:46:07

终极免费NVMe-VMD固件替代方案:DMA技术完整指南

终极免费NVMe-VMD固件替代方案&#xff1a;DMA技术完整指南 【免费下载链接】Pcileech-DMA-NAMe-VMD Firmware emulation to implement NVMe-VMD functionality 项目地址: https://gitcode.com/gh_mirrors/pc/Pcileech-DMA-NAMe-VMD 还在为昂贵的VMD固件而烦恼吗&#x…

作者头像 李华
网站建设 2026/4/15 18:45:01

Uber FX依赖注入框架完整指南:构建优雅的Go应用架构

Uber FX依赖注入框架完整指南&#xff1a;构建优雅的Go应用架构 【免费下载链接】fx A dependency injection based application framework for Go. 项目地址: https://gitcode.com/gh_mirrors/fx1/fx Uber FX是一个基于依赖注入的Go语言应用程序框架&#xff0c;专为构…

作者头像 李华
网站建设 2026/4/7 2:39:23

DeepPurpose:AI药物发现的终极完整教程

DeepPurpose&#xff1a;AI药物发现的终极完整教程 【免费下载链接】DeepPurpose A Deep Learning Toolkit for DTI, Drug Property, PPI, DDI, Protein Function Prediction (Bioinformatics) 项目地址: https://gitcode.com/gh_mirrors/de/DeepPurpose DeepPurpose是一…

作者头像 李华
网站建设 2026/4/16 8:27:30

快速上手Phi-2:解锁27亿参数模型的强大文本生成能力

快速上手Phi-2&#xff1a;解锁27亿参数模型的强大文本生成能力 【免费下载链接】phi-2 项目地址: https://ai.gitcode.com/hf_mirrors/ai-gitcode/phi-2 想要体验前沿AI技术却担心配置复杂&#xff1f;Phi-2作为微软推出的27亿参数Transformer模型&#xff0c;以其出色…

作者头像 李华