当前位置: 首页 > news >正文

使用string和string_view(四)——练习

目录

  • 1. 练习2.1
    • 题目
    • 答案
  • 2. 练习2.2
    • 题目
    • 答案
  • 3. 练习2.3
    • 题目
    • 答案
  • 参考

1. 练习2.1

题目

  编写一个程序,要求用户输入两个字符串,然后使用三向比较运算符将其按字母表顺序打印出来。为了获取一个字符串,可以使用std::cin流。

std::string s;
std::getline(std::cin, s);

答案

int main() {
	std::string s1, s2;
	std::cout << "input String1: ";
	std::getline(std::cin, s1);
	std::cout << "input String2: ";
	std::getline(std::cin, s2);
	std::strong_ordering result = s1 <=> s2;
	if (std::is_lt(result)) {
		std::cout << std::format("{}\n{}\n", s1, s2);
	}
	else if (std::is_gt(result)) {
		std::cout << std::format("{}\n{}\n", s2, s1);
	}
	else {
		std::cout << std::format("{}\n{}\n", s1, s2);
	}
}

2. 练习2.2

题目

  编写一个程序,要求用户提供源字符串haystack、要在源字符串中查找的字符串needle以及替换字符串。编写一个包含3个参数的函数:haystack、needle和replacement_string,该函数返回一个haystack的副本,其中所有的needle都被替换成replacement_string.要求使用std::string,不使用std::string。你将使用哪种类型的参数,为什么?在main()中调用此函数并打印所有字符串以进行验证。

答案

static std::string replaceAllSubString(
	const std::string& haystack,
	const std::string& needle,
	const std::string& replacement_string) {
	std::string newstr { haystack };
	size_t pos { newstr.find(needle) };
	
	while (pos != std::string::npos) {
		newstr = newstr.erase(pos, needle.size());
		newstr = newstr.insert(pos, replacement_string);
		pos = newstr.find(needle, pos + replacement_string.size());
	}

	return newstr;
}

int main() {
	std::string s { "ababbbbbaaababababababababbaaaaa" };
	std::string olds { "ab" };
	std::string news { "aba" };
	std::cout << replaceAllSubString(s, olds, news) << std::endl;
}

3. 练习2.3

题目

  修改练习2.2中的程序,并尽可能多地使用std::string_view。

答案

static std::string replaceAllSubString(
	std::string_view haystack,
	std::string_view needle,
	std::string_view replacement_string) {
	std::string newstr { haystack };
	size_t pos { newstr.find(needle) };
	
	while (pos != std::string::npos) {
		newstr = newstr.erase(pos, needle.size());
		newstr = newstr.insert(pos, replacement_string);
		pos = newstr.find(needle, pos + replacement_string.size());
	}

	return newstr;
}

int main() {
	std::string_view s { "ababbbbbaaababababababababbaaaaa" };
	std::string_view olds { "ab" };
	std::string_view news { "aba" };
	std::cout << replaceAllSubString(s, olds, news) << std::endl;
}

参考

[比] 马克·格雷戈勒著 程序喵大人 惠惠 墨梵 译 C++20高级编程(第五版)

http://www.dtcms.com/a/105048.html

相关文章:

  • 位置编码汇总 # 持续更新
  • AI提示词:自然景区智能客服
  • 计算机网络知识点汇总与复习——(二)物理层
  • # 深度学习基础算法:NN、RNN、CNN
  • 机器学习-04-分类算法-04-支持向量机SVM-案例
  • 保姆级教程:synchronized 同步方法 vs 同步代码块,看完彻底懂锁!
  • QML-项目实战二
  • Ubuntu Live USB 如何使用
  • 《深度洞察:MySQL与Oracle中游标的性能分野》
  • 重新排序--区间问题--差分求频率,全开ll
  • 静态路由复习实验
  • MyBatis-Plus逆向工程
  • ORM框架
  • SQL Server安装后 SSMS 无法连接:身份验证模式错误
  • 可编辑36页PPT | “新基建”在数字化智慧高速公路中的支撑应用方案智慧高速解决方案智慧交通方案
  • 《C奥林匹斯宝典:基础篇 - 重载函数》
  • 机器人传感器系统---时间戳对齐
  • vue使用markdown-it-katex部分公式展示不正确 katex版本低
  • 深度学习--softmax回归
  • 基于TradingView和CTPBee的自动化期货交易系统实现
  • Saas产品性能优化实战
  • bluecode-字符替换与最长子串问题
  • 【开题报告+论文+源码】基于SpringBoot+Vue的乡村公共资源管理系统
  • VS Code查看html格式文件插件:Live Server、HTML Preview
  • 多个SVN版本库,共用同一个和密码配置文件
  • Nacos 配置信息的发布流程是怎样的?服务端是如何校验和处理这些信息的?
  • 什么是SAQ评级?SAQ评级的好处?SAQ评级的意义
  • 第七章:OSPF
  • Stable Diffusion|Ai赋能电商 Inpaint Anything
  • ideal自动生成类图的方法