在C++中,可以使用std::stringstream
或者printf
函数来格式化字符串。
使用std::stringstream
:
#include <iostream>
#include <sstream>
int main() {
std::stringstream ss;
int num = 10;
float f = 3.14;
std::string str = "Hello";
ss << "Number: " << num << ", Float: " << f << ", String: " << str;
std::string formatted_str = ss.str();
std::cout << formatted_str << std::endl;
return 0;
}
使用printf
函数:
#include <iostream>
#include <cstdio>
int main() {
int num = 10;
float f = 3.14;
std::string str = "Hello";
char buffer[100];
std::sprintf(buffer, "Number: %d, Float: %.2f, String: %s", num, f, str.c_str());
std::string formatted_str = buffer;
std::cout << formatted_str << std::endl;
return 0;
}
这样就可以通过std::stringstream
或者printf
函数来格式化字符串。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/897090.html