#include <iostream>
#include <iomanip>
#include <string>
int main() {
// 格式化输出整数
int num = 42;
std::cout << "整数: " << num << std::endl;
// 设置宽度和对齐方式
std::cout << std::setw(10) << std::left << "左对齐" << std::endl;
std::cout << std::setw(10) << std::right << "右对齐" << std::endl;
// 格式化输出浮点数
double pi = 3.1415926535;
std::cout << "默认精度: " << pi << std::endl;
std::cout << "设置精度为2: " << std::fixed << std::setprecision(2) << pi << std::endl;
// 格式化字符串
std::string name = "Alice";
std::cout << "Hello, " << name << "!" << std::endl;
return 0;
}
std::cout
输出整数。std::setw
设置输出宽度,std::left
和 std::right
控制对齐方式。std::fixed
固定小数点表示法,并用 std::setprecision
设置精度。以上代码展示了如何在 C++ 中使用标准库中的工具来格式化输出不同类型的数据。
上一篇:c++ orm框架
下一篇:c++如何判断素数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站