C++语法基础--输出格式控制--setf(),标准控制符,iomanip中的格式控制

11.控制小数显示格式
   fmtflags setf (fmtflags fmtfl);
         Set specific format flags
         sets the stream's format flags whose bits are set in fmtfl, leaving unchanged the rest

  
  其中fmtflags是bitmask类型的typedef名,用于存储格式标记,该名称是在ios_base类中定义的.
  ios_base类定义的代表位值的常量:(仅供参考,无需记忆)
   


  
设置修改将一直有效,直到被覆盖为止
  Example:
  int main()
{
   cout.setf(ios_base::boolalpha);
   cout<<true<<endl;
   cout<<false<<endl;
   cout.setf(ios_base::uppercase);
   cout.setf ( ios::showbase );                  
   cout <<hex<<100 << '\n';
   cout.setf(ios_base::showpoint);
   cout<<(float)1.1<<endl;
   return 0;
}



 运行结果:
 






 fmtflags setf (fmtflags fmtfl, fmtflags mask):
          sets the stream's format flags whose bits are set in both fmtfl and mask, 
          and clears the format flags whose bits are set in mask but not in fmtfl.


 第一个参数包含了所需设置的fmtflags值,第二个参数指出要清除第一个参数中的哪些位 
  常用参数组合及其含义:
 
  
 

  解析:因为上图第二个参数ajustfield,basefield,floatfield,清除了不止一个位,而第一个参数只设置对应的一个位



  *将位恢复为0
  void unsetf (fmtflags mask);
             Clear specific format flags
             Clears the format flags selected in mask


  Example:
  int main()
{
    cout.setf ( ios::hex, ios::basefield );  
// set hex as the basefield
    cout.setf ( ios::showbase );             // activate showbase
    cout << 100 << endl; //0x64
    cout.unsetf (ios::showbase );      // deactivate showbase
    cout << 100 << endl; //64
   
   return 0;
}






12.标准控制符
   使用setf()进行格式化比较繁琐,为此C++提供了多个控制符,这些控制符能调用setf(),并自动提供正确的参数
   一些常用标准控制符
 





13.头文件iomanip
   使用iostream工具来设置一些格式相当繁琐,为此,C++在头文件iomanip中提供了其他一些等价控制符
   setfill (char_type c):
            Set fill character
            Sets c as the stream's fill character.


   setprecision (int n):
            Set decimal precision
            Sets the decimal precision to be used to format floating-point values on output operations.


  setw (int n):
            Set field width
            Sets the field width to be used on output operations.


   setbase (int base):
            Set basefield flag
            Sets the basefield to one of its possible values: dec, hex or oct, according to argument base.


    setiosflags (ios_base::fmtflags mask):
            Set format flags
            Sets the format flags specified by parameter mask


   resetiosflags (ios_base::fmtflags mask);
             Reset format flags
             Unsets the format flags specified by parameter mask



   Example:
 #include<iostream>
#include<iomanip> 
using namespace std;


int main()
{


    cout << setfill ('*') <<setw(30);
//设置填充和宽度 
    cout << "hello" << endl;
  
    double f =1.0/3; 
    cout <<setprecision(9) << f << endl; //设置精度 
    
     cout << setbase(16);  //设置16进制 
     cout << 13 << endl;

     cout << setiosflags (ios::showbase | ios::uppercase); //用大写表示前缀 
     cout << 100 << endl;
     cout << resetiosflags(ios::showbase) << 100 << endl;//去除前缀 
   
   return 0;
}




  运行结果:
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值