【STL】string类 (下)

目录

1,insert

2,erase

3,find

4,replace

5,rfind

6,substr

7,find_first_of

8,find_first_not_of

9,find_last_of

10,operator+

11,getline


1,insert

在 pos 位置之前插入字符串

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1("hello world");
	s1.insert(0, "xx");
	cout << s1 << endl;

	s1.insert(0,2,'y');
	cout << s1 << endl;

	s1.insert(s1.begin(),'z');
	cout << s1 << endl;

	string s2("iiiiiiiiii");
	s1.insert(0,s2, 5);
	cout << s1 << endl;
	return 0;
}

2,erase

擦除范围字符串

int main()
{
	string s1("hello world");
	s1.erase(5, 4);
	cout << s1 << endl;

	s1.erase(1);
	cout << s1 << endl;

	return 0;
}

3,find

int main()
{
	string s1("hello world");

	size_t pos = s1.find('l',0);
	cout << s1[pos] << endl;

	pos = s1.find("ll", 1);
	cout << pos << endl;

	return 0;
}

4,replace

从 pos 位置开始,用 n 个字符替换;

int main()
{
	string s1("hello world");

	s1.replace(0, 2, "xx");
	cout << s1 << endl;

	s1.replace(0, 5,"yyy");
	cout << s1 << endl;

	return 0;
}

上述可以看到,第一个替换从下标 0 开始用两个字符也就是 ” he “ 替换 “ xx ” ;

第二个替换是从下标 0 开始用5个字符也就是 “ hello ” 替换 “ yyy ”;

给大家写一个替换字符的题目

将字符串中的空格都替换成其他字符串

int main()
{
	string s1("hello world hello bit");

	size_t pos = s1.find(" ", 0);
	while (pos != string::npos)
	{
		s1.replace(pos, 1, "%20");
		pos = s1.find(" ", pos + 3);
	}
	cout << s1 << endl;
	
	return 0;
}

查找字符然后进行替换,然后在查找再替换直到查找不到为止退出;

5,rfind

从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置

int main()
{
	string s1("hello world");

	size_t pos = s1.rfind('l',3);
	cout << pos << endl;

	pos = s1.rfind('l', 10);
	cout << pos << endl;

	pos = s1.rfind('o');
	cout << pos << endl;

	pos = s1.find("l");
	cout << pos << endl;
	pos = s1.rfind("l");
	cout << pos << endl;

	return 0;
}

6,substr

在 str 中从 pos 位置开始,截取 n 个字符,然后将其返回

int main()
{
	string s1("hello world");

	string s2=s1.substr(2, 3);
	cout << s2 << endl;

	string s3 = s1.substr(0);
	cout << s3 << endl;

	return 0;
}

我们再写一个查找后缀的程序;

int main()
{
	string s1("test.cpp");
	string s2("code.jbp");

	size_t pos = s1.rfind('.');
	if(pos != string::npos)
	{
		string buff = s1.substr(pos);
		cout << buff << endl;
	}

	pos = s2.rfind('.');
	if(pos != string::npos)
	{
		string buff = s2.substr(pos);
		cout << buff << endl;
	}

	return 0;
}

我们再写一个分离字符串的小程序

int main()
{
	string str("https://legacy.cplusplus.com/reference/string/string/substr/");
	size_t pos = str.find(':');
	string buff = str.substr(0, pos+1);
	cout << buff << endl;

	size_t pos1 = str.find('/',pos+3);
	buff = str.substr(pos + 1, pos1-pos);
	cout << buff << endl;

	size_t pos2 = str.rfind('/');
	buff = str.substr(pos1 + 1, pos2-pos1);
	cout << buff << endl;

	return 0;
}

7,find_first_of

直接看代码兄弟们

int main()
{
	string str("Please, replace the vowels in this sentence by asterisks.");
	size_t pos = str.find_first_of("abc");
	while (pos != string::npos)
	{
		str.replace(pos, 1,"*");
		pos = str.find_first_of("abc",pos);
	}
	cout << str << endl;

	return 0;
}

8,find_first_not_of

与 find_first_of 功能相反,返回不属于字符串的下标

int main()
{
	string str("Please, replace the vowels in this sentence by asterisks.");
	size_t pos = str.find_first_not_of("abc");
	while (pos != string::npos)
	{
		str.replace(pos, 1,"*");
		pos = str.find_first_not_of("abc",pos+1);
	}
	cout << str << endl;

	return 0;
}

9,find_last_of

跟 find_first_of 类似,只不过 find_last_of 是从后往前找的;

来个例子:

void SplitFilename(const std::string& str)
{
	std::cout << "Splitting: " << str << '\n';
	std::size_t found = str.find_last_of("/\\");
	std::cout << " path: " << str.substr(0, found) << '\n';
	std::cout << " file: " << str.substr(found + 1) << '\n';
}

int main()
{
	std::string str1("/usr/bin/man");
	std::string str2("c:\\windows\\winhelp.exe");

	SplitFilename(str1);
	SplitFilename(str2);

	return 0;
}

10,operator+

int main()
{
	string s1("hello world");
	string s2("abcdefg");

	string s3 = s1 + s2;
	cout << s3 << endl;

	s1 = s2 + "666";
	cout << s1 << endl;

	s2 = "999" + s2;
	cout << s2 << endl;
	return 0;
}

11,getline

我们正常的输入是使用 cin

int main()
{
	string s1;
	//我们输入 hello world
	cin >> s1;
	cout << s1 << endl;
	
	return 0;
}

但是 cin 遇到空格就会停下来,所以我们引入了 getline ;

int main()
{
	string s1;
	getline(cin, s1);
	cout << s1 << endl;
	cout << endl;

	//我们输入 hello world
	cin >> s1;
	cout << s1 << endl;
	
	return 0;
}

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Qt TCP网络上位机的设计(通过网络编程与下位机结合)

目录 TCP 协议基础 QTcpServer 和 QAbstractSocket 主要接口函数 TCP 应用程序 1.服务端 2.客户端 上位机通过网络编程与下位机实现通信 TCP 协议基础 传输控制协议&#xff08;TCP&#xff0c;Transmission Control Protocol&#xff09;是一种面向连接的、可靠的、基于…

Camtasia Studio2024专业的屏幕录制和视频剪辑软件

Camtasia2024专业的屏幕录制和视频剪辑软件3000多万专业人士在全球范围内使用Camtasia展示产品&#xff0c;教授课程&#xff0c;培训他人&#xff0c;以更快的速度和更吸引人的方式进行沟通和屏幕分享。使您在Windows和Mac上进行录屏和剪辑创作专业外观的视频变得更为简单。 …

BGP选路实验

要求 1 使用PreVal策略&#xff0c;确保R4通过R2到达192.168.10.0/24 2 使用AS_Path策略&#xff0c;确保R4通过R3到达192.168.11.0/24 3 配置MED策略&#xff0c;确保R4通过R3到达192.168.12.0/24 4 使用Local Preference策略&#xff0c;确保R1通过R2到达192.168.1.0/24 5 使…

【古诗生成AI实战】之五——加载模型进行古诗生成

回顾上一篇博客&#xff0c;我们已经成功地训练了我们的模型&#xff0c;并将其保存下来。这是一个重要的里程碑&#xff0c;因为训练好的模型是我们进行文本生成的基础。 现在&#xff0c;接下来的步骤是加载这个训练好的模型&#xff0c;然后使用它来生成古诗。 本章的内容属…

打印菱形-第11届蓝桥杯选拔赛Python真题精选

[导读]&#xff1a;超平老师的Scratch蓝桥杯真题解读系列在推出之后&#xff0c;受到了广大老师和家长的好评&#xff0c;非常感谢各位的认可和厚爱。作为回馈&#xff0c;超平老师计划推出《Python蓝桥杯真题解析100讲》&#xff0c;这是解读系列的第9讲。 打印菱形&#xff…

Android 虚拟机与类加载机制

1、Dalvik 虚拟机 Android 应用程序运行在 Dalvik/Art 虚拟机上&#xff0c;并且每一个应用程序都有一个单独的 Dalvik/Art 虚拟机实例。 1.1 JVM 与 Dalvik Dalvik 虚拟机也算是一个 Java 虚拟机&#xff0c;它是按照 JVM 虚拟机规范实现的&#xff0c;二者的特性差不多&am…

爬楼梯(力扣LeetCode)动态规划

爬楼梯 题目描述 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢&#xff1f; 示例 1&#xff1a; 输入&#xff1a;n 2 输出&#xff1a;2 解释&#xff1a;有两种方法可以爬到楼顶。 1 阶 1 阶2 阶 示…

树状数组专题

折叠 区间修改&#xff0c;区间查询&#xff0c;这一类题通常都可以使用线段树解决&#xff0c;但对于此题&#xff0c;树状数组同样可以&#xff0c;而且常数较小&#xff0c;代码简单。 思路&#xff1a; 考虑使用树状数组去维护差分数组&#xff0c;即对于 a i a_i ai​,我们…

找不到vcomp120.dll该如何修复?vcomp120.dll丢失的5个可行解决方法

本文将对vcomp120.dll文件的丢失原因进行详细分析&#xff0c;并提供五个有效的修复方法。同时&#xff0c;本文还将深入介绍vcomp120.dll文件的作用及其在程序运行中的重要性。 一、vcomp120.dll文件丢失原因 操作系统损坏&#xff1a;由于病毒感染、系统错误等原因&#xf…

linux复习笔记04(小滴课堂)

软件安装rpm方式介绍&#xff1a; 先去挂载光盘&#xff1a; 要确保这是已连接状态。 我们查看到已经挂载成功了。 进到这个目录下。 我们可以看到这有很多rpm软件包。 man rpm: 可以看到很多参数&#xff0c;但是我们不需要全部掌握。 举例&#xff1a; 这就是告诉我们需要安…

docker (简介、dcoker详细安装步骤)- day01

一、 为什么出现 Docker是基于Go语言实现的云开源项目。 Docker的主要目标是“Build&#xff0c;Ship and Run Any App,Anywhere”&#xff0c;也就是通过对应用组件的封装、分发、部署、运行等生命周期的管理&#xff0c;使用户的APP&#xff08;可以是一个WEB应用或数据库应…

WiFi的CSMA/CA竞争窗口流程简述

1、若站点最初有数据要发送&#xff08;不是发送不成功再进行重传的那种&#xff09;&#xff0c;且检测到信道空闲&#xff0c;在等待DIFS后&#xff0c;就发送整个数据帧。 2、否则&#xff0c;站点执行退避算法。一旦检测到信道忙&#xff0c;就冻结退避计时器。只要信道空…

深度学习之循环神经网络

视频链接&#xff1a;6 循环神经网络_哔哩哔哩_bilibili 给神经网络增加记忆能力 对全连接层而言&#xff0c;输入输出的维数固定&#xff0c;因此无法处理序列信息 对卷积层而言&#xff0c;因为卷积核的参数是共享的&#xff0c;所以卷积操作与序列的长度无关。但是因为卷积…

北塞浦路斯土耳其共和国关于成立欧洲数字股票交易所企业交流会

在地中海的温暖波涛中&#xff0c;北塞浦路斯土耳其共和国这个古老而充满活力的国家正成为全球关注的焦点。2023年11月22日至11月24日&#xff0c;为期三天的北塞浦路斯土耳其共和国关于成立欧洲数字股票交易所企业交流会隆重谢幕&#xff0c;北塞副总统&#xff0c;经济部长&a…

【学习记录】从0开始的Linux学习之旅——驱动模块编译与加载

一、概述 Linux操作系统通常是基于Linux内核&#xff0c;并结合GNU项目中的工具和应用程序而成。Linux操作系统支持多用户、多任务和多线程&#xff0c;具有强大的网络功能和良好的兼容性。本文主要讲述如何编译及加载linux驱动模块。 二、概念及原理 应用程序通过系统调用与内…

STK Components 二次开发-创建地面站

1.地面站只需要知道地面站的经纬高。 // Define the location of the facility using cartographic coordinates.var location new Cartographic(Trig.DegreesToRadians(-75.596766667), Trig.DegreesToRadians(40.0388333333), 0.0); 2.创建地面站 创建方式和卫星一样生成对…

【开源】基于Vue+SpringBoot的食品生产管理系统

项目编号&#xff1a; S 044 &#xff0c;文末获取源码。 \color{red}{项目编号&#xff1a;S044&#xff0c;文末获取源码。} 项目编号&#xff1a;S044&#xff0c;文末获取源码。 目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块2.1 加工厂管理模块2.2 客户管理模块2.3…

UE5人物残影学习(材质实现)

学习视频 UE4简单的材质球残影人教学&#xff0c;你学会了吗&#xff01;_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1rY411q7Yb/?spm_id_from333.788.top_right_bar_window_history.content.click 结果预览 1.创建残值&#xff0c;混合模式勾选半透明 “混合模…

Qt4利用MVC开发曲线数据编辑器

目录 1 需求 2 开发流程 1 搭建框架 2 构造函数 3 打开工程 4 实现应用程序参数加载 5 QCustomPlot和TableView的联动 6 数据的可视化修改 7 列表点击事件事先键盘控制 8 表格实现复制&#xff0c;粘贴&#xff0c;删除等一系列功能 9 曲线实现自适应范围和统一范围…

MyBatis插入操作返回主键报错问题记录

一开始用直接传参数的方法写的插入操作 StudentMapper.java接口 Integer insertStudent(Param("sname") String name,Param("sage") int age); 然后在网上搜了返回主键的方法 StudentMapper.xml: <insert id"insertStudent" useGenerat…
最新文章