C++——stl,string容器详解

目录

1.string是什么

2.string的构造

代码示例:

3.string的赋值操作

代码示例:

4.string字符串拼接

​编辑 代码示例:

5.string查找和替换

代码示例: 

6.字符串比较

代码示例: 

7.string字符存取

代码示例: 

8.string插入和删除

代码示例: 

8.string字串获取 

 代码示例:


1.string是什么

 

2.string的构造

 

代码示例:

#include<bits/stdc++.h>
using namespace std;int main()
{string s1;const char* str = "hello!";string s2(str);cout << s2 << endl;string s3(s2);cout << s3 << endl;string s4(5,'a');cout << s4;return 0;
}

 

3.string的赋值操作

 

代码示例:

#include<bits/stdc++.h>
using namespace std;int main()
{string s1;s1 = "hello!";cout << s1 << endl;string s2;s2 = s1;cout << s2 << endl;string s3;s3 = 'a';cout << s3 << endl;string s4;s4.assign("hello!!!!!");cout << s4 << endl;string s5;s5.assign("mygo!!!!!",5);cout << s5 << endl;string s6;s6.assign(s5);cout << s6 << endl;string s7;s7.assign(5,'a');cout << s7 << endl;return 0;
}

 

4.string字符串拼接

 代码示例:

#include<bits/stdc++.h>
using namespace std;int main()
{string s1 = "my";s1 += "go!!!!";cout << s1 << endl;s1 += '!';cout << s1 << endl;string s2 = " ave";s1 += s2;cout << s1 << endl;s1.append(" mujica");cout << s1 << endl;s2.append("mujica",3);cout << s2 << endl;s2.append(s1);cout << s2 << endl;string s3 = "hello";s3.append(s1,2,7);cout << s3 << endl;return 0;
}

 

5.string查找和替换

 

代码示例: 

#include<bits/stdc++.h>
using namespace std;int main()
{string s = "starrail";//查找int pos1 = s.find("r");cout << pos1 << endl;int pos2 = s.find("zenlesszonezero");cout << pos2 << endl;//不存在会输出-1int pos4 = s.rfind("r");//rfind从右往左查,但也是从左往右计算下标位置cout << pos4 << endl;//替换//下标4开始替换3个字符//或者说删掉下标4开始的3个字符,并且将github放在那个空位,其它的不变s.replace(4,3,"github");cout << s << endl;return 0;
}

 

6.字符串比较

 

代码示例: 

#include<bits/stdc++.h>
using namespace std;int main()
{string s1 = "starrail";string s2 = "black myth";//按照ascii码一个字母一个字母的比较//s在b的后面,更大cout << s1.compare(s2);return 0;
}

 

7.string字符存取

 

代码示例: 

#include<bits/stdc++.h>
using namespace std;int main()
{string s1 = "starrail";for(int i = 0; i < s1.size(); i++){cout << s1[i] << ' ';}cout << endl;for(int i = 0; i < s1.size(); i++){cout << s1.at(i) << ' ';}return 0;
}

8.string插入和删除

 

代码示例: 

#include<bits/stdc++.h>
using namespace std;int main()
{string s1 = "starrail";//插入//在2下标的元素的左边插入s1.insert(2," hello ");cout << s1 << endl;s1.erase(2,7);//从第2个位置起删7个cout << s1 << endl;return 0;
}

 

 

8.string字串获取 

 代码示例:

#include<bits/stdc++.h>
using namespace std;int main()
{string s1 = "starrail";string s2 = s1.substr(1,6);cout << s2 << endl;return 0;
}

 

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

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

相关文章

springboot与Elasticsearch版本兼容对比

首先 大家在下载 Elasticsearch 时 最好先弄清楚版本 因为 如果 Spring Boot 版本 不兼容 Elasticsearch 那就是到头一场空了 Elasticsearch 版本 6.x 可以兼容 Spring Boot 2.x Elasticsearch 版本 7.x 可以兼容 Spring Boot 2.x 3.x 4x Elasticsearch 版本 7.x 以及 8.x 可以…

车位检测,YOLOV8,OPENCV调用

车位检测YOLOV8NANO,opencv调用 车位检测&#xff0c;YOLOV8NANO&#xff0c;训练得到PT模型&#xff0c;然后转换成ONNX&#xff0c;OPENCV的DNN调用&#xff0c;支持C,PYTHON,ANDROID

CodeFuse-VLM 开源,支持多模态多任务预训练/微调

CodeFuse-MFT-VLM 项目地址&#xff1a;https://github.com/codefuse-ai/CodeFuse-MFT-VLM CodeFuse-VLM-14B 模型地址&#xff1a;CodeFuse-VLM-14B CodeFuse-VLM框架简介 随着huggingface开源社区的不断更新&#xff0c;会有更多的vision encoder 和 LLM 底座发布&#x…

记一次VulnStack渗透

信息收集 netdiscover的主机发现部分不再详解&#xff0c;通过访问端口得知20001-2003端口都为web端口&#xff0c;所以优先考虑从此方向下手 外网渗透 GetShell Struct漏洞 访问2001端口后&#xff0c;插件Wappalyzer爬取得知这是一个基于Struct的web站点&#xff0c;直接…

基于深度置信网络的多模态过程故障评估方法及应用

源自&#xff1a;自动化学报 作者&#xff1a;张凯, 杨朋澄, 彭开香, 陈志文 “人工智能技术与咨询” 发布 摘 要 传统的多模态过程故障等级评估方法对模态之间的共性特征考虑较少, 导致当被评估模态故障信息不充分时, 评估的准确性较低. 针对此问题, 首先, 提出一种共性–…

最新模型VMamba:颠覆视觉Transformer,下一代主流Backbone?

论文标题&#xff1a; VMamba: Visual State Space Model 论文作者&#xff1a; Yue Liu, Yunjie Tian, Yuzhong Zhao, Hongtian Yu, Lingxi Xie, Yaowei Wang, Qixiang Ye, Yunfan Liu 1. 摘要 卷积神经网络&#xff08;CNN&#xff09;与视觉Transformer&#xff08;ViT…

【数据结构与算法】(11)基础数据结构 之 二叉树 二叉树的存储与遍历及相关示例 详细代码讲解

目录 2.10 二叉树1) 存储2) 遍历广度优先深度优先递归实现非递归实现 习题E01. 前序遍历二叉树-Leetcode 144E02. 中序遍历二叉树-Leetcode 94E03. 后序遍历二叉树-Leetcode 145E04. 对称二叉树-Leetcode 101E05. 二叉树最大深度-Leetcode 104E06. 二叉树最小深度-Leetcode 111…

Django部署到服务器后无法获取到静态元素 The requested resource was not found on this server

问题描述 写了一个Django项目&#xff0c;部署到云主机后&#xff0c;访问发现图片无法访问&#xff0c;报错The requested resource was not found on this server 图片是一个词云图&#xff0c;根据爬虫爬取的信息生成的&#xff0c;根据爬取的信息会改变&#xff0c;所以没…

宠物空气净化器是宠物家庭必备?宠物空气净化器改善空气质量吗?

作为一个长期养猫的猫奴&#xff0c;我们都深刻体会到猫毛问题的困扰。然而&#xff0c;我们需要明确一点&#xff0c;肉眼可见的猫毛可以通过扫把或拖把清理掉。真正令人担忧的是那些肉眼不可见的浮毛、粉尘和微小颗粒&#xff0c;它们在无形中对人和宠物的健康环境造成影响。…

车载测试中:如何处理 bug

一&#xff1a;Jira 提交 bug 包含那些内容 二&#xff1a;如何处理现上 bug 三&#xff1a;车载相关的 bug 如何定位 四&#xff1a;遇到 bug &#xff0c;复现不出来怎么办 五&#xff1a;bug 的处理流程 一&#xff1a;Jira 提交 bug 包含那些内容二&#xff1a;如何处理现上…

【DDD】学习笔记-数据模型与对象模型

在建立数据设计模型时&#xff0c;我们需要注意表设计与类设计之间的差别&#xff0c;这事实上是数据模型与对象模型之间的差别。 数据模型与对象模型 我们首先来分析在设计时对冗余的考虑。前面在讲解数据分析模型时就提及&#xff0c;在确定数据项模型时&#xff0c;需要遵…

【C++搜索二叉树】

文章目录 二叉搜索树二叉搜索树的概念二叉搜索树的操作搜索二叉树的具体实现 二叉搜索树 二叉搜索树的概念 二叉搜索树又称二叉排序树&#xff0c;它或者是一棵空树&#xff0c;或者是具有以下性质的二叉树: 若它的左子树不为空&#xff0c;则左子树上所有节点的值都小于根节点…