C++标准库——iomanip

1.功能

用来对输入输出操作的格式进行更加方便的控制,在ios_base基类的基础上将每一种格式的设置和删除都进行了函数级的同名封装,提供了全局的调用接口函数,支持在运算符“<<”和“>>”上的多次使用,配合ios_base实例的控制。是I/O流控制头文件,就像C里面的格式化输出一样。
如果在一次输出过程中需要混杂多种格式,使用ios_base的成员函数来处理就显得很不方便。STL另提供了iomanip库可以满足这种使用方式。

2.接口

控 制 符作 用
setbase(n)设置整数为n进制(n=8,10,16)
setfill(n)设置字符填充,c可以是字符常或字符变量
setprecision(n)设置浮点数的有效数字为n位
setw(n)设置字段宽度为n位
setiosflags(ios::fixed)设置浮点数以固定的小数位数显示
setiosflags(ios::scientific)设置浮点数以科学计数法表示
setiosflags(ios::left)输出左对齐
setiosflags(ios::right)输出右对齐
setiosflags(ios::skipws)忽略前导空格
setiosflags(ios::uppercase)在以科学计数法输出E与十六进制输出X以大写输出,否则小写。
setiosflags(ios::showpos)输出正数时显示”+”号
setiosflags(ios::showpoint)强制显示小数点
resetiosflags()终止已经设置的输出格式状态,在括号中应指定内容

上述接口与ios_base的格式控制成员是对应的,可以二者配合进行输出格式的精准控制。
其中的精度控制默认是6位有效数字,科学计数法中的指数部分e为默认小写。setw设置的宽度如果小于字段宽度会失效。

3.实例

#include <iostream>  
#include <iomanip>      
using namespace std;
int main()
{  
    double PI=3.141592654;  
    cout<<PI<<endl;  
    cout<<setprecision(2)<<PI<<endl;  
    cout<<fixed<<setprecision(2)<<PI<<endl;   
    cout<<setfill('*')<<setw(20)<<setprecision(10)<<PI<<endl;  
    cout<<setfill('*')<<setw(20)<<setprecision(10)<<left<<PI<<endl;  
    cout<<scientific<<setprecision(10)<<PI<<endl;  
    cout<<scientific<<uppercase<<setprecision(10)<<PI<<endl;    
    return 0 ;  
}  

输出结果如下:

3.141592654
3.1
*******3.1415926540
3.1415926540*******
3.1415926540e+000
3.1415926540E+000

4.iomanip文件核心内容

struct _Resetiosflags { ios_base::fmtflags _M_mask; };

  /**
   *  @brief  Manipulator for @c setf.
   *  @param  mask  A format flags mask.
   *
   *  Sent to a stream object, this manipulator resets the specified flags,
   *  via @e stream.setf(0,mask).
  */
  inline _Resetiosflags 
  resetiosflags(ios_base::fmtflags __mask)
  { 
    _Resetiosflags __x; 
    __x._M_mask = __mask; 
    return __x; 
  }

  template<typename _CharT, typename _Traits>
    inline basic_istream<_CharT,_Traits>& 
    operator>>(basic_istream<_CharT,_Traits>& __is, _Resetiosflags __f)
    { 
      __is.setf(ios_base::fmtflags(0), __f._M_mask); 
      return __is; 
    }

  template<typename _CharT, typename _Traits>
    inline basic_ostream<_CharT,_Traits>& 
    operator<<(basic_ostream<_CharT,_Traits>& __os, _Resetiosflags __f)
    { 
      __os.setf(ios_base::fmtflags(0), __f._M_mask); 
      return __os; 
    }


  struct _Setiosflags { ios_base::fmtflags _M_mask; };

  /**
   *  @brief  Manipulator for @c setf.
   *  @param  mask  A format flags mask.
   *
   *  Sent to a stream object, this manipulator sets the format flags
   *  to @a mask.
  */
  inline _Setiosflags 
  setiosflags(ios_base::fmtflags __mask)
  { 
    _Setiosflags __x; 
    __x._M_mask = __mask; 
    return __x; 
  }

  template<typename _CharT, typename _Traits>
    inline basic_istream<_CharT,_Traits>& 
    operator>>(basic_istream<_CharT,_Traits>& __is, _Setiosflags __f)
    { 
      __is.setf(__f._M_mask); 
      return __is; 
    }

  template<typename _CharT, typename _Traits>
    inline basic_ostream<_CharT,_Traits>& 
    operator<<(basic_ostream<_CharT,_Traits>& __os, _Setiosflags __f)
    { 
      __os.setf(__f._M_mask); 
      return __os; 
    }


  struct _Setbase { int _M_base; };

  /**
   *  @brief  Manipulator for @c setf.
   *  @param  base  A numeric base.
   *
   *  Sent to a stream object, this manipulator changes the
   *  @c ios_base::basefield flags to @c oct, @c dec, or @c hex when @a base
   *  is 8, 10, or 16, accordingly, and to 0 if @a base is any other value.
  */
  inline _Setbase 
  setbase(int __base)
  { 
    _Setbase __x; 
    __x._M_base = __base; 
    return __x; 
  }

  template<typename _CharT, typename _Traits>
    inline basic_istream<_CharT,_Traits>& 
    operator>>(basic_istream<_CharT,_Traits>& __is, _Setbase __f)
    {
      __is.setf(__f._M_base ==  8 ? ios_base::oct : 
          __f._M_base == 10 ? ios_base::dec : 
          __f._M_base == 16 ? ios_base::hex : 
          ios_base::fmtflags(0), ios_base::basefield);
      return __is; 
    }

  template<typename _CharT, typename _Traits>
    inline basic_ostream<_CharT,_Traits>& 
    operator<<(basic_ostream<_CharT,_Traits>& __os, _Setbase __f)
    {
      __os.setf(__f._M_base ==  8 ? ios_base::oct : 
        __f._M_base == 10 ? ios_base::dec : 
        __f._M_base == 16 ? ios_base::hex : 
        ios_base::fmtflags(0), ios_base::basefield);
      return __os; 
    }


  template<typename _CharT> 
    struct _Setfill { _CharT _M_c; };

  /**
   *  @brief  Manipulator for @c fill.
   *  @param  c  The new fill character.
   *
   *  Sent to a stream object, this manipulator calls @c fill(c) for that
   *  object.
  */
  template<typename _CharT> 
    inline _Setfill<_CharT> 
    setfill(_CharT __c)
    { 
      _Setfill<_CharT> __x; 
      __x._M_c = __c; 
      return __x; 
    }

  template<typename _CharT, typename _Traits>
    inline basic_istream<_CharT,_Traits>& 
    operator>>(basic_istream<_CharT,_Traits>& __is, _Setfill<_CharT> __f)
    { 
      __is.fill(__f._M_c); 
      return __is; 
    }

  template<typename _CharT, typename _Traits>
    inline basic_ostream<_CharT,_Traits>& 
    operator<<(basic_ostream<_CharT,_Traits>& __os, _Setfill<_CharT> __f)
    { 
      __os.fill(__f._M_c); 
      return __os; 
    }


  struct _Setprecision { int _M_n; };

  /**
   *  @brief  Manipulator for @c precision.
   *  @param  n  The new precision.
   *
   *  Sent to a stream object, this manipulator calls @c precision(n) for
   *  that object.
  */
  inline _Setprecision 
  setprecision(int __n)
  { 
    _Setprecision __x; 
    __x._M_n = __n; 
    return __x; 
  }

  template<typename _CharT, typename _Traits>
    inline basic_istream<_CharT,_Traits>& 
    operator>>(basic_istream<_CharT,_Traits>& __is, _Setprecision __f)
    { 
      __is.precision(__f._M_n); 
      return __is; 
    }

  template<typename _CharT, typename _Traits>
    inline basic_ostream<_CharT,_Traits>& 
    operator<<(basic_ostream<_CharT,_Traits>& __os, _Setprecision __f)
    { 
      __os.precision(__f._M_n); 
      return __os; 
    }


  struct _Setw { int _M_n; };

  /**
   *  @brief  Manipulator for @c width.
   *  @param  n  The new width.
   *
   *  Sent to a stream object, this manipulator calls @c width(n) for
   *  that object.
  */
  inline _Setw 
  setw(int __n)
  { 
    _Setw __x; 
    __x._M_n = __n; 
    return __x; 
  }

  template<typename _CharT, typename _Traits>
    inline basic_istream<_CharT,_Traits>& 
    operator>>(basic_istream<_CharT,_Traits>& __is, _Setw __f)
    { 
      __is.width(__f._M_n); 
      return __is; 
    }

  template<typename _CharT, typename _Traits>
    inline basic_ostream<_CharT,_Traits>& 
    operator<<(basic_ostream<_CharT,_Traits>& __os, _Setw __f)
    { 
      __os.width(__f._M_n); 
      return __os; 
    }
  • 8
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值