您的位置:首页 > 编程语言 > C语言/C++

C++ 标准库之iomanip

2009-10-01 19:17 330 查看
C++ 语言下

头文件:#include <iomanip>

说明:是I/O流控制头文件,就像C里面的格式化输出一样

控 制 符 作 用
dec 设置整数为十进制
hex设置整数为十六进制
oct设置整数为八进制
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() 终止已经设置的输出格式状态,在括号中应指定内容

在此需要说一下,有效位数默认是6位,即setprecision(6),即小数点前面和小数点后面加起来的位数为6个有效数字(注意会四舍五入)。

另外,科学计数法输出E与十六进制输出默认是以小写的,要换成大写需添加uppercase

而setw(n)设置宽度,若是实际宽度大于被设置的,则setw函数此时失效。

以下是测试程序:

#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 ;
}


结果如下:




相关课件,更多的内容在课件里面,收集于网络:下载一

以下是<iomanip>头文件的声明:

// iomanip standard header
#pragma once
#ifndef _IOMANIP_
#define _IOMANIP_
#ifndef RC_INVOKED
#include <istream>
#ifdef _MSC_VER
 #pragma pack(push,_CRT_PACKING)
 #pragma warning(push,3)
#endif  /* _MSC_VER */
_STD_BEGIN
		// TEMPLATE STRUCT _Fillobj
template<class _Elem>
	struct _Fillobj
	{	// store fill character
	_Fillobj(_Elem _Ch)
		: _Fill(_Ch)
		{	// construct from fill character
		}
	_Elem _Fill;	// the fill character
	};
		// TEMPLATE FUNCTION setfill
template<class _Elem> inline
	_Fillobj<_Elem> __CLRCALL_OR_CDECL setfill(_Elem _Ch)
	{	// return a _Fillobj manipulator
	return (_Fillobj<_Elem>(_Ch));
	}
template<class _Elem,
	class _Traits> inline
	basic_istream<_Elem, _Traits>&
		__CLRCALL_OR_CDECL operator>>(basic_istream<_Elem, _Traits>& _Istr,
			const _Fillobj<_Elem>& _Manip)
	{	// set fill character in input stream
	_Istr.fill(_Manip._Fill);
	return (_Istr);
	}
template<class _Elem,
	class _Traits> inline
	basic_ostream<_Elem, _Traits>&
		__CLRCALL_OR_CDECL operator<<(basic_ostream<_Elem, _Traits>& _Ostr,
			const _Fillobj<_Elem>& _Manip)
	{	// set fill character in output stream
	_Ostr.fill(_Manip._Fill);
	return (_Ostr);
	}
		// TEMPLATE STRUCT _Smanip
template<class _Arg>
	struct _Smanip
	{	// store function pointer and argument value
	_Smanip(void (__cdecl *_Left)(ios_base&, _Arg), _Arg _Val)
		: _Pfun(_Left), _Manarg(_Val)
		{	// construct from function pointer and argument value
		}
	void (__cdecl *_Pfun)(ios_base&, _Arg);	// the function pointer
	_Arg _Manarg;	// the argument value
	};
template<class _Elem,
	class _Traits,
	class _Arg> inline
	basic_istream<_Elem, _Traits>& __CLRCALL_OR_CDECL operator>>(
		basic_istream<_Elem, _Traits>& _Istr, const _Smanip<_Arg>& _Manip)
	{	// extract by calling function with input stream and argument
	(*_Manip._Pfun)(_Istr, _Manip._Manarg);
	return (_Istr);
	}
template<class _Elem,
	class _Traits,
	class _Arg> inline
	basic_ostream<_Elem, _Traits>& __CLRCALL_OR_CDECL operator<<(
		basic_ostream<_Elem, _Traits>& _Ostr, const _Smanip<_Arg>& _Manip)
	{	// insert by calling function with output stream and argument
	(*_Manip._Pfun)(_Ostr, _Manip._Manarg);
	return (_Ostr);
	}
		// INSTANTIATIONS
_MRTIMP2 _Smanip<ios_base::fmtflags> __cdecl resetiosflags(ios_base::fmtflags);
_MRTIMP2 _Smanip<ios_base::fmtflags> __cdecl setiosflags(ios_base::fmtflags);
_MRTIMP2 _Smanip<int> __cdecl setbase(int);
_MRTIMP2 _Smanip<streamsize> __cdecl setprecision(streamsize);
_MRTIMP2 _Smanip<streamsize> __cdecl setw(streamsize);
_STD_END
#ifdef _MSC_VER
 #pragma warning(pop)
 #pragma pack(pop)
#endif  /* _MSC_VER */
#endif /* RC_INVOKED */
#endif /* _IOMANIP_ */
/*
 * Copyright (c) 1992-2006 by P.J. Plauger.  ALL RIGHTS RESERVED.
 * Consult your license regarding permissions and restrictions.
 V5.02:0009 */
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: