c++简单格式化输出输入

前言

若想使用c++特有的输出格式控制,需要引用 iomanip 关文件。下面将介绍以下内容,已知请自行跳过:
本文提到的内容在sstream、fstream等流中也可以使用。

  • dec
  • oct
  • hex
  • uppercase
  • nouppercase
  • showbase
  • noshowbase
  • —————–
  • left
  • internal
  • right
  • setw()
  • setfill()
  • —————–
  • defaultfloat
  • hexfloat
  • scientific
  • fixed
  • showpos
  • noshowpos
  • setprecision()
  • —————–
  • boolalpha
  • noboolalhpa
  • —————–
  • skipws
  • noskipws

进制的操作

介绍

dec 将整数以10进制的方式输出,这也是默认的输出方式。
oct 将整数以8进制的方式输出
hex 将整数以16进制的方式输出
uppercase整数的16进制表示为字,包括内容和0X前缀。将将小数的科学表示法中的E表示为写。
nouppercase整数的16进制表示为字,包括内容和0X前缀。将将小数的科学表示法中的E表示为写。默认方式。
showbase 显示整数的16进制表示的0X前缀,显示整数的8进制表示的0前缀。
noshowbase 不显示前缀。默认方式。

代码

#include
#include
using namespace std;
int main() {
    int a = 1234;
    double b = 0.000123456;
    cout << oct << a << " " << a << endl;
    cout << dec << a << " " << a << endl;
    cout << hex << a << " " << a << endl;
    cout << showbase << a << " " << a << endl;
    cout << uppercase << a << " " << a << " "<cout << nouppercase << a << " " << a <<" " << scientific << b<//scientific:科学计数法
    cout << noshowbase << a << " " << a << endl;
    system("pause");
}

结果

c++简单格式化输出输入_第1张图片

宽度的操作

介绍

left 让输出内容在一定宽度内左对齐
internal 让输出内容在一定宽度内右对齐,如果是十进制数,并让符号左对齐
right 让输出内容在一定宽度内右对齐,默认方式。
setw(int n) 设置输出内容宽度,超出则原样显示,否则用字符填充,效果只有一次性
setfill(char ch) 设置填充字符

代码

#include
#include
using namespace std;
int main() {
    int a = -1234;
    double b = 0.000123456;//小数也有同样的效果,在此略
    cout << setfill('#');
    cout << setw(30) << left << a << endl;
    cout << setw(30) << internal << a << endl;
    cout << setw(30) << right << a << endl;
    a = -a;
    cout << setw(30) <//showpos就是把符号显示的意思
    system("pause");
}

结果

c++简单格式化输出输入_第2张图片

数的操作

介绍

defaultfloat 默认输出小数的方式
hexfloat 以16进制小数输出
scientific 以科学计数法输出
fixed 不使用科学计数法输出
showpos 如果是>=0的10进制数则输出带+号
noshowpos 如果是>=0的10进制数则输出不带+号
setprecision() 设置有效数字的个数

代码

#include
#include
using namespace std;
int main() {
    double b = 0.000123456;
    cout <<1<<" "<< hexfloat << b << endl;
    cout<<2 << " " << scientific << b << endl;
    cout << 3 << " " << fixed<< b << endl;
    cout << 4 << " " << defaultfloat << b << endl;
    cout << 5 << " " << setprecision(2) << b<cout << 6 << " " << 1234.4 << endl;
    cout << 7 << " " << scientific << b << endl;
    cout << 8 << " " << showpos << b << endl;
    cout << 9 << " " << noshowpos << b << endl;
    b = -b;
    cout << 10 << " " << showpos << b << endl;
    cout << 11 << " " << noshowpos << b << endl;
    system("pause");
}

结果

c++简单格式化输出输入_第3张图片

布尔的操作

介绍

boolalpha 将bool值以turefalse的形式输出
noboolalhpa 将bool值以10的形式输出,默认方式

代码

#include
#include
using namespace std;
int main() {
    bool a = true, b = false;
    cout << a << b<cout << boolalpha << a << b<cout << noboolalpha << a << b<"pause");
}

结果

outcome

输入的操作

介绍

skipws 输入的时候不接收空格,默认方式
noskipws 输入的时候接收空格

代码

#include
#include
using namespace std;
int main() {
    char a, b;
    cin >> a >> b;
    cout << a << b<cin >> noskipws;
    cin >> a >> b;
    cout << a << b<cin >> skipws;
    cin >> a >> b;
    cout << a << b << endl;
    system("pause");
}

结果

c++简单格式化输出输入_第4张图片

参考

http://www.cplusplus.com/reference/ios/skipws/

你可能感兴趣的:(c++)