C++中字符串格式化主要通过printf和stringstream实现,前者源自C语言、效率高但类型不安全,后者为C++流库组件、类型安全且可扩展;两者在精度、对齐、填充控制上各有语法体系,stringstream支持自定义类型输出并通过重载operator<<提升代码一致性与维护性,而printf受限于基本类型参数传递;现代C++推荐使用stringstream或C++20的std::format以兼顾安全性与性能。

C++中格式化输出字符串主要通过C风格的
printf
stringstream
解决方案
在C++中,处理字符串格式化输出,我们主要有两大阵营:C语言继承下来的
printf
stringstream
printf
立即学习“C++免费学习笔记(深入)”;
#include <cstdio> // For printf
void demonstrate_printf() {
int value = 42;
double pi = 3.1415926535;
printf("整数: %d, 圆周率: %.2f\n", value, pi); // 输出:整数: 42, 圆周率: 3.14
printf("左对齐字符串: %-10s, 右对齐整数: %5d\n", "Hello", 123); // 输出:左对齐字符串: Hello , 右对齐整数: 123
}这里
%d
%.2f
%-10s
%5d
printf
%d
stringstream
<sstream>
std::cout
<<
stringstream
str()
#include <iostream> // For cout
#include <sstream> // For stringstream
#include <iomanip> // For manipulators like setprecision, setw, setfill
void demonstrate_stringstream() {
int value = 42;
double pi = 3.1415926535;
std::ostringstream oss; // 使用 ostringstream 用于输出
oss << "整数: " << value << ", 圆周率: " << std::fixed << std::setprecision(2) << pi << std::endl;
// 输出:整数: 42, 圆周率: 3.14
oss << "左对齐字符串: " << std::left << std::setw(10) << "Hello"
<< ", 右对齐整数: " << std::right << std::setw(5) << std::setfill(' ') << 123 << std::endl;
// 输出:左对齐字符串: Hello , 右对齐整数: 123
std::cout << oss.str(); // 获取并输出最终的字符串
}stringstream
<iomanip>
std::setprecision
std::setw
std::setfill
printf
stringstream
选择哪个,很多时候取决于你的项目背景、团队习惯以及对性能和安全性的权衡。我个人在现代C++项目中更倾向于
stringstream
printf
stringstream
在实际的C++项目开发中,
printf
stringstream
printf
printf
stringstream
stringstream
printf
printf
stringstream
stringstream
printf
stringstream
printf
operator<<
我个人在C++项目中更倾向于
stringstream
std::format
printf
stringstream
精确控制输出格式是字符串格式化的核心需求,无论是打印报表、生成日志还是构建用户界面,都需要对数字的精度、文本的对齐方式以及空白填充有细致的掌控。
printf
stringstream
printf
printf
%.Nf
%.2f
%g
f
e
%Wf
%Ws
-
%-10s
printf
#include <cstdio>
void printf_formatting_example() {
double value = 123.456789;
int num = 7;
const char* text = "Data";
printf("浮点数(2位精度):%.2f\n", value); // 123.46
printf("浮点数(总宽10,2位精度):%10.2f\n", value); // 123.46
printf("整数(总宽5,右对齐):%5d\n", num); // 7
printf("字符串(总宽10,左对齐):%-10s\n", text); // Data
printf("字符串(总宽10,右对齐):%10s\n", text); // Data
}stringstream
stringstream
<iomanip>
std::fixed
std::setprecision(N)
std::fixed
std::setw(W)
std::left
std::right
std::setfill(char_value)
std::setfill('*')std::hex
std::dec
std::oct
#include <iostream>
#include <sstream>
#include <iomanip> // 包含 setprecision, setw, setfill, fixed, left, right
void stringstream_formatting_example() {
double value = 123.456789;
int num = 7;
const char* text = "Data";
std::ostringstream oss;
oss << "浮点数(2位精度):" << std::fixed << std::setprecision(2) << value << std::endl;
// 浮点数(2位精度):123.46
oss << "浮点数(总宽10,2位精度):" << std::setw(10) << std::setprecision(2) << value << std::endl;
// 浮点数(总宽10,2位精度): 123.46
oss << "整数(总宽5,右对齐):" << std::setw(5) << num << std::endl;
// 整数(总宽5,右对齐): 7
oss << "字符串(总宽10,左对齐):" << std::left << std::setw(10) << text << std::endl;
// 字符串(总宽10,左对齐):Data
oss << "字符串(总宽10,右对齐,填充*):" << std::right << std::setw(10) << std::setfill('*') << text << std::endl;
// 字符串(总宽10,右对齐,填充*):******Data
oss << "整数(十六进制):" << std::hex << num << std::dec << std::endl; // 切换回十进制
// 整数(十六进制):7
std::cout << oss.str();
}有时候,仅仅是调整一个数字的对齐方式,就能让日志文件或报表变得清晰很多。我记得有一次调试一个金融应用,数据不对齐简直是灾难,根本无法快速比对数值。
stringstream
printf
在C++中,处理自定义类型的格式化输出,以及确保格式化过程的健壮性,是体现C++面向对象特性和流库强大之处的关键。
stringstream
printf
自定义类型与stringstream
operator<<
cout
stringstream
operator<<
假设我们有一个表示三维坐标的
Point
#include <iostream>
#include <sstream>
#include <string>
struct Point {
int x, y, z;
// 为 Point 类型重载 operator<<
friend std::ostream& operator<<(std::ostream& os, const Point& p) {
os << "Point(" << p.x << ", " << p.y << ", " << p.z << ")";
return os;
}
};
void custom_type_formatting() {
Point p = {10, 20, 30};
std::ostringstream oss;
oss << "我的点是: " << p << std::endl;
// 输出:我的点是: Point(10, 20, 30)
std::cout << oss.str();
}通过重载
operator<<
Point
std::ostream
std::cout
std::ostringstream
Point
int
double
<<
printf
printf
printf
Point
printf
int
#include <cstdio>
struct Point_printf {
int x, y, z;
};
void printf_custom_type_limitation() {
Point_printf p = {10, 20, 30};
printf("我的点是: Point(%d, %d, %d)\n", p.x, p.y, p.z);
// 输出:我的点是: Point(10, 20, 30)
}这不仅增加了代码的冗余,也失去了封装性。每次输出
Point
Point
printf
错误处理与健壮性: 格式化输出过程中的错误相对较少,但并非不可能。
printf
sprintf
snprintf
printf
stringstream
stringstream
failbit
badbit
eofbit
badbit
oss.bad()
oss.fail()
stringstream
以上就是c++++如何格式化输出字符串_c++ printf与stringstream格式化技巧的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号