C++ 输入 / 输出操纵符

输入 / 输出操纵符简介

C++ 的输入输出靠的是 iostream 库提供的对象和运算符重载实现。这种方法的好处是使用时自动处理,方便快捷。但是在实际应用时难免会遇到需要定制输入输出方式的情况。这时就需要使用 输入 / 输出操纵符 对 输入 / 输出流 进行控制。

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

不以实参调用的操纵符(例如 std::cout << std::boolalpha; 或 std::cin >> std::hex;)实现为接受流的引用为其唯一实参的函数。

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

iOS 库

使用时记得包含 < ios > 库头文件,并使用名称空间 std。

#include <ios>

using namespace std;
  1. 在布尔值的文本和数值表示间切换
// 函数原型为:std::ios_base& boolalpha( std::ios_base& str );
// 启用流 str 中的 boolalpha 标志
std::cout << std::boolalpha << true << endl;	// 输出 true

// 函数原型为:std::ios_base& noboolalpha( std::ios_base& str );
// 禁用流 str 中的 boolalpha 标志
std::cout << std::noboolalpha << true << endl;	// 输出 1
  1. 控制是否使用前缀指示数值基数
// 函数原型为:std::ios_base& showbase( std::ios_base& str );
// 启用流 str 中的 showbase 标志。
std::cout << std::hex << std::showbase << 42 << endl;
// 输出 0x2a

// 函数原型为:std::ios_base& noshowbase( std::ios_base& str );
// 禁用流 str 中的 showbase 标志。
std::cout << std::hex << std::noshowbase << 42 << '\n';
// 输出 2a
  1. 控制浮点表示是否始终包含小数点
// 函数原型为:std::ios_base& showpoint( std::ios_base& str );
// 启用浮点输出中的无条件小数点包含。在输入上无效果。
std::cout << std::showpoint << 1.0 << endl;	// 输出 1.00000

// 函数原型为:std::ios_base& noshowpoint( std::ios_base& str );
// 禁用浮点输出中的无条件小数点包含。在输入上无效果。
std::cout << std::noshowpoint << 1.0 << endl;	// 输出 1
  1. 控制是否将 + 号与非负数一同使用
// 函数原型为:std::ios_base& showpos( std::ios_base& str );
// 启用非负数输出中的正号 '+' 的显示。在输入上无效果。
std::cout << std::showpos << 3.14 << endl;	// 输出 +3.14

// 函数原型为:std::ios_base& noshowpos( std::ios_base& str );
// 禁用非负数输出中的正号 '+' 的显示。在输入上无效果。
std::cout << std::showpos << 3.14 << endl;	// 输出 3.14
  1. 控制是否跳过输入上的前导空白符(默认启用)
// 函数原型为:std::ios_base& skipws( std::ios_base& str );
// 启用有格式输入函数所做的跳过前导空白符。在输出上无效果。
char a, b, c;
std::cin >> std::skipws >> a >> b >> c;		// 输入 a b c
std::cout << a << b << c << endl;		// 输出 abc

// 函数原型为:std::ios_base& noskipws( std::ios_base& str );
//禁用有格式输入函数所做的跳过前导空白符。在输出上无效果。
char a, b, c;
std::cin >> std::noskipws >> a >> b >> c;	// 输入 a b c
std::cout << a << b << c << endl;		// 输出 a b
  1. 控制一些输出操作是否使用大写字母
// 函数原型为:std::ios_base& uppercase( std::ios_base& str );
// 启用浮点和十六进制整数输出中大写字符的使用。在输入时无效果。
std::cout << std::hex << std::uppercase << 0x2a << << endl;	// 输出 2A

// 函数原型为:std::ios_base& nouppercase( std::ios_base& str );
// 禁用浮点和十六进制整数输出中大写字符的使用。在输入时无效果。
std::cout << std::hex << std::nouppercase << 0x2a << << endl;	// 输出 2a
  1. 设置填充字符的布置
// 函数原型为:std::ios_base& left( std::ios_base& str );
// 修改填充字符的默认定位。应用到任何输出。在输入时无效果。
std::cout << std::left << std::setfill('*') << std::setw(12) <<
	-1.23  << ' ' << std::setw(12) << std::hex << 42 << endl;
// 输出 -1.23******* 2a**********

// 函数原型为:std::ios_base& right( std::ios_base& str );
// 修改填充字符的默认定位。应用到任何输出。在输入时无效果。
std::cout << std::right << std::setfill('*') << std::setw(12) <<
	-1.23  << ' ' << std::setw(12) << std::hex << 42 << endl;
// 输出 *******-1.23 ********2a

// 函数原型为:std::ios_base& internal( std::ios_base& str );
// 修改填充字符的默认定位。应用到整数、浮点和货币输出。在输入时无效果。
std::cout << std::internal << std::setfill('*') << std::setw(12) <<
	-1.23  << ' ' << std::setw(12) << std::hex << 42 << endl;
// 输出 -*******1.23 **********2a
  1. 更改用于整数输入/输出的基数
// 函数原型为:std::ios_base& dec( std::ios_base& str );
// 修改整数 I/O 的默认数值底。设置流 str 的 basefield 为 dec。
// 10 进制
std::cout << std::dec << 42 << endl;
// 输出 42

// 函数原型为:std::ios_base& hex( std::ios_base& str );
// 修改整数 I/O 的默认数值底。设置流 str 的 basefield 为 hex。
// 16 进制
std::cout << std::hex << 42 << endl;
// 输出 2a

// 函数原型为:std::ios_base& oct( std::ios_base& str );
// 修改整数 I/O 的默认数值底。设置流 str 的 basefield 为 oct。
// 8 进制
std::cout << std::oct << 42 << endl;
// 输出 52
  1. 更改用于浮点 I/O 的格式化
// 函数原型为:std::ios_base& fixed( std::ios_base& str );
// 设置流 str 的 floatfield 为 fixed。
// 以默认方式显示小数,默认显示 6 位小数
std::cout << std::fixed << 0.01 << endl;
// 输出:0.010000

// 函数原型为:std::ios_base& scientific( std::ios_base& str );
// 设置流 str 的 floatfield 为 scientific。
// 科学记数法显示小数
std::cout << std::scientific << 0.01 << endl;
// 输出:1.000000e-02

// 函数原型为:std::ios_base& hexfloat( std::ios_base& str );
// 设置流 str 的 floatfield 同时为 fixed 和 scientific。这启用十六进制浮点格式化。
// 十六进制浮点格式化
std::cout << std::hexfloat << 0.01 << endl;
// 输出:0x1.47ae147ae147bp-7

istream 库

使用时记得包含 < istream > 或 < iostream > 库头文件,并使用名称空间 std。

#include <istream>
// 或者
#include <iostream>

using namespace std;

消耗空白符

// 函数原型为:template< class CharT, class Traits > std::basic_istream<CharT, Traits>& ws( std::basic_istream<CharT, Traits>& is );
// 从输入流舍弃前导空白符。(例如:' ', '\t'等)
string line;
std::getline(cin >> std::ws, line);
cout << line << endl;
// 输入:	123456
// 输出:123456

ostream 库

使用时记得包含 < ostream > 或 < iostream > 库头文件,并使用名称空间 std。

#include <ostream>
// 或者
#include <iostream>

using namespace std;
  1. 输出 ‘\0’
// 函数原型为:template< class CharT, class Traits > std::basic_ostream<CharT, Traits>& ends( std::basic_ostream<CharT, Traits>& os );
// 向输出序列 os 中插入空字符。
// 注:此操纵符通常和 std::ostrstream 一起使用,用于其关联输出缓冲区需要为空终止,以作为 C 字符串处理的情形。
// 此操纵符不冲洗流。
std::ostrstream oss;
oss << "Sample text: " << 42 << std::ends;
std::printf("%s\n", oss.str());
// 输出:Sample text: 42
  1. 冲洗输出流
// 函数原型为:template< class CharT, class Traits > std::basic_ostream<CharT, Traits>& flush( std::basic_ostream<CharT, Traits>& os );
// 冲洗输出序列 os。
// 注:需要冲洗完整行时,可使用 std::endl 操纵符。每次输出操作都需要冲洗时,可使用 std::unitbuf 操纵符。
std::cout << std::flush;

  1. 输出 ‘\n’ 并冲洗输出流
// 函数原型为:template< class CharT, class Traits > std::basic_ostream<CharT, Traits>& endl( std::basic_ostream<CharT, Traits>& os );
// 插入换行符到输出序列 os 并冲洗它
// 注:多数实现中,标准输出是行缓冲的,而写入 '\n' 就会导致冲洗,除非执行 std::ios::sync_with_stdio(false)。这些情形中,不必要的 endl 会降低 文件输出 的性能,但不影响 标准输出 的性能。
std::cout << "123456" << endl;
// 输出:123456

iomanip 库

使用时记得包含 < iomanip > 库头文件,并使用名称空间 std。

#include <iomanip>

using namespace std;
  1. 更改填充字符
// 函数原型为:template< class CharT > /*未指定*/ setfill( CharT c );
// 设置流 out 的填充字符为 c。
cout << setfill('0') << setw(8) << 16 << endl;
// 输出:00000016
  1. 更改浮点精度
// 函数原型为:/*未指定*/ setprecision( int n );
//  设置流 out 或 in 的 precision 参数为 n。
// 设置 n 位有效数字(四舍五入)
cout << setprecision(4) << 3.1415 << endl;
// 输出:3.142

// 设置 n 位有效小数(四舍五入)
cout << fixed << setprecision(3) << 3.1415 << endl;
// 输出:3.142
  1. 更改下个输入/输出字段的宽度
// 函数原型为:/* 未指定 */ setw( int n );
// 设置流 out 或 in 的 width 参数为 n。
// 注:有些操作会将宽度重置为零,所以需要为多个操作设置宽度时可能需要多次调用 std::setw。
cout << setw(8) << "Hello" << endl;
// 输出:   Hello

参考资料

https://zh.cppreference.com/w/cpp/io/manip

  • 13
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值