【黑马程序员】STL容器之string

news/发布时间2024/5/16 1:20:43

string

string 基本概念

string本质

  • string是c++风格的字符串,而string本质上是一个类

string和char* 区别

  • char* 是一个指针
  • string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器

特点

  • string 内部封装了很多成员方法
  • string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责

string构造函数

  • string的多种构造方式没有可比性,灵活使用即可

构造函数原型

  • 创建一个空字符串
string();   
  • 使用字符串s初始化
string(const char* s);  
  • 使用一个string对象初始化另一个string对象
string(const string& str);
  • 使用n个字符c初始化
string(int n, char c); 

代码示例

#include <iostream>
#include <string>using namespace std;// string构造函数
void test() {string s;cout << s << endl;string s1("aaaa");cout << s1 << endl;string s2(s1);cout << s2 << endl;string s3(5, 's');cout << s3 << endl;
}int main() {test();return 0;
}

运行结果

在这里插入图片描述

string的赋值操作

string赋值的函数原型

  • char*类型字符串赋值给当前的字符串
string& operator=(const char* s); 
  • 把字符串s赋值给当前的字符串
string& operator=(const string &s); 
  • 字符赋值给当前的字符串
string& operator=(char c); 
  • 把字符串s赋值给当前的字符串
string& assign(const char* s); 
  • 把字符串s的前n个字符赋值给当前的字符串
string& assign(const char* s, int n);  
  • 把字符串s赋值给当前的字符串
string& assign(const string &s); 
  • 用n个字符c赋值给当前的字符串
string& assign(int n, char c);  

代码示例

#include <iostream>
#include <string>using namespace std;void test() {string s;s = "asdfghjk";cout << "s: " << s << endl;string s1;s1 = s;cout << "s1: " << s1 << endl;string s2;s2 = 'a';cout << "s2: " << s2 << endl;string s3;s3.assign("asdfghjk");cout << "s3: " << s3 << endl;string s4;s4.assign("asdfghjk", 3);cout << "s4: " << s4 << endl;string s5;s5.assign(s3);cout << "s5: " << s5 << endl;string s6;s6.assign(5, 'f');cout << "s6: " << s6 << endl;
}int main() {test();return 0;
}

结果示例

在这里插入图片描述

string字符串拼接

  • 实现在字符串末尾拼接字符串

函数原型

在这里插入图片描述

代码示例

#include <iostream>
#include <string>using namespace std;void test() {string str1 = "I";str1 += " am Tom.";cout << str1 << endl;str1 += " you are pig";cout << str1 << endl;str1 += str1;cout << str1 << endl;str1.append("xxx");cout << str1 << endl;string str2 = "hhhh";string str3 = "123456789";// 主叫从第i个字符之后的字符str2.append(str3, 5);cout << str2 << endl;// 主叫从第i个字符之后的n个 字符str2.append(str3, 5, 2);cout << str2 << endl;
}int main() {test();return 0;
}

运行结果

在这里插入图片描述

string查找和替换

功能描述

  • 查找:查找指定字符串是否存在
  • 替换:在指定位置替换字符串

函数原型

在这里插入图片描述

字符串查找

  • find和rfind区别

    • find 从左往右找

    • rfind 从右往左找

  • 找到返回对应位置的下标,没找到返回-1

  • 代码示例

#include <iostream>
#include <string>using namespace std;void test() {string str1 = "abcdefghdeaa";// find 从左往右找int pos = str1.find("de");if (pos == -1) {cout << "未找到" << endl;} else {cout << "找到了,pos = " << pos << endl;}// rfind 从右往左找pos = str1.rfind("de");if (pos == -1) {cout << "未找到" << endl;} else {cout << "找到了,pos = " << pos << endl;}
}int main() {test();return 0;
}
  • 结果

在这里插入图片描述

字符串替换

  • replace在替换时,要制定从那个位置开始,多少个字符,替换的字符串
#include <iostream>
#include <string>using namespace std;void test() {string str = "asdfghjkl";str.replace(1, 4, "111111");cout << str << endl;
}int main() {test();return 0;
}
  • 结果

在这里插入图片描述

字符串比较

  • compare 内部实现:逐字符一个一个按ascii码比较

代码示例

#include <iostream>
#include <string>using namespace std;void test() {string str1 = "hello";string str2 = "hello";string str3 = "hello1";// compare 内部实现:逐字符一个一个按ascii码比较if (str1.compare(str2) == 0) {cout << "str1 == str2" << endl;} else if (str1.compare(str2) > 0) {cout << "str1 > str2" << endl;} else {cout << "str1 < str2" << endl;}
}int main() {test();return 0;
}

string字符存取

存取方式

  • 通过[]方式取字符:char& operator[](int n);

  • 通过at方法获取字符:char& at(int n);

代码示例

#include <iostream>
#include <string>using namespace std;void test() {string str = "sdsafs";cout << "通过[]方式访问:";for (int i = 0; i < str.size(); i++) {cout << str[i] << " ";}cout << endl;cout << "通过at方式访问:";for (int i = 0; i < str.size(); i++) {cout << str.at(i) << " ";}cout << endl;str[1] = 'a';cout << "通过[]方式访问修改后的字符:";for (int i = 0; i < str.size(); i++) {cout << str[i] << " ";}cout << endl;str.at(2) = 'c';cout << "通过at方式访问:";for (int i = 0; i < str.size(); i++) {cout << str.at(i) << " ";}cout << endl;
}int main() {test();return 0;
}

运行结果

在这里插入图片描述

string插入和删除

  • 对string字符串进行插入和删除字符操作

函数原型

在这里插入图片描述

代码示例

#include <iostream>
#include <string>using namespace std;void test() {string str = "hello";// insert 在指定位置插入指定字符串str.insert(1, "12345");cout << str << endl;// delete 删除指定位置开始的n个字符str.erase(1, 5);cout << str << endl;
}int main() {test();return 0;
}

运行结果

在这里插入图片描述

string字符串

  • 从字符串中获取想要的字符串

函数原型

  • 返回由pos开始的n个字符组成的字符串:string substr(int pos=0,int n=npos) const;

代码示例

#include <iostream>
#include <string>using namespace std;void test() {string str = "hello";string sub = str.substr(1, 3);cout << sub << endl;
}int main() {test();return 0;
}

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

如若内容造成侵权/违法违规/事实不符,请联系编程老四网进行投诉反馈email:xxxxxxxx@qq.com,一经查实,立即删除!

相关文章

小程序--事件处理

一、事件对象 给小程序的事件传递参数&#xff0c;有以下两种方法&#xff1a; 1、自定义属性 <view class"item" wx:for"{{ 5 }}" wx:key"*this" data-index"{{index}}" bind:tap"onClick"></view> Page({o…

C#安装CommunityToolkit.Mvvm依赖

这里需要有一定C#基础&#xff0c; 首先找到右边的解决方案&#xff0c;右键依赖项 然后选择nuget管理 这里给大家扩展一下nuget的国内源&#xff08;https://nuget.cdn.azure.cn/v3/index.json&#xff09; 然后搜自己想要的依赖性&#xff0c;比如CommunityToolkit.Mvvm 再点…

vue中实现拖拽排序功能

npm i vuedraggable <template><div class"app-container"><!-- <div :class"canEdit ? dargBtn-lock el-icon-unlock : dargBtn-lock el-icon-lock" click"removeEvent()">{{ canEdit ? 调整 : 锁定 }}</div> --&…

Linux--自定义shell

shell shell就是操作系统提供给用户与操作系统进行交互的命令行界面。它可以理解为一个用户与操作系统之间的接口&#xff0c;用户可以通过输入命令来执行各种操作&#xff0c;如文件管理、进程控制、软件安装等。Shell还可以通过脚本编程实现自动化任务。 常见的Unix系统中使…

如何修改unity的背景颜色

要在Unity中将背景颜色设为黑色&#xff0c;可以按照以下步骤进行&#xff1a; 1、在Unity编辑器中&#xff0c;选择你想要修改背景颜色的摄像机对象&#xff08;一般是Main Camera&#xff09;。 2、在Inspector面板中&#xff0c;找到"Clear Flags"&#xff08;清…

后端经典面试题合集

目录 1. Java基础1-1. JDK 和 JRE 和 JVM 分别是什么&#xff0c;有什么区别&#xff1f;1-2. 什么是字节码&#xff1f;采用字节码的最大好处是什么&#xff1f;1-3. JDK 动态代理和 CGLIB 动态代理的区别是什么&#xff1f; 1. Java基础 1-1. JDK 和 JRE 和 JVM 分别是什么&…

使用 yarn 的时候,遇到 Error [ERR_REQUIRE_ESM]: require() of ES Module 怎么解决?

晚上回到家&#xff0c;我打开自己的项目&#xff0c;执行&#xff1a; cd HexoPress git pull --rebase yarn install yarn dev拉取在公司 push 的代码&#xff0c;然后更新依赖&#xff0c;最后开始今晚的开发时候&#xff0c;意外发生了&#xff0c;竟然报错了&#xff0c;…

【linux】查看openssl程序的安装情况

【linux】查看openssl程序的安装情况 1、查看安装包信息 $ rpm -qa |grep openssl 2、安装路径 $ rpm -ql openssl $ rpm -ql openssl-libs $ rpm -ql openssl-devel 3、相关文件和目录 /usr/bin/openssl /usr/include/openssl /usr/lib64/libssl.so.* /usr/lib64/libcrypto…

[golang] 24 fmt.Errorf(), error.Is() 和 error.As()

文章目录 一、默认的 error 实现二、自定义 error 实现2.1 通过 fmt.Errorf(%w) 创建嵌套 error2.1.1 fmt.Errorf() 实现 2.2 errors.Is()2.2.1 使用2.2.2 实现 2.3 errors.As()2.3.1 使用2.3.2 实现 一、默认的 error 实现 首先&#xff0c;go 定义了 error interface&#x…

跨区互联组网怎么做?SD-WAN专线可以实现吗?

在当今数字化时代&#xff0c;企业不断扩张&#xff0c;跨区域互联成为业务发展的必然需求。然而&#xff0c;跨区互联组网涉及到复杂的网络架构和连接&#xff0c;传统的网络方案往往难以满足高性能、高可靠性和低成本的要求。SD-WAN专线技术的出现&#xff0c;为跨区互联组网…

IO线程进程作业day6

1> 将标准io文件IO的内容复习一遍 2> 进程线程的相关函数复习一遍 3> 将信号和消息队列的课堂代码敲一遍 1、处理普通信号 #include <myhead.h> //定义信号处理函数 void handler(int signo) {if(signoSIGINT){puts("按下ctrlc");} } int main(in…

chatGPT 使用随想

一年前 chatGPT 刚出的时候&#xff0c;我就火速注册试用了。 因为自己就是 AI 行业的&#xff0c;所以想看看国际上最牛的 AI 到底发展到什么程度了. 自从一年前 chatGPT 火出圈之后&#xff0c;国际上的 AI 就一直被 OpenAI 这家公司引领潮流&#xff0c;一直到现在&#x…

创建型设计模式 - 原型设计模式 - JAVA

原型设计模式 一 .简介二. 案例三. 补充知识 前言 这是我在这个网站整理的笔记,有错误的地方请指出&#xff0c;关注我&#xff0c;接下来还会持续更新。 作者&#xff1a;神的孩子都在歌唱 一 .简介 原型模式提供了一种机制&#xff0c;可以将原始对象复制到新对象&#xff0…

突出最强算法模型——回归算法 !!

文章目录 1、特征工程的重要性 2、缺失值和异常值的处理 &#xff08;1&#xff09;处理缺失值 &#xff08;2&#xff09;处理异常值 3、回归模型的诊断 &#xff08;1&#xff09;残差分析 &#xff08;2&#xff09;检查回归假设 &#xff08;3&#xff09;Cooks 距离 4、学…

如何准确查询自己的大数据信用报告?

在当今数字化时代&#xff0c;大数据信用报告在个人信用评估中扮演着越来越重要的角色。然而&#xff0c;很多人可能不知道如何查询自己的大数据信用报告。本文贷大家一起了解一下&#xff0c;希望对你有帮助。 如何准确查询自己的大数据信用报告&#xff1a; 一、找到可靠的查…

学习Redis基础篇

1.初识Redis 1.认识NoSQL 2.认识Redis 3.连接redis命令 4.数据结构的介绍 5.通用命令 2.数据类型 1.String类型 常见命令&#xff1a;例子&#xff1a;set key value

(提供数据集下载)基于大语言模型LangChain与ChatGLM3-6B本地知识库调优:数据集优化、参数调整、Prompt提示词优化实战

文章目录 &#xff08;提供数据集下载&#xff09;基于大语言模型LangChain与ChatGLM3-6B本地知识库调优&#xff1a;数据集优化、参数调整、提示词Prompt优化本地知识库目标操作步骤问答测试的预设问题原始数据情况数据集优化&#xff1a;预处理&#xff0c;先后准备了三份数据…

【AI数字人-论文】RAD-NeRF论文

文章目录 前言模型框架动态的NeRF前处理头部模型音频特征眼部控制头部总体表示 躯干模型loss 结果参考 【AI数字人-论文】AD-NeRF论文 前言 本篇论文有三个主要贡献点&#xff1a; 提出一种分解的音频空间编码模块&#xff0c;该模块使用两个低维特征网格有效地建模固有高维音…

ChatGPT调教指南 | 咒语指南 | Prompts提示词教程(一)

在我们开始探索人工智能的世界时&#xff0c;了解如何与之有效沉浸交流是至关重要的。想象一下&#xff0c;你手中有一把钥匙&#xff0c;可以解锁与OpenAI的GPT模型沟通的无限可能。这把钥匙就是——正确的提示词&#xff08;prompts&#xff09;。无论你是AI领域的新手&#…

Linux线程同步(3)生产者与消费者、条件变量与信号量

1.生产者与消费者的概念 生产者与消费者&#xff08;Producer-Consumer&#xff09;问题&#xff0c;是一个经典的并发编程问题。在这个问题中&#xff0c;涉及到两类进程&#xff1a;生产者和消费者。生产者负责生产数据&#xff0c;并将其放入一个缓冲区中&#xff1b;消费者…
推荐文章