【C++干货基地】揭秘C++STL库的魅力:stiring的初步了解和使用


在这里插入图片描述

🎬 鸽芷咕:个人主页

 🔥 个人专栏: 《C++干货基地》《粉丝福利》

⛺️生活的理想,就是为了理想的生活!

引入

  哈喽各位铁汁们好啊,我是博主鸽芷咕《C++干货基地》是由我的襄阳家乡零食基地有感而发,不知道各位的城市有没有这种实惠又全面的零食基地呢?C++ 本身作为一门篇底层的一种语言,世面的免费课程大多都没有教明白。所以本篇专栏的内容全是干货让大家从底层了解C++,把更多的知识由抽象到简单通俗易懂。

⛳️ 推荐

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。

文章目录

  • 引入
  • ⛳️ 推荐
  • 一、STL是什么?
  • 二、STL的六大组件
  • 三、我们为什么要学string?
    • 3.1 string 的定义
  • 四、string的常用接口使用
    • 4.1 成员函数
      • 构造函数
      • 拷贝构造
      • operator=
    • 4.2 迭代器的使用
      • string 的三种遍历方式
      • rbegin && rend
    • 4.3 容量部分
      • capacity 获取当前容量
      • size 获取当前存储了多少字符
      • resize 减少字符存储,或填充字符
      • reserve 为string扩容
      • clear 清空所有字符
      • empty 判断当前字符串是否为空
      • shrink_to_fit 为当前字符串请求缩容
    • 4.4 元素访问
      • operator[]
    • 4.5 修改
      • += 操作
      • append 追加字符或字符串
      • push_back 尾插
      • assign 替换字符串
      • insert 插入
      • erase 删除字符串的一部分,减少其长度
      • replace 替换
      • swap交换
    • 4.6 字符串的操作
      • find 查找字符或字符串
      • rfind 从后往前查找字符或字符串
      • c_str 返回C形式的字符串指针

一、STL是什么?

STL我相信各位学C++的肯定都不会陌生,C++自从模版出来之后就发生了革命性的意义。有了模版这个东西我们就可以只书写一个库来不给不同类型的数据使用。

在这里插入图片描述

STL(standard template libaray-标准模板库):是C++标准库的重要组成部分,不仅是一个可复用的组件库,而且是一个包罗数据结构与算法的软件框架。

二、STL的六大组件

在这里插入图片描述

STL主要是由四大组件组成的,前面说了STL 是一个包罗数据结构与算法的软件框架 其中里面的容器就是数据结构库含有各种常用的数据结构

  • 例如 顺序表 链表 队列 二叉树 等等常用数据结构
  • 其中今天介绍的string 其实也算是 STL 的一员是 存放字符的顺序表

但是由于历史原因,string是先出来的 STL 是后面由惠普实验室后开发出来开源所以人们并没有把string 归类到STL 之中。

三、我们为什么要学string?

在C语言中,字符串是以’\0’结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列的库函数。

  • 但是这些库函数与字符串是分离开的,不太符合OOP的思想.
  • 而且底层空间需要用户自己管理,稍不留神可能还会越界访问。

所以在C++中 专门把字符串操作封装成了 string 容器,来给开发者更好的调用接口支持。不用去管理底层的空间分配使得使用更加省心。

3.1 string 的定义

  1. string是表示字符串的字符串类
  2. 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。
    比特就业课
  3. string在底层实际是:basic_string模板类的别名,typedef basic_string<char, char_traits, allocator>
    string;
  4. 不能操作多字节或者变长字符的序列。

在使用string类时,必须包含#include头文件以及using namespace std;

四、string的常用接口使用

4.1 成员函数

构造函数

构造函数介绍我们初始化string 对象的几种方法

  • 1. 构造空的string类对象,即空字符串
int main()
{string s1();return 0;
}
  • 2. 用C-string来构造string类对象
#include<iostream>
using namespace std;int main()
{string s1("hello gugu");cout << s1 << endl;return 0;
}
  • 3.使用string 中的 pos 位置开始,n个字符开始构造
#include<iostream>
using namespace std;int main()
{string s1("hello gugu");string s2(s1, 6, 4);cout << s2 << endl;return 0;
}
  • 4.使用 n 个字符初始化
#include<iostream>
using namespace std;int main()
{string s1(4,'x');cout << s1 << endl;return 0;
}

拷贝构造

#include<iostream>
using namespace std;int main()
{string s1("hello gugu");string s2(s1);cout << s2 << endl;return 0;
}

operator=

#include<iostream>
using namespace std;int main()
{string s1("hello gugu");string s2= s1;cout << s2 << endl;return 0;
}

4.2 迭代器的使用

迭代器是C++提供的一种新的遍历方式,其底层是一种类似指针的实现方式。可能很多人觉得这有什么可说的,但是迭代器不仅可以遍历string还能遍历二叉树链表是一种通用的遍历方式。

string 的三种遍历方式

  • 使用迭代器遍历
#include<iostream>
using namespace std;int main()
{string s1("hello gugu");//使用迭代器遍历string::iterator it1 = s1.begin();while (it1 != s1.end()){cout << *it1 << " ";it1++;}cout << endl;//使用迭代器修改string::iterator it2 = s1.begin();while (it2 != s1.end()){*it2 -= 1;cout << *it2 << " ";it2++;}cout << endl;return 0;
}
  • 使用方括号遍历 【】
#include<iostream>
using namespace std;int main()
{string s1("hello gugu");for (int i = 0; i < s1.size(); i++){cout << s1[i] << ' ';}cout << endl;return 0;
}
  • 使用范围 for 遍历
#include<iostream>
using namespace std;int main()
{string s1("hello gugu");for (auto e : s1){cout << e << ' ';}cout << endl;return 0;
}

rbegin && rend

这俩就是反向迭代器,使用他们打印出来的结果是从后往前

int main()
{string s1("hello gugu");//使用迭代器遍历string::reverse_iterator rit = s1.rbegin();while (rit != s1.rend()){cout << *rit << " ";++rit;}cout << endl;return 0;
}

4.3 容量部分

capacity 获取当前容量

#include<iostream>
using namespace std;int main()
{string s1("hello gugu");cout << s1.capacity() << endl;return 0;
}

size 获取当前存储了多少字符

#include<iostream>
using namespace std;int main()
{string s1("hello gugu");cout << s1.size() << endl;return 0;
}

resize 减少字符存储,或填充字符

#include<iostream>
using namespace std;int main()
{string s1("hello gugu");s1.resize(3, 'x');cout << s1 << endl;s1.resize(5);cout << s1 << endl;return 0;
}

reserve 为string扩容

#include<iostream>
using namespace std;int main()
{string s1("hello gugu");cout << s1.capacity() << endl;s1.reserve(6);cout << s1.capacity() << endl;s1.resize(100);cout << s1.capacity() << endl;return 0;
}

clear 清空所有字符

#include<iostream>
using namespace std;int main()
{string s1("hello gugu");cout << s1 << endl;s1.clear();cout << s1 << endl;return 0;
}

empty 判断当前字符串是否为空

#include<iostream>
using namespace std;int main()
{string s1("hello gugu");cout << s1.empty() << endl;return 0;
}

shrink_to_fit 为当前字符串请求缩容

#include<iostream>
using namespace std;int main()
{string s1("hello gugu");cout << s1.capacity() << endl;s1.reserve(100);cout << s1.capacity() << endl;s1.shrink_to_fit();cout << s1.capacity() << endl;return 0;
}

4.4 元素访问

operator[]

```cpp
#include<iostream>
using namespace std;int main()
{string s1("hello gugu");for (int i = 0; i < s1.size(); i++){cout << s1[i] << ' ';}cout << endl;return 0;
}

4.5 修改

+= 操作

#include<iostream>
using namespace std;int main()
{string s1("hello gugu");string s2("xxxxx");s1 += s2;cout << s1 << endl;s1 += "vvvv";cout << s1 << endl;s1 += 'x';cout << s1 << endl;return 0;
}

append 追加字符或字符串

#include<iostream>
using namespace std;int main()
{string s1("hello gugu");string s2("xxxxx");s1.append(s2);cout << s1 << endl;s1.append("vvvv");cout << s1 << endl;s1.append(4,'c');cout << s1 << endl;s1.append("abcdef",3);cout << s1 << endl;return 0;
}

push_back 尾插

#include<iostream>
using namespace std;int main()
{string s1("hello gugu");s1.push_back('x');cout << s1 << endl;return 0;
}

assign 替换字符串

#include<iostream>
using namespace std;int main()
{string s1("hello gugu");string s2("xxxxxxx");s1.assign(s2);cout << s1 << endl;s1.assign("Linux C++");cout << s1 << endl;s1.assign(5,'c');cout << s1 << endl;return 0;
}

insert 插入

#include<iostream>
using namespace std;int main()
{string s1("hello gugu");string s2("C++");s1.insert(6, s2);cout << s1 << endl;s1.insert(2, "xxxx");cout << s1 << endl;s1.insert(6, 2,'v');cout << s1 << endl;s1.insert(6,"bbbbbb",2);cout << s1 << endl;return 0;
}

erase 删除字符串的一部分,减少其长度

#include<iostream>
using namespace std;int main()
{string s1("hello gugu");cout << s1 << endl;cout << s1.size() << endl;cout << s1.capacity() << endl;s1.erase(5);cout << s1 << endl;cout << s1.size() << endl;cout << s1.capacity() << endl;return 0;
}

replace 替换

#include<iostream>
using namespace std;int main()
{string s1("hello gugu");s1.replace(5,1,"C++");cout << s1 << endl;return 0;
}

swap交换

#include<iostream>
using namespace std;int main()
{string s1("hello gugu");string s2("C++ Linux");cout << s1 << endl;cout << s2 << endl;swap(s1, s2);cout << s1 << endl;cout << s2 << endl;return 0;
}

4.6 字符串的操作

find 查找字符或字符串

#include<iostream>
using namespace std;int main()
{string s1("hello gugu");int pos = s1.find('g');cout << s1[pos];return 0;
}

rfind 从后往前查找字符或字符串

#include<iostream>
using namespace std;int main()
{string s1("hello gugu");int pos = s1.rfind('g');cout << pos << endl;cout << s1[pos];return 0;
}

c_str 返回C形式的字符串指针

#include<iostream>
using namespace std;int main()
{string s1("hello gugu");cout << s1.c_str() << endl;return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/3018490.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

使用Portal V17搜索PN(profinet)设备的方法

这里的PN就是profinet&#xff0c;无需连接PLC&#xff0c;只需要将PN设备连接电脑即可&#xff0c;如下图&#xff0c; 跳出如下窗口&#xff0c; 点击start search 搜索完毕后就看到PN设备的名字啦&#xff1a; 是不是很简单呢。

泰克示波器怎么看波形的有效值?

泰克示波器是一款高性能的数字示波器&#xff0c;可以用于各种信号的测量和分析。在实际测试中&#xff0c;我们经常需要了解信号的有效值&#xff0c;以评估信号的幅度大小和稳定性。泰克示波器提供了多种方法来查看信号的有效值&#xff0c;下面将介绍几种常用的方法。 垂直…

Redis 源码安装(CentOS 单机)

序言 本文给大家介绍如何在 CentOS 上&#xff0c;通过 Redis 源码单机部署 Redis 服务。 一、部署流程 通过官网下载源码 # 下载源码 wget https://download.redis.io/redis-stable.tar.gz# 解压源码包 tar -xzvf redis-stable.tar.gz在 linux 中执行以下命令&#xff0c;安…

品鉴中的音乐搭配:如何为红酒选择合适的音乐伴侣

品鉴红酒时&#xff0c;音乐是一个不可忽视的元素。合适的音乐能够增强红酒的口感&#xff0c;提升品鉴体验。对于云仓酒庄雷盛红酒而言&#xff0c;如何为其选择合适的音乐伴侣&#xff0c;是一个值得探讨的话题。 首先&#xff0c;了解红酒的风格和特点至关重要。云仓酒庄雷…

(图论)最短路问题合集(包含C,C++,Java,Python,Go)

不存在负权边&#xff1a; 1.朴素dijkstra算法 原题&#xff1a; 思路&#xff1a;&#xff08;依然是贪心的思想&#xff09; 1.初始化距离&#xff1a;dis[1]0&#xff0c;dis[i]INF&#xff08;正无穷&#xff09; 2.循环n次&#xff1a; 找到当前不在s中的dis最小的点&…

推荐一个gpt全能网站

进入后&#xff0c;里面是这样的 点开后&#xff0c;里面是这样的 你以为只有这些吗&#xff1f; 往下翻一翻&#xff0c;你会发现新大陆&#xff01;&#xff01; 在输入框的下面&#xff0c;有一个分类栏&#xff0c;鼠标移上去&#xff0c;下面就会给出一堆网站 光是gp…

Github 2024-05-08 C开源项目日报 Top8

根据Github Trendings的统计,今日(2024-05-08统计)共有8个项目上榜。根据开发语言中项目的数量,汇总情况如下: 开发语言项目数量C项目8PHP项目1Python项目1C++项目1PHP:流行的Web开发脚本语言 创建周期:4710 天开发语言:C, PHP协议类型:OtherStar数量:37340 个Fork数量…

上市公司财务困境模型​MertonDD、OScore、RLPM、ZScore四种模型​(1992-2022年)

01、数据介绍 上市公司财务困境模型是用于预测和评估上市公司是否可能陷入财务困境的一种模型。这个模型通常基于一系列的财务比率和其他相关变量&#xff0c;通过统计分析方法来构建。​ 数据名称&#xff1a;上市公司财务困境模型MertonDD、OScore、RLPM、ZScore五种模型 …

从零开始:搭建PXE远程批量安装服务器

在大规模服务器部署时&#xff0c;面对成百上千台服务器&#xff0c;通过手动插入光盘或者USE驱动器来安装操作系统无比繁琐&#xff0c;让大量工程师在现场挨个安装系统也不切实际&#xff0c;PXE的出现使得网络远程批量自动安装和配置操作系统成为现实。 什么是PXE&#xff1…

layui的treeTable组件,多层级上传按钮失效的问题解决

现象描述: layui的treeTable 的上传按钮在一层能用&#xff0c;展开后其他按钮正常点击&#xff0c;上传按钮无效。 具体原因没有深究&#xff0c;大概率是展开的子菜单没有被渲染treeTable的done管理到&#xff0c;导致没有重绘上传按钮。 解决方案: 不使用layu的上传组件方法…

Pytorch学习笔记——神经网络基本框架

一、神经网络是什么 神经网络在人工智能和深度学习的领域&#xff0c;一般称为人工神经网络&#xff0c;即ANN(Artificial Neural Network)&#xff0c;是一种模仿人脑神经系统工作方式的计算模型。被广泛应用于人工智能、自动控制、机器人、统计学等领域的信息处理中。 二、…

docker搭建代码审计平台sonarqube

docker搭建代码审计平台sonarqube 一、代码审计关注的质量指标二、静态分析技术分类三、sonarqube流程四、快速搭建sonarqube五、sonarqube scanner的安装和使用 一、代码审计关注的质量指标 代码坏味道 代码规范技术债评估 bug和漏洞代码重复度单测与集成 测试用例数量覆盖率…

使用sqlmodel实现唯一性校验

代码&#xff1a; from sqlmodel import Field, Session, SQLModel, create_engine# 声明模型 class User(SQLModel, tableTrue):id: int | None Field(defaultNone, primary_keyTrue)# 不能为空&#xff0c;必须唯一name: str Field(nullableFalse, uniqueTrue)age: int | …

HarmonyOS NEXT应用开发之多模态页面转场动效实现案例

介绍 本示例介绍多模态页面转场动效实现&#xff1a;通过半模态转场实现半模态登录界面&#xff0c; 与全屏模态和组件转场结合实现多模态组合登录场景&#xff0c;其中手机验证码登录与账号密码登录都为组件&#xff0c; 通过TransitionEffect.move()实现组件间转场达到近似页…

跟随Facebook的足迹:社交媒体背后的探索之旅

在当今数字化时代&#xff0c;社交媒体已经成为了人们日常生活中不可或缺的一部分。而在这庞大的社交媒体网络中&#xff0c;Facebook作为其中的巨头&#xff0c;一直在引领着潮流。从创立之初的一个大学社交网络到如今的全球性平台&#xff0c;Facebook的发展历程承载了无数故…

tf2使用savemodel保存之后转化为onnx适合进行om模型部署

tf2使用savemodel保存之后转化为onnx适合进行om模型部署 tf保存为kears框架h5文件将h5转化为savemodel格式&#xff0c;方便部署查看模型架构将savemodel转化为onnx格式使用netrononnx模型细微处理代码转化为om以及推理代码&#xff0c;要么使用midstudio tf保存为kears框架h5文…

基于SSM的“游戏交易网站”的设计与实现(源码+数据库+文档+PPT)

基于SSM的“游戏交易网站”的设计与实现&#xff08;源码数据库文档PPT) 开发语言&#xff1a;Java 数据库&#xff1a;MySQL 技术&#xff1a;SSM 工具&#xff1a;IDEA/Ecilpse、Navicat、Maven 系统展示 游戏交易网站功能结构图 游戏交易网站首页 游戏交易网站用户注册…

Hive Partitioned Tables 分区表

Hive Partitioned Tables 分区表 1.分区表概念 Hive分区表&#xff08;Partitioned Tables&#xff09;是一种用于管理大量数据的机制&#xff0c;它可以将数据分散到不同的目录或分区中&#xff0c;以提高查询性能、优化数据存储和管理。 这种表结构可以根据某个列的值进行分…

Jenkins +配置邮件 centos8.5 安装部署 运维系列一

1 jenkins的war包下载地址: Download and deploy 2 xftp 等方式上传到服务器 #安装jdk tar zxvf jdk-11.0.8_linux-x64_bin.tar.gz mv jdk-11.0.8/ /usr/local/jdk vim /etc/profile export JAVA_HOME/usr/local/jdk export PATH$JAVA_HOME/bin:$PATH CLASSPATH.:$JAVA_…

三分钟了解计算机网络核心概念-数据链路层和物理层

计算机网络数据链路层和物理层 节点&#xff1a;一般指链路层协议中的设备。 链路&#xff1a;一般把沿着通信路径连接相邻节点的通信信道称为链路。 MAC 协议&#xff1a;媒体访问控制协议&#xff0c;它规定了帧在链路上传输的规则。 奇偶校验位&#xff1a;一种差错检测方…