cout配合流操作算子举例(达到和printf近似的功能)

包含流操作算子的头文件是"iomanip"
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>//流操纵算子
#define pi acos(1.0)
#define e exp(-1.0)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pa;
const ll INF=0x3f3f3f3f3f3f3f3f;
     
int main() {
    
    
   
 // freopen(" .../.txt","r",stdin)
 // freopen(" .../.txt","w",stdout)
    ios::sync_with_stdio(false);
       int n=141;
    //1)分别以十六进制,十进制,八进制先后输出n
    cout << "1)" << hex << n << ' ' << dec << n << ' ' << oct << n << endl;
    double x = 1234567.89, y = 12.3456789;
    //2)保留五位有效数字
    cout <<"2) "<< setprecision(5) << x << ' ' << y << endl;
    //3)保留小数点后五位
    cout << "3)" << fixed << setprecision(5) << x << ' ' << y << endl;
    //4)科学计数法输出,保留小数点后五位
    cout << "4)" << scientific << setprecision(5) << x << ' ' << y << endl;
    //5)非负数要显示正号,输出宽度为12字符,宽度不足用'*'填充
    cout << "5)" << showpos << setw(12) << setfill('*') << 12.1 << endl;
    //6)非负数不显示正号,输出宽度为12字符,宽度不足则在右边用填充字符填充
    cout << "6)" << noshowpos << setw(12) << right << 12.1 << endl;
    //7)输出宽度为12字符,宽度不足则在左边用填充字符填充
    cout << "7)" << noshowpos << setw(12) << left << 12.1 << endl;
    //8)宽度不足时,负号和数值分列左右,中间用填充字符填充
    cout << "8)" << setw(12) << internal << -12.1 << endl;
    cout << "9)" << 12.1 << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43311695/article/details/106767907