【STL】string类 (下)

这篇具有很好参考价值的文章主要介绍了【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


【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

1,insert

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

在 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;
}

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

2,erase

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

擦除范围字符串

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

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

	return 0;
}

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

3,find

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

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;
}

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

4,replace

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

从 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;
}

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

上述可以看到,第一个替换从下标 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;
}

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

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

5,rfind

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

从字符串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;
}

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

6,substr

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

在 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;
}

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

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

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;
}

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

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

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;
}

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

7,find_first_of

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

直接看代码兄弟们

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;
}

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

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;
}

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

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;
}

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

10,operator+

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

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;
}

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

11,getline

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

我们正常的输入是使用 cin

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

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

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

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

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

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端

【STL】string类 (下),C++,c++,算法,开发语言,运维,服务器,后端文章来源地址https://www.toymoban.com/news/detail-754431.html

到了这里,关于【STL】string类 (下)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 阿里云——云服务器基础运维与管理

    作者简介:一名云计算网络运维人员、每天分享网络与运维的技术与干货。   座右铭:低头赶路,敬事如仪 个人主页: 网络豆的主页​​​​​ 目录 写在前面 学习目标: 一.3个理由拥抱云服务器 1.什么是云服务器  2.使用云服务的好处 3.推荐云服务的理由 二.1分钟快速定

    2024年02月16日
    浏览(40)
  • shell脚本——服务器巡检(自动化运维)

     目的   自动 获取集群内 多个主机 的内存、磁盘、cpu等信息 生成日志  准备    VMware虚拟主机IP在同一个网段(互相能ping通)             虚拟主机都有公钥免登录            修改主机IP  vi/etc/sysconfig/netwoek-scripts/ifcfg-ens160            设置主机名 hostnamectl set-ho

    2024年02月15日
    浏览(38)
  • 【运维】Linux 跨服务器复制文件文件夹

    如果是云服务 建议用内网ip scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的。可能会稍微影响一下速度。当你服务器硬盘变为只读 read only system时,用scp可以帮你把文件移出来

    2024年02月08日
    浏览(46)
  • 远程管理服务器 用户组创建 1(运维笔记)

    修改跳板机名称: 修改跳板机网络模式为仅主机模式,ssh连接 假设有三个开发人员: 为三个开发人员设置密码: 创建相应的目录,给开发人员使用: 查看开发人员属组: 创建一个组: 添加用户附加组 查看组信息 赋予权限,更改目录权限: 冒险位(setuid):4000针对一些命令,临

    2023年04月13日
    浏览(37)
  • 【Linux 服务器运维】定时任务 crontab 详解 | 文末送书

    本文思维导图概述的主要内容: 1.1 什么是 crontab Crontab 是一个在 Unix 和 Linux 操作系统上 用于定时执行任务 的工具。它允许用户创建和管理计划任务,以便在特定的时间间隔或时间点自动运行命令或脚本。Crontab 是 cron table 的缩写, cron 指的是 Unix 系统中的一个后台进程,它

    2024年02月08日
    浏览(62)
  • 基于SSM的服务器运维管理的论坛系统+98166(免费领源码+开发文档)可做计算机毕业设计JAVA、PHP、爬虫、APP、小程序、C#、C++、python、数据可视化、大数据、全套文案

    随着科学技术的飞速发展,社会的方方面面、各行各业都在努力与现代的先进技术接轨,通过科技手段来提高自身的优势,服务器运维管理的论坛系统当然也不能排除在外。服务器运维管理的论坛系统是以实际运用为开发背景,运用软件工程原理和开发方法,采用 SSM技术构

    2024年01月16日
    浏览(47)
  • 【Linux运维】shell脚本检查服务器内存和CPU利用率

    在管理服务器时候写了一个 shell脚本,在服务上实现每天凌晨3点查系统的指定文件夹下的容量大小,如果超过10G就要删除3天前的内容,还要时刻查询内存和cpu利用率,如果超过80%就要提示用户出现过载 将以上代码保存为一个.sh文件,然后通过crontab在每天凌晨3点运行即可:

    2024年02月09日
    浏览(48)
  • 哪吒监控:开源、轻量、易用的服务器监控、运维工具(内附主题美化代码)

    哪吒监控是一款开源、轻量、易用的服务器监控、运维工具,为用户提供了一系列强大的功能和便捷的操作方式。 一键安装:支持一键脚本安装面板和监控服务,适用于Linux、Windows、MacOS、OpenWRT等主流系统,让您轻松上手。 实时监控:能够同时监控多个服务器的系统状态,

    2024年03月10日
    浏览(114)
  • VMware vCenter服务器常用的巡检命令、运维命令和PowerShell脚本

    一、前言 最近整理一些VMware vCenter和Esxi常用的巡检命令和运维命令如下: 二、巡检命令 三、运维命令 运维常用命令: 四、Powershell脚本 以上就是vCenter和ESXi常用的运维与监控命令,可以帮助vSphere管理员管理和监控环境。

    2024年02月11日
    浏览(40)
  • 【树莓派烤肉 001】从 0 开始用自己的树莓派搭建服务器:运维篇

    没错,我承认这个标题写的很好吃…… 话说我家里那只树莓派 4B 8GB 放了两年了,除了吃灰还是吃灰~ 于是我想用它做一些非同寻常的事情,比如搭服务器。之前建网站都是用的 GitHub Pages,这次我们整个真实的 LAMP 环境。话不多说,下面开始搭建运行环境~ (不用担心树莓派

    2024年02月09日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包