C++ 标准库之iomanip
2015-07-04 20:27
405 查看
C++ 标准库之iomanip
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() | 终止已经设置的输出格式状态,在括号中应指定内容 |
另外,科学计数法输出E与十六进制输出默认是以小写的,要换成大写需添加uppercase
而setw(n)设置宽度,若是实际宽度大于被设置的,则setw函数此时失效。
以下是测试程序:
[cpp] view
plaincopy
#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>头文件的声明:
[cpp] view
plaincopy
// 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 */
相关文章推荐
- 堆排序C++
- 自学笔记-C语言复习2015年7月4日
- C++ 中string.find() 函数的用法总结(转载)
- 【学习笔记】【C语言】typedef
- 如何用Windbg找到被catch住的C++的异常
- C++ STL关联容器 set和map学习笔记
- 关于C++ const 的全面总结
- C语言中的预处理命令
- C++ map的简单应用
- c++ stl algorithm: std::find, std::find_if
- C++ inline函数
- C++ QT中的QSound使用方法
- 国嵌C++专题1-蜕变成蝶
- 关于C++语言从磁盘里面读取浮点数据的问题
- [转载] C++ 程序员快过来围观:非常实用全面的 C++ 资源
- error C2440: “static_cast”: 无法从“void (__thiscall CPppView::* )(WPARAM,LPARAM)”转换为“LRESULT (__thiscal
- C++中枚举定义运算符
- c语言函数指针的理解与使用
- Huffman编码与解码C++程序
- c++过程