C++ <iomanip>头文件

`iomanip`是C++标准库中提供了对输入输出流的输入输出格式进行控制的头文件。它定义了一些操纵符(manipulator),可以用于设置输出流的格式。

下面是一些常用的 `iomanip` 操纵符的详细说明:

  • setw(n):设置输出字段的宽度为n个字符,默认为右对齐。例如:
#include <iostream>
#include <iomanip>
using namespace std;

int main() 
{   
int number = 12345;   cout << setw(10) << number << endl;   return 0; }

输出:

12345 

 

  • setfill(ch):设置填充字符为ch(可以是字符常量或字符变量)。例如:
#include <iostream>
#include <iomanip>
using namespace std;

int main() 
{
    int number = 12345;
    cout << setw(10) << setfill('*') << number << endl;
    return 0;
}

输出:

*****12345

 

  • setprecision(n):设置浮点数输出的精度为n位小数。例如:
#include <iostream>
#include <iomanip>
using namespace std;

int main() 
{
    double number = 3.14159;
    cout << setprecision(4) << number << endl;
    return 0;
}

输出:

3.142

 

  • fixed:以定点数的形式输出浮点数。例如:
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    double number = 3.14159;
    cout << fixed << number << endl;
    return 0;
}

输出:

3.141590

 

  • scientific:以科学计数法的形式输出浮点数。例如:
#include <iostream>
#include <iomanip>
using namespace std;

int main() 
{
    double number = 12345.6789;
    cout << scientific << number << endl;
    return 0;
}

输出:

1.234568e+04

 

  • left:左对齐输出。例如:
#include <iostream>
#include <iomanip>
using namespace std;

int main() 
{
    int number = 12345;
    cout << left << setw(10) << number << endl;
    return 0;
}

输出:

12345

 

  • right:右对齐输出(默认)。

 

通过使用这些操纵符,你可以灵活控制输出流的格式,以满足特定的输出需求。

posted @ 2023-07-02 12:42  Geni_w  阅读(175)  评论(0编辑  收藏  举报