setw的使用

// setw的使用
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    int num1 = 123, num2 = 4567;

    cout << "未使用setw则输出:" << endl ;
    cout << num1 << num2 << endl ;
    cout << "使用stew则输出: " << endl ;
    cout << setw( 4 ) << num1 << setw( 7 ) << num2 << endl;

    return 0;
}


 

/*
结果
未使用setw则输出:
1234567
使用stew则输出:
 123   4567
*/

// 其中123的前面只有一个空格,4567前面有三个空格
// setw(4)只对其后面的 num1 有作用,用来设置123的宽度即列数
// 同理 setw(7) 亦是如此

 

你可能感兴趣的:(c,setw,流操纵符)