C++学习-IO流控制iomanip(14)

本文介绍了C++中使用iomanip进行输入输出流控制的方法,包括不同进制的设置、填充字符、精度控制、宽度设置等,并详细解释了如何通过设置格式标志来调整输出样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作者:gzshun. 原创作品,转载请标明出处!
来源:http://blog.youkuaiyun.com/gzshun


在C++输入输出流控制中,就把话语权交给iomanip吧。
以下列出一些比较常用的设置方法:

包含头文件:#include <iomanip>
dec 十进制 dec(c++) == %d(c)
hex 十六进制 oct(c++) == %o(c)
oct 八进制 hex(c++) == %x(c)
setfill(c) 填充字符为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) 16进制数大写输出
setiosflags(ios::lowercase) 16进制小写输出
setiosflags(ios::showpoint) 强制显示小数点
setiosflags(ios::showpos) 强制显示符号(+/-)

在设置mask有两个方法:setiosflags和setf。

这些用法看头文件就很清楚了,主要在:ios_base.h和iomanip中。

ios_base.h:

/**
 *  @brief  Access to format flags.
 *  @return  The format control flags for both input and output.
*/
fmtflags
flags() const
{ return _M_flags; }

/**
 *  @brief  Setting new format flags all at once.
 *  @param  fmtfl  The new flags to set.
 *  @return  The previous format control flags.
 *
 *  This function overwrites all the format flags with @a fmtfl.
*/
fmtflags
flags(fmtflags __fmtfl)
{
  fmtflags __old = _M_flags;
  _M_flags = __fmtfl;
  return __old;
}

/**
 *  @brief  Setting new format flags.
 *  @param  fmtfl  Additional flags to set.
 *  @return  The previous format control flags.
 *
 *  This function sets additional flags in format control.  Flags that
 *  were previously set remain set.
*/
fmtflags
setf(fmtflags __fmtfl)
{
  fmtflags __old = _M_flags;
  _M_flags |= __fmtfl;
  return __old;
}

/**
 *  @brief  Setting new format flags.
 *  @param  fmtfl  Additional flags to set.
 *  @param  mask  The flags mask for @a fmtfl.
 *  @return  The previous format control flags.
 *
 *  This function clears @a mask in the format flags, then sets
 *  @a fmtfl @c & @a mask.  An example mask is @c ios_base::adjustfield.
*/
fmtflags
setf(fmtflags __fmtfl, fmtflags __mask)
{
  fmtflags __old = _M_flags;
  _M_flags &= ~__mask;
  _M_flags |= (__fmtfl & __mask);
  return __old;
}

/**
 *  @brief  Clearing format flags.
 *  @param  mask  The flags to unset.
 *
 *  This function clears @a mask in the format flags.
*/
void
unsetf(fmtflags __mask)
{ _M_flags &= ~__mask; }

/**
 *  @brief  Flags access.
 *  @return  The precision to generate on certain output operations.
 *
 *  Be careful if you try to give a definition of "precision" here; see
 *  DR 189.
*/
streamsize
precision() const
{ return _M_precision; }

/**
 *  @brief  Changing flags.
 *  @param  prec  The new precision value.
 *  @return  The previous value of precision().
*/
streamsize
precision(streamsize __prec)
{
  streamsize __old = _M_precision;
  _M_precision = __prec;
  return __old;
}

/**
 *  @brief  Flags access.
 *  @return  The minimum field width to generate on output operations.
 *
 *  "Minimum field width" refers to the number of characters.
*/
streamsize
width() const
{ return _M_width; }

/**
 *  @brief  Changing flags.
 *  @param  wide  The new width value.
 *  @return  The previous value of width().
*/
streamsize
width(streamsize __wide)
{
  streamsize __old = _M_width;
  _M_width = __wide;
  return __old;
}

// [27.4.2.4] ios_base static members
/**
 *  @brief  Interaction with the standard C I/O objects.
 *  @param  sync  Whether to synchronize or not.
 *  @return  True if the standard streams were previously synchronized.
 *
 *  The synchronization referred to is @e only that between the standard
 *  C facilities (e.g., stdout) and the standard C++ objects (e.g.,
 *  cout).  User-declared streams are unaffected.  See
 *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch28s02.html
*/
static bool
sync_with_stdio(bool __sync = true);

// [27.4.2.3] ios_base locale functions
/**
 *  @brief  Setting a new locale.
 *  @param  loc  The new locale.
 *  @return  The previous locale.
 *
 *  Sets the new locale for this stream, and then invokes each callback
 *  with imbue_event.
*/
locale
imbue(const locale& __loc);

/**
 *  @brief  Locale access
 *  @return  A copy of the current locale.
 *
 *  If @c imbue(loc) has previously been called, then this function
 *  returns @c loc.  Otherwise, it returns a copy of @c std::locale(),
 *  the global C++ locale.
*/
locale
getloc() const
{ return _M_ios_locale; }

/**
 *  @brief  Locale access
 *  @return  A reference to the current locale.
 *
 *  Like getloc above, but returns a reference instead of
 *  generating a copy.
*/
const locale&
_M_getloc() const
{ return _M_ios_locale; }

// [27.4.2.5] ios_base storage functions
/**
 *  @brief  Access to unique indices.
 *  @return  An integer different from all previous calls.
 *
 *  This function returns a unique integer every time it is called.  It
 *  can be used for any purpose, but is primarily intended to be a unique
 *  index for the iword and pword functions.  The expectation is that an
 *  application calls xalloc in order to obtain an index in the iword and
 *  pword arrays that can be used without fear of conflict.
 *
 *  The implementation maintains a static variable that is incremented and
 *  returned on each invocation.  xalloc is guaranteed to return an index
 *  that is safe to use in the iword and pword arrays.
*/
static int
xalloc() throw();

/**
 *  @brief  Access to integer array.
 *  @param  __ix  Index into the array.
 *  @return  A reference to an integer associated with the index.
 *
 *  The iword function provides access to an array of integers that can be
 *  used for any purpose.  The array grows as required to hold the
 *  supplied index.  All integers in the array are initialized to 0.
 *
 *  The implementation reserves several indices.  You should use xalloc to
 *  obtain an index that is safe to use.  Also note that since the array
 *  can grow dynamically, it is not safe to hold onto the reference.
*/
long&
iword(int __ix)
{
  _Words& __word = (__ix < _M_word_size)
	? _M_word[__ix] : _M_grow_words(__ix, true);
  return __word._M_iword;
}

/**
 *  @brief  Access to void pointer array.
 *  @param  __ix  Index into the array.
 *  @return  A reference to a void* associated with the index.
 *
 *  The pword function provides access to an array of pointers that can be
 *  used for any purpose.  The array grows as required to hold the
 *  supplied index.  All pointers in the array are initialized to 0.
 *
 *  The implementation reserves several indices.  You should use xalloc to
 *  obtain an index that is safe to use.  Also note that since the array
 *  can grow dynamically, it is not safe to hold onto the reference.
*/
void*&
pword(int __ix)
{
  _Words& __word = (__ix < _M_word_size)
	? _M_word[__ix] : _M_grow_words(__ix, false);
  return __word._M_pword;
}

// Destructor
/**
 *  Invokes each callback with erase_event.  Destroys local storage.
 *
 *  Note that the ios_base object for the standard streams never gets
 *  destroyed.  As a result, any callbacks registered with the standard
 *  streams will not get invoked with erase_event (unless copyfmt is
 *  used).
*/
virtual ~ios_base();

protected:
ios_base();

// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 50.  Copy constructor and assignment operator of ios_base
private:
ios_base(const ios_base&);

ios_base&
operator=(const ios_base&);
};

// [27.4.5.1] fmtflags manipulators
/// Calls base.setf(ios_base::boolalpha).
inline ios_base&
boolalpha(ios_base& __base)
{
__base.setf(ios_base::boolalpha);
return __base;
}

/// Calls base.unsetf(ios_base::boolalpha).
inline ios_base&
noboolalpha(ios_base& __base)
{
__base.unsetf(ios_base::boolalpha);
return __base;
}

/// Calls base.setf(ios_base::showbase).
inline ios_base&
showbase(ios_base& __base)
{
__base.setf(ios_base::showbase);
return __base;
}

/// Calls base.unsetf(ios_base::showbase).
inline ios_base&
noshowbase(ios_base& __base)
{
__base.unsetf(ios_base::showbase);
return __base;
}

/// Calls base.setf(ios_base::showpoint).
inline ios_base&
showpoint(ios_base& __base)
{
__base.setf(ios_base::showpoint);
return __base;
}

/// Calls base.unsetf(ios_base::showpoint).
inline ios_base&
noshowpoint(ios_base& __base)
{
__base.unsetf(ios_base::showpoint);
return __base;
}

/// Calls base.setf(ios_base::showpos).
inline ios_base&
showpos(ios_base& __base)
{
__base.setf(ios_base::showpos);
return __base;
}

/// Calls base.unsetf(ios_base::showpos).
inline ios_base&
noshowpos(ios_base& __base)
{
__base.unsetf(ios_base::showpos);
return __base;
}

/// Calls base.setf(ios_base::skipws).
inline ios_base&
skipws(ios_base& __base)
{
__base.setf(ios_base::skipws);
return __base;
}

/// Calls base.unsetf(ios_base::skipws).
inline ios_base&
noskipws(ios_base& __base)
{
__base.unsetf(ios_base::skipws);
return __base;
}

/// Calls base.setf(ios_base::uppercase).
inline ios_base&
uppercase(ios_base& __base)
{
__base.setf(ios_base::uppercase);
return __base;
}

/// Calls base.unsetf(ios_base::uppercase).
inline ios_base&
nouppercase(ios_base& __base)
{
__base.unsetf(ios_base::uppercase);
return __base;
}

/// Calls base.setf(ios_base::unitbuf).
inline ios_base&
unitbuf(ios_base& __base)
{
 __base.setf(ios_base::unitbuf);
 return __base;
}

/// Calls base.unsetf(ios_base::unitbuf).
inline ios_base&
nounitbuf(ios_base& __base)
{
 __base.unsetf(ios_base::unitbuf);
 return __base;
}

// [27.4.5.2] adjustfield manipulators
/// Calls base.setf(ios_base::internal, ios_base::adjustfield).
inline ios_base&
internal(ios_base& __base)
{
 __base.setf(ios_base::internal, ios_base::adjustfield);
 return __base;
}

/// Calls base.setf(ios_base::left, ios_base::adjustfield).
inline ios_base&
left(ios_base& __base)
{
__base.setf(ios_base::left, ios_base::adjustfield);
return __base;
}

/// Calls base.setf(ios_base::right, ios_base::adjustfield).
inline ios_base&
right(ios_base& __base)
{
__base.setf(ios_base::right, ios_base::adjustfield);
return __base;
}

// [27.4.5.3] basefield manipulators
/// Calls base.setf(ios_base::dec, ios_base::basefield).
inline ios_base&
dec(ios_base& __base)
{
__base.setf(ios_base::dec, ios_base::basefield);
return __base;
}

/// Calls base.setf(ios_base::hex, ios_base::basefield).
inline ios_base&
hex(ios_base& __base)
{
__base.setf(ios_base::hex, ios_base::basefield);
return __base;
}

/// Calls base.setf(ios_base::oct, ios_base::basefield).
inline ios_base&
oct(ios_base& __base)
{
__base.setf(ios_base::oct, ios_base::basefield);
return __base;
}

// [27.4.5.4] floatfield manipulators
/// Calls base.setf(ios_base::fixed, ios_base::floatfield).
inline ios_base&
fixed(ios_base& __base)
{
__base.setf(ios_base::fixed, ios_base::floatfield);
return __base;
}

/// Calls base.setf(ios_base::scientific, ios_base::floatfield).
inline ios_base&
scientific(ios_base& __base)
{
__base.setf(ios_base::scientific, ios_base::floatfield);
return __base;
}

iomanip:

/**
 *  @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; 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值