阅读量:108
在C++中没有内置的format函数,相对应的功能可以使用以下方法实现:
- 使用
std::stringstream类来格式化输出数据:
#include
#include
#include
int main() {
std::stringstream ss;
int num = 123;
double pi = 3.14159;
ss << "The number is: " << num class="hljs-string">", and the value of pi is: " << std class="hljs-built_in">setprecision(2) << pi class="hljs-built_in">str() << std class="hljs-keyword">return 0;
}
- 使用
printf函数进行格式化输出:
#include
int main() {
int num = 123;
double pi = 3.14159;
printf("The number is: %d, and the value of pi is: %.2f\n", num, pi);
return 0;
}
这些方法可以实现对数据的格式化输出,提供了一定程度上的灵活性和控制。