C++学习笔记七:输出格式

发布时间 2023-12-11 21:36:28作者: Sternenhimmel1997

这一篇主要总结一下C++标准库里输出格式相关的库函数。

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

1.库:

<ostream> <ios> <iomanip>

 

2.库函数:

2.1 <ostream>

std::endl: 输出换行,同"\n"的效果一样。

std::flush: causes immediate sending of data to the device connected to the stream. 输出到终端的数据会先存到buffer缓存中,等到缓存满了之后再输出到终端。这个函数设置跳过缓存直接输出到终端。

std::cout << "This is a nice message...." << std::endl << std::flush;

2.2 <iomanip>

std::setw(n): 设置字符串的占位,这个函数需要在输出的字符串之前加入,默认右对齐。

std::setfill(''): 自动将空位填充为某个字符,与setw()连用

std::cout << "Formatted table : " << std::endl;
std::cout << std::setfill('*');
std::cout << std::setw(10) <<  "Lastname"  << std::setw(10) << "Firstname" << std::setw(5) << "Age" << std::endl;
std::cout << std::setw(10) << "Daniel"  << std::setw(10) << "Gray" << std::setw(5) << "25" << std::endl;
std::cout << std::setw(10) << "Stanley" << std::setw(10)  << "Woods" << std::setw(5) <<  "33" << std::endl;
std::cout << std::setw(10) <<  "Jordan" << std::setw(10)  << "Parker" << std::setw(5) << "45" << std::endl;
std::cout << std::setw(10) <<  "Joe" << std::setw(10) << "Ball" << std::setw(5) << "21" << std::endl;
std::cout << std::setw(10) << "Josh" << std::setw(10) << "Carr" << std::setw(5) <<"27" << std::endl;
std::cout << std::setw(10) << "Izaiah" << std::setw(10) << "Robinson" << std::setw(5) << "29" << std::endl;

std::setprecision(n): 设置小数点的精度,设置之后输出,默认是6位

a = 3.1415926535897932384626433832795;
    
std::cout << std::endl;
std::cout << "a (default precision(6)) : " << a <<  std::endl;
std::cout << std::setprecision(10);
std::cout << "a (precision(10)) : " << a << std::endl;
std::cout << std::setprecision(20);
std::cout << "a (precision(20)) : " << a << std::endl;

 

2.3 <ios>

std::right/left/internal: 设置之后对齐方式,一般和std::setw()一起使用

std::cout << "Left justified table(default) :  " << std::endl;
col_width = 20;
std::cout << std::left;
std::cout << std::setw(col_width) <<  "Lastname"  << std::setw(col_width) << "Firstname" << std::setw(col_width/2) << "Age" << std::endl;
std::cout << std::setw(col_width) << "Daniel"  << std::setw(col_width) << "Gray" << std::setw(col_width/2) << "25" << std::endl;
std::cout << std::setw(col_width) << "Stanley" << std::setw(col_width)  << "Woods" << std::setw(col_width/2) <<  "33" << std::endl;
std::cout << std::setw(col_width) <<  "Jordan" << std::setw(col_width)  << "Parker" << std::setw(col_width/2) << "45" << std::endl;
std::cout << std::setw(col_width) <<  "Joe" << std::setw(col_width) << "Ball" << std::setw(col_width/2) << "21" << std::endl;
std::cout << std::setw(col_width) << "Josh" << std::setw(col_width) << "Carr" << std::setw(col_width/2) <<"27" << std::endl;
std::cout << std::setw(col_width) << "Izaiah" << std::setw(col_width) << "Robinson" << std::setw(col_width/2) << "29" << std::endl;

std::boolalpha / noboolalpha: 设置之后输出,bool变量的输出格式

std::showpos / noshowpos: 设置之后输出,(不)显示输出的数字的正负号

std::dec / hex / oct: 设置之后输出,数字的几进制,只对整型数有效

int pos_int {717171};
std::cout << std::endl;
std::cout << "pos_int in different bases : " << std::endl;
std::cout << "pos_int (dec) : " << std::dec << pos_int << std::endl;
std::cout << "pos_int (hex) : " << std::hex << pos_int << std::endl;
std::cout << "pos_int (oct) : " << std::oct << pos_int << std::endl;

std::showbase / noshowcase: 设置之后的输出,配合上面的函数使用,会在十六进制前面加上0x,在八进制前面加上0

std::uppercase / nouppercase / lowercase: 设置之后的输出,字母的大小写

std::fixed / scientific: 设置之后的输出,默认使用科学计数法

std::cout << std::fixed;
std::cout << std::scientific;
std::cout.unsetf(std::ios::scientific | std::ios::fixed); // Hack

std::showpoint / noshowpoint: 设置之后的输出,默认不显示为0的小数,(是否)强制对无小数部分的浮点数加上小数点

double d {34.1};
double e {101.99};
double f {12.0};
int    g {45};

std::cout << std::endl;
std::cout << "noshowpoint (default) : " << std::endl;
std::cout << "d : " << d << std::endl;
std::cout << "e : " << e << std::endl;
std::cout << "f : " << f << std::endl; // 12
std::cout << "g : " << g << std::endl;

std::cout << std::endl;
std::cout << "showpoint: " << std::endl;
std::cout << std::showpoint;
std::cout << "d : " << d << std::endl;
std::cout << "e : " << e << std::endl;
std::cout << "f : " << f << std::endl; // 12.0
std::cout << "g : " << g << std::endl;