操纵符是令代码能以 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::showpoint,
std::noshowpoint
std::ios_base& showpoint( std::ios_base& str ); |
(1) | |
std::ios_base& noshowpoint( std::ios_base& str ); |
(2) |
启用或禁用浮点输出中的无条件小数点包含。在输入上无效果。
1) 如同用调用 str.setf(std::ios_base::showpoint) 启用流 str
中的 showpoint
标志
2) 如同用调用 str.unsetf(std::ios_base::showpoint) 禁用流 str
中的 showpoint
标志
这是一个 I/O 操纵符,可用如 out << std::showpoint 的表达式对任何 std::basic_ostream 类型的 out
或用如 in >> std::showpoint 的表达式对任何 std::basic_istream 类型的 in
调用。
由在输出时刻流中感染的 locale 的 numpunct 平面确定用作小数点的字符,如 std::num_put::put 中所描述。
参数
str | - | 到 I/O 流的引用 |
返回值
str
(到操纵后的流的引用)
调用示例
#include <iostream>
int main()
{
std::cout << "1.0 with showpoint: "
<< std::showpoint << 1.0 << std::endl
<< "1.0 with noshowpoint: "
<< std::noshowpoint << 1.0 << std::endl;
return 0;
}
输出
控制是否将 + 号与非负数一同使用
std::showpos,
std::noshowpos
std::ios_base& showpos( std::ios_base& str ); |
(1) | |
std::ios_base& noshowpos( std::ios_base& str ); |
(2) |
启用或禁用非负数输出中的正号 '+' 的显示。在输入上无效果。
1) 如同用调用 str.setf(std::ios_base::showpos) 启用流 str
中的 showpos
标志 2) 如同用调用 str.unsetf(std::ios_base::showpos) 禁用流 str
中的 showpos
标志
这是一个 I/O 操纵符,可用如 out << std::showpos 的表达式对任何 std::basic_ostream 类型的 out
或用如 in >> std::showpos 的表达式对任何 std::basic_istream 类型的 in
调用。
参数
str | - | 到 I/O 流的引用 |
返回值
str
(到操纵后的流的引用)
调用示例
#include <iostream>
int main()
{
std::cout << "showpos: " << std::showpos << 42
<< ' ' << 3.14 << ' ' << 0 << std::endl
<< "noshowpos: " << std::noshowpos << 42
<< ' ' << 3.14 << ' ' << 0 << std::endl;
return 0;
}
输出
控制是否跳过输入上的前导空白符
std::skipws,
std::noskipws
std::ios_base& skipws( std::ios_base& str ); |
(1) | |
std::ios_base& noskipws( std::ios_base& str ); |
(2) |
启用或禁用有格式输入函数所做的跳过前导空白符(默认启用)。在输出上无效果。
1) 如同用调用 str.setf(std::ios_base::skipws) 启用流 str
中的 skipws
标志
2) 如同用调用 str.unsetf(std::ios_base::skipws) 禁用流 str
中的 skipws
标志
跳过空白符由 std::basic_istream::sentry 的构造函数进行,它读取并舍弃由流所感染的 locale 的 std::ctype 平面分类为空白符的字符。
这是一个 I/O 操纵符,可用如 out << std::noskipws 的表达式对任何 std::basic_ostream 类型的 out
或用如 in >> std::noskipws 的表达式对任何 std::basic_istream 类型的 in
调用。
参数
str | - | 到 I/O 流的引用 |
返回值
str
(到操纵后的流的引用)
调用示例
#include <iostream>
#include <sstream>
int main()
{
char c1, c2, c3;
std::istringstream("a b c") >> std::skipws >> c1 >> c2 >> c3;
std::cout << "Default behavior: c1 = "
<< c1 << " c2 = " << c2 << " c3 = " << c3 << std::endl;
std::istringstream("a b c") >> std::noskipws >> c1 >> c2 >> c3;
std::cout << "noskipws behavior: c1 = "
<< c1 << " c2 = " << c2 << " c3 = " << c3 << std::endl;
return 0;
}