【C++】输入输出流 ⑧ ( cout 输出格式控制 | 设置进制格式 - dex、hex、oct | 指定输出宽度 / 填充 - setw / setfill | 指定浮点数格式 )
  K1I6GvxBEuyy 2023年12月22日 42 0



文章目录

  • 一、cout 输出格式控制
  • 1、cout 输出格式控制的两种方式
  • 2、格式化输出 控制符
  • 二、指定输出进制 - dex、hex、oct
  • 1、cout 输出进制设置
  • 2、代码示例 - cout 输出进制设置
  • 3、显示进制前缀 - showbase
  • 4、使用 setbase() 指定进制
  • 三、指定输出宽度 / 填充 - setw / setfill
  • 1、cout 指定输出宽度 / 填充
  • 2、代码示例 - cout 指定输出宽度 / 填充
  • 四、指定浮点数格式 - setprecision / setiosflags
  • 1、cout 指定浮点数格式
  • 2、代码示例 - cout 指定浮点数格式




指定输出格式 , 一般需要导入 <iomanip> 头文件 ;

#include "iostream"
using namespace std;
#include <iomanip>






一、cout 输出格式控制




1、cout 输出格式控制的两种方式



在使用 cout 标准输出流 输出数据时 , 默认不指定 输出格式 , 系统会根据输出的类型 输出 默认的字符格式 ,



如果开发者希望指定数据的 输出格式 , 如 : 指定 十六进制 / 八进制 显示 , 小数点位数要求 等 ; 可以使用以下两种方式 指定 输出格式 :

  • 使用 cout 对象的 成员函数 指定输出格式 , 上一篇博客 【C++】输入输出流 ⑦ ( cout 标准输出流对象 | cout.write 函数 | cout.width / cout.fill / cout.setf 函数 ) 中 使用 cout.width / cout.fill / cout.setf 函数 就是指定 输出格式 ;
  • 使用 控制符 指定输出格式 , 本篇博客中着重讲解 ;


2、格式化输出 控制符



输出流 中 格式化输出 控制符 :

  • std::left : 左对齐 ;
  • std::right : 右对齐 ;
  • std::internal : 内部对齐 ;
  • std::dec : 使用十进制数 ;
  • std::hex : 使用十六进制数 ;
  • std::oct : 使用 八进制数 ;
  • std::showbase : 显示符号或前缀 ;
  • std::showpoint : 显示小数点后的 0 ;
  • std::showpos : 显示正号 ;
  • std::fixed : 固定精度 ;
  • std::scientific : 科学计数法 ;





二、指定输出进制 - dex、hex、oct




1、cout 输出进制设置



cout 输出进制设置 :

  • std::dec : 使用 十进制数 ;
  • std::hex : 使用 十六进制数 ;
  • std::oct : 使用 八进制数 ;


2、代码示例 - cout 输出进制设置



代码示例 :

#include "iostream"
using namespace std;

int main() {

	int a = 16;

	cout << "八进制 : " << oct << a << endl;
	cout << "十进制 : " << dec << a << endl;
	cout << "十六进制 : " << hex << a << endl;

	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

执行结果 :

八进制 : 20
十进制 : 16
十六进制 : 10
Press any key to continue . . .

【C++】输入输出流 ⑧ ( cout 输出格式控制 | 设置进制格式 - dex、hex、oct | 指定输出宽度 / 填充 - setw / setfill | 指定浮点数格式 )_IO流



3、显示进制前缀 - showbase



八进制 通常是以 数字 0 开头 ;

十进制 是默认显示样式 , 前面没有前缀 ;

十六进制 前缀为 0x ;



默认情况下 是不显示 前缀的 , 使用 showbase 操作符 , 可以设置 输出格式 显示前缀 ;



代码示例 :

#include "iostream"
using namespace std;

int main() {

	int a = 16;

	cout << showbase;

	cout << "八进制 : " << oct << a << endl;
	cout << "十进制 : " << dec << a << endl;
	cout << "十六进制 : " << hex << a << endl;

	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

执行结果 :

八进制 : 020
十进制 : 16
十六进制 : 0x10
Press any key to continue . . .

【C++】输入输出流 ⑧ ( cout 输出格式控制 | 设置进制格式 - dex、hex、oct | 指定输出宽度 / 填充 - setw / setfill | 指定浮点数格式 )_cout_02



4、使用 setbase() 指定进制



<iomanip> 头文件中的 setbase() 函数 , 可以直接设置进制数 ; 原型如下 :

_NODISCARD _MRTIMP2 _Smanip<int> __cdecl setbase(int);

使用前需要导入 <iomanip> 头文件 ;



代码示例 :

#include "iostream"
using namespace std;
#include <iomanip> 

int main() {

	int a = 16;

	cout << showbase;

	cout << "八进制 : " << setbase(8) << a << endl;
	cout << "十进制 : " << setbase(10) << a << endl;
	cout << "十六进制 : " << setbase(16) << a << endl;

	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

【C++】输入输出流 ⑧ ( cout 输出格式控制 | 设置进制格式 - dex、hex、oct | 指定输出宽度 / 填充 - setw / setfill | 指定浮点数格式 )_开发语言_03






三、指定输出宽度 / 填充 - setw / setfill




1、cout 指定输出宽度 / 填充



<iomanip> 头文件中的 setw() 函数 , 可以设置输出字符宽度 ; 原型如下 :

_NODISCARD _MRTIMP2 _Smanip<streamsize> __cdecl setw(streamsize);

<iomanip> 头文件中的 setfill() 函数 , 可以设置输出字符填充 , 如果输出字符宽度不够 , 使用该函数设置填充 ; 原型如下 :

// FUNCTION TEMPLATE setfill
template <class _Elem>
_NODISCARD _Fillobj<_Elem> setfill(_Elem _Ch) {
    return _Fillobj<_Elem>(_Ch);
}

使用前需要导入 <iomanip> 头文件 ;



2、代码示例 - cout 指定输出宽度 / 填充



代码示例 : 下面的代码中 , 设置输出字符个数 10 个 , 如果不够 10 个 , 使用 * 填充 ;

#include "iostream"
using namespace std;
#include <iomanip> 

int main() {

	char buf[] = "hello";

	cout << setw(10) << setfill('*') << buf << endl;

	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

执行结果 :

*****hello
Press any key to continue . . .

【C++】输入输出流 ⑧ ( cout 输出格式控制 | 设置进制格式 - dex、hex、oct | 指定输出宽度 / 填充 - setw / setfill | 指定浮点数格式 )_输入输出流_04






四、指定浮点数格式 - setprecision / setiosflags




1、cout 指定浮点数格式



setprecision 函数的作用是 控制 浮点数 有效数字个数 , 如 : 浮点数 3.14 的 有效字符个数是 3 ; 函数原型如下 :

_NODISCARD _MRTIMP2 _Smanip<streamsize> __cdecl setprecision(streamsize);

setiosflags 函数可以设置 输出格式 , setiosflags(ios::scientific) 设置指数形式输出 ;

_NODISCARD _MRTIMP2 _Smanip<ios_base::fmtflags> __cdecl setiosflags(ios_base::fmtflags);

使用前需要导入 <iomanip> 头文件 ;

常见的输出格式如下 :

static constexpr _Fmtflags skipws     = static_cast<_Fmtflags>(0x0001);
    static constexpr _Fmtflags unitbuf    = static_cast<_Fmtflags>(0x0002);
    static constexpr _Fmtflags uppercase  = static_cast<_Fmtflags>(0x0004);
    static constexpr _Fmtflags showbase   = static_cast<_Fmtflags>(0x0008);
    static constexpr _Fmtflags showpoint  = static_cast<_Fmtflags>(0x0010);
    static constexpr _Fmtflags showpos    = static_cast<_Fmtflags>(0x0020);
    static constexpr _Fmtflags left       = static_cast<_Fmtflags>(0x0040);
    static constexpr _Fmtflags right      = static_cast<_Fmtflags>(0x0080);
    static constexpr _Fmtflags internal   = static_cast<_Fmtflags>(0x0100);
    static constexpr _Fmtflags dec        = static_cast<_Fmtflags>(0x0200);
    static constexpr _Fmtflags oct        = static_cast<_Fmtflags>(0x0400);
    static constexpr _Fmtflags hex        = static_cast<_Fmtflags>(0x0800);
    static constexpr _Fmtflags scientific = static_cast<_Fmtflags>(0x1000);
    static constexpr _Fmtflags fixed      = static_cast<_Fmtflags>(0x2000);

    static constexpr _Fmtflags hexfloat = static_cast<_Fmtflags>(0x3000); // added with TR1 (not in C++11)

    static constexpr _Fmtflags boolalpha   = static_cast<_Fmtflags>(0x4000);
    static constexpr _Fmtflags _Stdio      = static_cast<_Fmtflags>(0x8000);
    static constexpr _Fmtflags adjustfield = static_cast<_Fmtflags>(0x01C0); // left | right | internal
    static constexpr _Fmtflags basefield   = static_cast<_Fmtflags>(0x0E00); // dec | oct | hex
    static constexpr _Fmtflags floatfield  = static_cast<_Fmtflags>(0x3000); // scientific | fixed



2、代码示例 - cout 指定浮点数格式



代码示例 :

#include "iostream"
using namespace std;
#include <iomanip> 

int main() {

	double pi = 22.0 / 7.0; 

	cout << "正常输出 : " << pi << endl;
	cout << "控制浮点数有效数字个数 8 位 : " << setprecision(8) << pi << endl;
	cout << "指数形式输出 且 保留小数点后 8 位 : " << setiosflags(ios::scientific) << setprecision(8) << pi << endl;

	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

执行结果 :

正常输出 : 3.14286
控制浮点数有效数字个数 8 位 : 3.1428571
指数形式输出 且 保留小数点后 8 位 : 3.14285714e+00
Press any key to continue . . .

【C++】输入输出流 ⑧ ( cout 输出格式控制 | 设置进制格式 - dex、hex、oct | 指定输出宽度 / 填充 - setw / setfill | 指定浮点数格式 )_开发语言_05


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月22日 0

暂无评论

推荐阅读
  8Tw5Riv1mGFK   2024年05月01日   71   0   0 C++
  BYaHC1OPAeY4   2024年05月08日   53   0   0 C++
  yZdUbUDB8h5t   2024年04月29日   54   0   0 C++
  yZdUbUDB8h5t   2024年05月05日   40   0   0 C++
  oXKBKZoQY2lx   2024年05月17日   50   0   0 C++
K1I6GvxBEuyy
作者其他文章 更多