• C++标准模板(STL)- 输入/输出操纵符-(std::setprecision,std::setw)


    操纵符是令代码能以 operator<< 或 operator>> 控制输入/输出流的帮助函数。

    不以参数调用的操纵符(例如 std::cout << std::boolalpha; 或 std::cin >> std::hex; )实现为接受到流的引用为其唯一参数的函数。 basic_ostream::operator<< 和 basic_istream::operator>> 的特别重载版本接受指向这些函数的指针。这些函数(或函数模板的实例化)是标准库中仅有的可取址函数。 (C++20 起)

    以参数调用的操纵符(例如 std::cout << std::setw(10); )实现为返回未指定类型对象的函数。这些操纵符定义其自身的进行请求操作的 operator<< 或 operator>> 。

    定义于头文件
     

    更改浮点精度

    std::setprecision

    定义于头文件

    /*unspecified*/ setprecision( int n );

    用于表达式 out << setprecision(n) 或 in >> setprecision(n) 时,设置流 outinprecision 参数准确为 n

    参数

    n-精度的新值

    返回值

    返回未指定类型的对象,使得若 str 是 std::basic_ostream 类型的输出流名称或 std::basic_istream 类型的输入流名称,则表达式 str << setprecision(n) 或 str >> setprecision(n) 表现为如同执行下列代码:

    str.precision(n);

    调用示例

    1. #include <iostream>
    2. #include <iomanip>
    3. #include <cmath>
    4. #include <limits>
    5. int main()
    6. {
    7. const long double pi = std::acos(-1.L);
    8. std::cout << "default precision (6): "
    9. << pi << std::endl
    10. << "std::setprecision(10): "
    11. << std::setprecision(10) << pi << std::endl
    12. << "max precision: "
    13. << std::setprecision(std::numeric_limits<long double>::digits10 + 1)
    14. << pi << std::endl;
    15. return 0;
    16. }

    输出

    更改下个输入/输出域的宽度

    std::setw

    /*unspecified*/ setw( int n );

     用于表达式 out << setw(n) 或 in >> setw(n) 时,设置流 outinwidth 参数准确为 n

    参数

    n-width 的新值

    返回值

    返回未指定类型对象,满足若 str 是 std::basic_ostream 或 std::basic_istream 类型流的名称,则表达式 str << setw(n) 或 str >> setw(n) 表现为如同执行下列代码:

    str.width(n);

    注意

    若调用任何下列函数,则流的宽度属性将被设为零(表示“不指定”):

    • 输入
    • operator>>(basic_istream&, basic_string&)
    • operator>>(basic_istream&, char*)
    • 输出
    • basic_ostream::operator<<() 的重载 1-7 (在 num_put::put() 的阶段 3 )
    • operator<<(basic_ostream&, char) 和 operator<<(basic_ostream&, char*)
    • operator<<(basic_ostream&, basic_string&)
    • std::put_money (在 money_put::put() 内)
    • std::quoted (以输出流使用时)

    此修改器在输入与输出上的准确效果在单独的 I/O 函数间有别,单独地描述于每个 operator<<operator>> 重载的页面。

    调用示例

    1. #include <sstream>
    2. #include <iostream>
    3. #include <iomanip>
    4. int main()
    5. {
    6. std::cout << "no setw:" << 42 << std::endl
    7. << "setw(6):" << std::setw(6) << 42 << std::endl
    8. << "setw(6), several elements: " << 89
    9. << std::setw(6) << 12 << 34 << std::endl;
    10. std::istringstream is("hello, world");
    11. char arr[10];
    12. is >> std::setw(6) >> arr;
    13. std::cout << "Input from \"" << is.str()
    14. << "\" with setw(6) gave \""
    15. << arr << "\"" << std::endl;
    16. return 0;
    17. }

    输出

  • 相关阅读:
    哈希表与有序表
    Linux Docker 安装 Nacos
    【录用案例】CCF-B类,3区智能传感器类SCI&EI,仅2个月录用
    C语言--每日五道选择题--Day12
    WIFI6特性分析
    web缓存—Squid代理服务
    kotlin类
    如何避免毕业论文选题和别人雷同?
    壳聚糖接枝聚乙醇酸共聚物水凝胶/木质素磺酸钠/聚唾液酸-壳聚糖衍生物水凝胶的制备
    翼龙面板是什么,如何进行搭建
  • 原文地址:https://blog.csdn.net/qq_40788199/article/details/133419821