`
mmdev
  • 浏览: 12914605 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多
作者:gzshun. 原创作品,转载请标明出处!
来源:http://blog.csdn.net/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; 
}


分享到:
评论

相关推荐

    C++ iomanip库代码介绍

    C++ iomanip库代码介绍

    C语言头文件 IOMANIP.H

    C语言头文件 IOMANIP.HC语言头文件 IOMANIP.HC语言头文件 IOMANIP.HC语言头文件 IOMANIP.HC语言头文件 IOMANIP.HC语言头文件 IOMANIP.HC语言头文件 IOMANIP.HC语言头文件 IOMANIP.HC语言头文件 IOMANIP.HC语言头文件...

    C++I/O描述

    并介绍了 IO 流类中的各种成员函数,对于输出部分介绍了 cout 流, put 和 write 函数,介绍了 4 种控制输出时格式的 方法,即使用控制符,成员函数, iomanip 头文件及使用标记进行设置的 setf()函数来进行输出时的...

    五子棋C++源代码经测试可用

    完整的经过测试可用的五子棋C++源代码 #include #include #include #include &lt;iomanip&gt; int a[90],b[90]; //定义全局数组 int k=0; using namespace std; class CGobang //棋子类 { private: char chSort; //...

    绿色版PocketDOS 和 绿色版TC3.0

    INTRO14 CPP - Example program from User's Guide INTRO15 CPP - Example program from User's Guide INTRO16 CPP - Example program from User's Guide INTRO17 CPP - Example program from User's Guide ...

    C和C++头文件对比一览

    C、传统 C++ #include &lt;assert.h&gt; //设定插入点 #include &lt;ctype.h&gt; //字符处理 #include &lt;errno.h&gt; //定义错误码 #include &lt;float.h&gt; //浮点数处理 #include &lt;fstream.h&gt; //文件输入/输出 #include &lt;iomanip...

    学生成绩排名管理系统-C++-课程设计报告书.doc

    淮 海 工 学 院 计算机工程学院 课程设计报告 设计名称: C++语言课程设计 姓 名: 学 号: 专业班级: 系 (院): 计算机工程学院软件工程系 设计时间: 2011.6.8~2011.6.24 设计地点: 计算机工程学院机房 "指导...

    C++数据结构.pdf

    C++数据结构 数据结构 C++ 数据结构 C/C++ 数组允许定义可存储相同类型数据项的变量,但是结构是 C++ 中另⼀种⽤户⾃定义的可⽤的数据类型,它允许您存储不同类型的数 据项。 结构⽤于表⽰⼀条记录,假设您想要跟踪...

    C++课程设计-电影院售票系统

    #include&lt;iomanip&gt; using namespace std; struct Student { string name; int rollNum; int classNum; float marks; }; void addStudent() { Student s; string fileName = "students.txt"; ofstream ...

    iomanip.h头文件使用说明

    关于iomanip.h头文件(见名称就知道了...)

    C语言头文件 IOMANIP

    C语言头文件 IOMANIPC语言头文件 IOMANIPC语言头文件 IOMANIPC语言头文件 IOMANIPC语言头文件 IOMANIPC语言头文件 IOMANIPC语言头文件 IOMANIPC语言头文件 IOMANIPC语言头文件 IOMANIPC语言头文件 IOMANIPC语言...

    小型售货机

    //--------------------Add_info.h---------------...#include&lt;iomanip&gt; #include&lt;ios&gt;//基类 #include #include #include"Add_info.h" #include"Attached_fun.h" #include"Goodclass.h" #include"Goodnode.h" #include...

    C++函数库大全

    C++常用函数库大全 C++常用函数库大全 #include &lt;assert.h&gt; //设定插入点 #include &lt;ctype.h&gt; //字符处理 #include &lt;errno.h&gt; //定义错误码 #include &lt;float.h&gt; //浮点数处理 #include &lt;fstream.h&gt; //文件...

    用C++来写香农码编码

    这是一个使用C++来编写的香农码# include # include # include&lt;iomanip.h&gt; # include class T { public: T() {} ~T(); void Create(); void Coutpxj(); void Coutk(); void Coutz(); void Print(); protected...

    c++课程设计-车票管理系统方案.doc

    } } #include&lt;iostream&gt;//数据流输入/输出 #include&lt;fstream&gt;//文件输入/输出 #include&lt;string&gt;//字符串操作 #include&lt;iomanip&gt;//参数化输入/输出 #include&lt;time.h&gt;//时间库函数 usingnamespace std; //命名空间...

    C++标准库stl

    &lt;iomanip&gt; 提供操纵程序,允许改变流的状态,从而改变输出的格式。 &lt;ios&gt; 定义iostream的基类 &lt;istream&gt; 为管理输出流缓存区的输入定义模板类 &lt;ostream&gt; 为管理输出流缓存区的输出定义模板类 &lt;sstream&gt; 支持字符...

    C++头文件大全.pdf

    输入输出流操作:iomanip、sstream 字符处理:cctype、cwctype 局部化:locale 这只是一小部分C++标准库头文件,实际上C++标准库还有更多的头文件,涵盖了各种不同的功能和特性。你可以根据具体的需求在C++标准库中...

    本人精心收集,c++头文件一览

    C/C++头文件一览 C、传统 C++ #include &lt;assert.h&gt; //设定插入点 #include &lt;ctype.h&gt; //字符处理 #include &lt;errno.h&gt; //定义错误码 #include &lt;float.h&gt; //浮点数处理 #include &lt;...

    [vip专享]c++课程设计-车票管理系统.pdf

    #include&lt;iostream&gt; //数据流输入/输出 #include&lt;fstream&gt; //文件输入/输出 #include&lt;string&gt; //字符串操作 #include&lt;iomanip&gt; //参数化输入/输出 #include&lt;time.h&gt; //时间库函数 using namespace std;...

    c++读写文件流实例程序讲解

    C++文件流: 代码如下:fstream // 文件流ifstream // 输入文件流ofstream // 输出文件流//创建一个文本文件并写入信息//同向屏幕上输出信息一样将信息输出至文件#include&lt;iomanip&gt;#include&lt;fstream&gt;void main(){...

Global site tag (gtag.js) - Google Analytics