【35】C++实战篇—— string字符串与int、float数据合并成一个新的字符串

文章目录

  • [1 使用 sprintf_s 和buf数组](#1 使用 sprintf_s 和buf数组)
  • [2 使用to_string()转换为str](#2 使用to_string()转换为str)

1 使用 sprintf_s 和buf数组

cpp 复制代码
if (!bRtn)
{
	string str1 = "太模糊,3根线条边界黏连,只检测到  ";
	string str2 = " 跟线条";

	char buf[3];//为转换后的字符串预留足够的空间
	memset(buf, '\0', 3); //初始化 buf 数组,是一个良好的习惯,因为这样可以确保字符串的终止符在正确的位置。
	sprintf_s(buf, "%d ", peakWidths.size());

	strErr = str1 + buf + str2;
	return false;
}

2 使用to_string()转换为str

cpp 复制代码
	if (!bRtn)
	{
		string str1 = "太模糊,3根线条边界黏连,只检测到 " + to_string(peakWidths.size()) + " 跟线条,背景亮度:"+ to_string(backVal);
		
		return false;
	}
相关推荐
eggwyw1 小时前
C++设计模式之解释器模式
开发语言·c++
我不会插花弄玉14 小时前
13.多路转接epoll【由浅入深-网络】
网络·c++·epoll
郝学胜-神的一滴16 小时前
Effective STL 条款03:容器中的对象复制要廉价且正确
开发语言·数据结构·c++·算法·rpc·stl
西西弗Sisyphus17 小时前
C++ explicit 关键字工程实践
开发语言·c++
AIgorithmGEEK19 小时前
std::unordered_map 与 std::unordered_set:接口、算法技巧与权衡 (速查)
c++·算法·哈希表·负载因子·桶·unordered_map·unordered_set
一拳一个呆瓜1 天前
【STL】头文件:<cmath>
c++·stl
草莓熊Lotso1 天前
【C++ 基于微服务的即时通讯系统】ElasticSearch 用户数据管理实现
linux·数据库·c++·elasticsearch·微服务·架构
慧都产品智能化1 天前
Qt 开发 CAD/CAM 工序表的隐性成本分析
c++·qt·qt5·qt6.3
eggwyw1 天前
linux下的C\C++多进程多线程编程实例详解
开发语言·c++
青少儿编程课堂1 天前
第十八届蓝桥杯青少组图形化真题 + 全国青少年信息素养大赛 + 电子学会图形化考级:校园泡泡堂大挑战解析
c++·python·算法·bfs·信息学竞赛