Namespaces
Variants

std:: left, std:: right, std:: internal

From cppreference.net
< cpp ‎ | io ‎ | manip
Input/output manipulators
Floating-point formatting
Integer formatting
Boolean formatting
Field width and fill control
internal left right
Other formatting
Whitespace processing
Output flushing
Status flags manipulation
Time and money I/O
(C++11)
(C++11)
(C++11)
(C++11)
Quoted manipulator
(C++14)
定义于头文件 <ios>
(1)
(2)
std:: ios_base & internal ( std:: ios_base & str ) ;
(3)

修改输出流中填充字符的定位方式。 left right 适用于所有输出类型, internal 适用于整型、浮点型和货币数值输出。对输入操作无影响。

1) 将流 str adjustfield 设置为 left ,如同调用 str. setf ( std:: ios_base :: left , std:: ios_base :: adjustfield )
2) 设置流 str adjustfield right ,如同调用 str. setf ( std:: ios_base :: right , std:: ios_base :: adjustfield )
3) 设置流 str adjustfield internal ,如同调用 str. setf ( std:: ios_base :: internal , std:: ios_base :: adjustfield )

标准流的初始默认设置等同于 right

这是一个I/O操纵器。它可以像 out << std :: left 这样被调用,其中 out std::basic_ostream 类型的对象;或者像 in >> std :: left 这样被调用,其中 in std::basic_istream 类型的对象。

目录

参数

str - I/O 流的引用

返回值

str (指向经过操作后的流的引用)。

示例

#include <iomanip>
#include <iostream>
#include <locale>
int main()
{
    std::cout.imbue(std::locale("en_US.utf8"));
    std::cout << "默认定位:\n" << std::setfill('*')
              << std::setw(12) << -1.23  << '\n'
              << std::setw(12) << std::hex << std::showbase << 42 << '\n'
              << std::setw(12) << std::put_money(123, true) << "\n\n";
    std::cout << "左对齐定位:\n" << std::left
              << std::setw(12) << -1.23  << '\n'
              << std::setw(12) << 42 << '\n'
              << std::setw(12) << std::put_money(123, true) << "\n\n";
    std::cout << "内部定位:\n" << std::internal
              << std::setw(12) << -1.23  << '\n'
              << std::setw(12) << 42 << '\n'
              << std::setw(12) << std::put_money(123, true) << "\n\n";
    std::cout << "右对齐定位:\n" << std::right
              << std::setw(12) << -1.23  << '\n'
              << std::setw(12) << 42 << '\n'
              << std::setw(12) << std::put_money(123, true) << '\n';
}

输出:

默认定位:
*******-1.23
********0x2a
***USD *1.23
左对齐定位:
-1.23*******
0x2a********
USD *1.23***
内部定位:
-*******1.23
0x********2a
USD ****1.23
右对齐定位:
*******-1.23
********0x2a
***USD *1.23

参见

更改下一个输入/输出字段的宽度
(函数)
更改填充字符
(函数模板)
控制是否使用前缀指示数值进制
(函数)