标准IO流,比较详细了

Standard I/O Stream

C++的标准输入/输出库 iostream
                         不仅提供了I/O库,还提供了使用库的流模式
    " cin>>"流入和 " cout<<"流出到输出设备的操作符

1.流状态 (Stream States)
  
    1st  showpos    在正数(含0)前显示 + 号
    2nd  showbase   HEX前加 0X,OCT前加 0
    3rd  uppercase  HEX里字母大写
    4th  showpoint  浮点输出即使小数点后都为0也加小数点
    5th  boolalpha  逻辑值用true false
    6th  left       左对齐
    7th  right      右对齐
    8th  dec        十进制表示整数
    9th  hex        十六进制
    10   oct        八进制
    11   fixed      定点数格式输出
    12   scientific 科学记数法格式输出

 2.取消流状态的操作
  
   noshowpos   noshowbase   nouppercase   noshowpoint   noboolalpha

   cout.unsetf(ios::scientific);


 3.有参数的3个常用的流状态
  
   width(int)         //设置显示宽度
   fill(char)         //设置填充字符
   precision(int)     //设置有效位数
  
  这些流状态是以 cout 捆绑调用他们的形式设置的,不能与流出符 << 连用

  特别注意 width(n)为一次性操作,既第二次显示时将不再有效 default width(0)


  example
         cout.width(5);
         cout.fill('S');
         cout<<23<<23;
  //SSS2323

4.与<<连用的设置方式
  
   使用时,要包含头文件 iomanip
    

      setw(int)
      setfill(char)
      setprecision(int)

example
          cout<<setw(6)<<setfill('$')<<27<<endl; //输出:$$$$27
倒三角形例子

#include<iostream>
#include<iomanip>
using namespace std;
void main()
{
 for(int n=1;n<=10;++n)
  cout<<setfill(' ')<<setw(n)<<" "
  <<setfill('M')<<setw(21-2*n)<<"M"<<endl;
}

另外使用string

#include<iostream>
#include<string>
using namespace std;
int main()
{
   for(int n=1;n<=10;++n)
        cout<<string(n,' ')+string(21-2*n,'M')+"/n"
}

加个例子   对精度设置的说明 在VC上测试过,主要是小数点右边位与总有效数字位的区别

 

 

//*********************
//**  ch2_1.cpp   **
//*********************
#include <iostream.h>
#include <iomanip.h> //要用到格式控制符
void main()
{

    double amount = 1221122121.0/7;
    cout <<amount <<endl;
    cout <<setprecision(0) <<amount <<endl //0按1位理解,如果是负值 则输出最大长度
    <<setprecision(1) <<amount <<endl     
    <<setprecision(2) <<amount <<endl
    <<setprecision(3) <<amount <<endl
    <<setprecision(4) <<amount <<endl;
    cout <<setiosflags(ios::fixed);
    cout <<setprecision(8) <<amount <<endl;//fixed 加上 setprecision设置小数点右边位数(8)
    cout <<setiosflags(ios::scientific) //科学记数 有效位数(8)
    <<amount <<endl;

 //重新设置成原默认设置
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值