Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c++格式化字符串

作者:浪子罢了   发布日期:2025-10-11   浏览:56

#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;
}

解释说明:

  1. 格式化输出整数:直接使用 std::cout 输出整数。
  2. 设置宽度和对齐方式:使用 std::setw 设置输出宽度,std::leftstd::right 控制对齐方式。
  3. 格式化输出浮点数:使用 std::fixed 固定小数点表示法,并用 std::setprecision 设置精度。
  4. 格式化字符串:直接输出字符串变量。

以上代码展示了如何在 C++ 中使用标准库中的工具来格式化输出不同类型的数据。

上一篇:c++ orm框架

下一篇:c++如何判断素数

大家都在看

c++闭包

c++单引号和双引号的区别

c++ 注释

c++如何判断素数

c++格式化字符串

c++ orm框架

队列c++

c++freopen怎么用

c++ 获取系统时间

c++进制转换函数

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站