C++标准模板(STL)- 输入/输出操纵符-(std::left, std::right, std::internal,std::dec, std::hex, std::oct)

操纵符是令代码能以 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::left, 
std::right, 
std::internal

std::ios_base& left( std::ios_base& str );

(1)

std::ios_base& right( std::ios_base& str );

(2)

std::ios_base& internal( std::ios_base& str );

(3)

 修改填充字符的默认定位。 leftright 应用到任何输出,而 internal 应用到整数、浮点和货币输出。在输入时无效果。

1) 如同用调用 str.setf(std::ios_base::left, std::ios_base::adjustfield) ,设置流 stradjustfieldleft

2) 如同用调用 str.setf(std::ios_base::right, std::ios_base::adjustfield) ,设置流 stradjustfieldright

3) 如同用调用 str.setf(std::ios_base::internal, std::ios_base::adjustfield) ,设置流 stradjustfieldinternal

这是一个 I/O 操纵符,可用如 out << std::left 的表达式对任何 std::basic_ostream 类型的 out 或用如 in >> std::left 的表达式对任何 std::basic_istream 类型的 in 调用。

参数

str - 到 I/O 流的引用

返回值

str (到操纵后的流的引用)

调用示例

#include <iostream>
#include <iomanip>
#include <locale>

int main()
{
    std::cout.imbue(std::locale("POSIX"));
    std::cout << std::setfill('*');

    std::cout << "Left fill:" << std::endl << std::left
              << std::setw(12) << -1.23  << std::endl
              << std::setw(12) << std::hex
              << std::showbase << 42 << std::endl
              << std::setw(12) << std::put_money(123, true)
              << std::endl << std::endl;

    std::cout << "Internal fill:" << std::endl << std::internal
              << std::setw(12) << -1.23  << std::endl
              << std::setw(12) << 42 << std::endl
              << std::setw(12) << std::put_money(123, true)
              << std::endl << std::endl;

    std::cout << "Right fill:" << std::endl << std::right
              << std::setw(12) << -1.23  << std::endl
              << std::setw(12) << 42 << std::endl
              << std::setw(12) << std::put_money(123, true)
              << std::endl;

    return 0;
}

输出

更改用于整数 I/O 的基数

std::dec, 
std::hex,
std::oct

std::ios_base& dec( std::ios_base& str );

(1)

std::ios_base& hex( std::ios_base& str );

(2)

std::ios_base& oct( std::ios_base& str );

(3)

修改整数 I/O 的默认数值底。

1) 如同以调用 str.setf(std::ios_base::dec, std::ios_base::basefield) 设置流 strbasefielddec

2) 如同以调用 str.setf(std::ios_base::hex, std::ios_base::basefield) 设置流 strbasefieldhex

3) 如同以调用 str.setf(std::ios_base::oct, std::ios_base::basefield) 设置流 strbasefieldoct

这是一个 I/O 操纵符,可用如 out << std::hex 的表达式对任何 std::basic_ostream 类型的 out 或用如 in >> std::hex 的表达式对任何 std::basic_istream 类型的 in 调用。

参数

str - 到 I/O 流的引用

返回值

str (到操纵后的流的引用)

调用示例

#include <iostream>
#include <sstream>

int main()
{
    std::cout << "The number 42 in octal:   "
              << std::oct << 42 << std::endl
              << "The number 42 in decimal: "
              << std::dec << 42 << std::endl
              << "The number 42 in hex:     "
              << std::hex << 42 << std::endl;

    int n;
    std::istringstream("2A") >> std::hex >> n;
    std::cout << std::dec
              << "Parsing \"2A\" as hex gives "
              << n << std::endl;

    // 输出基底是持久的,直至更改
    std::cout << std::hex << "42 as hex gives " << 42
              << " and 21 as hex gives " << 21 << std::endl;

    return 0;
}

输出

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/132957771
今日推荐