C++标准库之iomanip

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C++标准库之iomanip
C++ 标准库之iomanip
istream & istream::get(char *, int, char = '\n');
istream & istream::getline(char *, int, char = '\n');
作⽤: 从⽂本中提取指定个数的字符串, 并在串数组末尾添加⼀个空字符.
区别: get() 不从流中提取终⽌字符, 终⽌字符仍在输⼊流中. getline() 从流中提取终⽌字符, 但终⽌字符被丢弃.
C++ 语⾔下
头⽂件:#include <iomanip>
说明:是I/O流控制头⽂件,就像C⾥⾯的格式化输出⼀样
编号⽅法和描述
1 - 它⽤于设置格式标志。

2 - ⽤于重置格式标志。

3 - 它⽤于设置basefield标志。

4 - 它⽤于设置填充字符
5 - 它⽤于设置⼩数精度。

6 - 它⽤于设置字段宽度。

7 - 它⽤于获得货币值。

8 - 它⽤来设置计算货币的值。

9 - 它⽤于获取⽇期和时间。

10 - 它⽤于放置(或设置)⽇期和时间。

控制符作⽤
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>
#include <fstream>
int main()
{
// 前缀0表⽰⼋进制前缀0x表⽰⼗六进制不带前缀表⽰⼗进制
int a = 123;
double pi = 22.0/7.0;
// setbase(n) 设置整数为n进制(n=8,10,16)
// oct ⼋进制 dec ⼗进制 hex ⼗六进制
// setiosflags(ios::showbase) 显⽰进制的前缀
// 数值默认⼗进制显⽰输出
std::cout << a << std::endl;
std::cout << "oct: " << std::showbase << std::setbase(8) << a << "" << std::oct << a << std::endl;
std::cout << "dec: " << std::showbase << std::setbase(10) << a << "" << std::dec << a << std::endl;
std::cout << "hex: " << std::showbase << std::setbase(16) << a << "" << std::hex << a << std::endl;
// setprecision(n) 设置浮点数的有效数字为n位
// 有效位数默认是6位,即setprecision(6),即⼩数点前⾯和⼩数点后⾯加起来的位数为6个有效数字(注意会四舍五⼊)
std::cout << pi << std::endl;
std::cout << std::setprecision(12) << pi << std::endl;
// setfill(n) 设置字符填充,c可以是字符常或字符变量
// setw(n) 设置字段宽度为n位, 若是实际宽度⼤于被设置的,则setw函数此时失效, 只针对其后的第⼀个输出项有效
// setiosflags(ios::left) 输出左对齐
// setiosflags(ios::right) 输出右对齐默认右对齐
std::cout << std::setfill('*') << std::setw(20) << std::setprecision(12) << pi << std::endl;
std::cout << std::setfill('*') << std::setw(20) << std::setprecision(12) << std::right << pi << std::endl;
std::cout << std::setfill('*') << std::setw(20) << std::setprecision(12) << std::left << pi << std::endl;
// setiosflags(ios::fixed) 设置浮点数以固定的⼩数位数显⽰
std::cout << std::fixed << std::setprecision(12) << pi << std::endl;
// setiosflags(ios::scientific) 设置浮点数以科学计数法表⽰科学计数法输出E与⼗六进制输出默认是以⼩写的,要换成⼤写需添加uppercase
std::cout << std::scientific << std::setprecision(12) << pi << std::endl;
std::cout << std::scientific << std::uppercase << std::setprecision(12) << pi << std::endl;
// resetiosflags() 终⽌已经设置的输出格式状态,在括号中应指定内容
std::cout << std::setiosflags(std::ios::scientific) << std::setprecision(12) << pi << "" << std::resetiosflags(std::ios::scientific) << pi << std::endl; system("pause");
return0;
}
运⾏结果:
msvc 11.0 Visual Studio 2012 中iomanip头⽂件内容:
// iomanip standard header
#pragma once
#ifndef _IOMANIP_
#define _IOMANIP_
#ifndef RC_INVOKED
#include <istream>
#include <type_traits>
#include <xlocmon>
#include <xloctime>
#pragma pack(push,_CRT_PACKING)
#pragma warning(push,3)
#pragma push_macro("new")
#undef new
_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> setfill(_Elem _Ch)
{ // return a _Fillobj manipulator
return (_Fillobj<_Elem>(_Ch));
}
template<class _Elem,
class _Traits,
class _Elem2> inline
basic_istream<_Elem, _Traits>&
operator>>(basic_istream<_Elem, _Traits>& _Istr,
const _Fillobj<_Elem2>& _Manip)
{ // set fill character in input stream
static_assert((is_same<_Elem, _Elem2>::value),
"wrong character type for setfill");
_Istr.fill(_Manip._Fill);
return (_Istr);
}
template<class _Elem,
class _Traits,
class _Elem2> inline
basic_ostream<_Elem, _Traits>&
operator<<(basic_ostream<_Elem, _Traits>& _Ostr,
const _Fillobj<_Elem2>& _Manip)
{ // set fill character in output stream
static_assert((is_same<_Elem, _Elem2>::value),
"wrong character type for setfill");
_Ostr.fill(_Manip._Fill);
return (_Ostr);
}
#if _HAS_CPP0X
// TEMPLATE STRUCT _Monobj
template<class _Money>
struct _Monobj
{ // store reference to monetary amount
_Monobj(_Money& _Val_arg, bool _Intl_arg)
: _Val(_Val_arg), _Intl(_Intl_arg)
{ // construct from monetary amount reference and int'l flag }
_Money& _Val; // the monetary amount reference
bool _Intl; // international flag
private:
_Monobj& operator=(const _Monobj&);
};
// TEMPLATE FUNCTION get_money
template<class _Money> inline
_Monobj<_Money> get_money(_Money& _Val_arg,
bool _Intl_arg = false)
{ // return a _Monobj manipulator
return (_Monobj<_Money>(_Val_arg, _Intl_arg));
}
template<class _Elem,
class _Traits,
class _Money> inline
basic_istream<_Elem, _Traits>&
operator>>(basic_istream<_Elem, _Traits>& _Istr,
const _Monobj<_Money>& _Manip)
{ // get monetary amount from input stream
typedef basic_istream<_Elem, _Traits> _Myis;
typedef istreambuf_iterator<_Elem, _Traits> _Iter;
typedef money_get<_Elem, _Iter> _Mymget;
ios_base::iostate _State = ios_base::goodbit;
const typename _Myis::sentry _Ok(_Istr);
if (_Ok)
{ // state okay, extract monetary amount
const _Mymget& _Mget_fac = _USE(_Istr.getloc(), _Mymget); _TRY_IO_BEGIN
_Mget_fac.get(_Iter(_Istr.rdbuf()), _Iter(0), _Manip._Intl,
_Istr, _State, _Manip._Val);
_CATCH_IO_(_Istr)
}
_Istr.setstate(_State);
return (_Istr);
}
// TEMPLATE FUNCTION put_money
template<class _Money> inline
_Monobj<const _Money>
put_money(const _Money& _Val_arg,
bool _Intl_arg = false)
{ // return a _Monobj manipulator
return (_Monobj<const _Money>(_Val_arg, _Intl_arg));
}
template<class _Elem,
class _Traits,
class _Money> inline
basic_ostream<_Elem, _Traits>&
operator<<(basic_ostream<_Elem, _Traits>& _Ostr,
const _Monobj<_Money>& _Manip)
{ // put monetary amount to output stream
typedef basic_ostream<_Elem, _Traits> _Myos;
typedef ostreambuf_iterator<_Elem, _Traits> _Iter;
typedef money_put<_Elem, _Iter> _Mymput;
ios_base::iostate _State = ios_base::goodbit;
const typename _Myos::sentry _Ok(_Ostr);
if (_Ok)
{ // state okay, insert monetary amount
const _Mymput& _Mput_fac = _USE(_Ostr.getloc(), _Mymput); _TRY_IO_BEGIN
if (_Mput_fac.put(_Iter(_Ostr.rdbuf()), _Manip._Intl,
_Ostr, _Ostr.fill(), _Manip._Val).failed())
_State |= ios_base::badbit;
_CATCH_IO_(_Ostr)
}
_Ostr.setstate(_State);
return (_Ostr);
}
// TEMPLATE STRUCT _Timeobj
template<class _Elem>
struct _Timeobj
{ // store reference to tm object and format
_Timeobj(struct tm *_Tptr_arg, const _Elem *_Fmt_arg)
: _Tptr(_Tptr_arg), _Fmtfirst(_Fmt_arg)
{ // construct from tm pointer and format pointer
for (_Fmtlast = _Fmtfirst; *_Fmtlast != 0; ++_Fmtlast)
; // find end of format string
}
struct tm *_Tptr; // the tm struct pointer
const _Elem *_Fmtfirst; // format string start
const _Elem *_Fmtlast; // format string end
};
// TEMPLATE FUNCTION get_time
template<class _Elem> inline
_Timeobj<_Elem>
get_time(struct tm *_Tptr_arg, const _Elem *_Fmt_arg)
{ // return a _Timeobj manipulator
return (_Timeobj<_Elem>(_Tptr_arg, _Fmt_arg));
}
template<class _Elem,
class _Traits,
class _Elem2> inline
basic_istream<_Elem, _Traits>&
operator>>(basic_istream<_Elem, _Traits>& _Istr,
const _Timeobj<_Elem2>& _Manip)
{ // get time information from input stream
typedef basic_istream<_Elem, _Traits> _Myis;
typedef istreambuf_iterator<_Elem, _Traits> _Iter;
typedef time_get<_Elem2, _Iter> _Mytget;
static_assert((is_same<_Elem, _Elem2>::value),
"wrong character type for get_time");
ios_base::iostate _State = ios_base::goodbit;
const typename _Myis::sentry _Ok(_Istr);
if (_Ok)
{ // state okay, extract time amounts
const _Mytget& _Tget_fac = _USE(_Istr.getloc(), _Mytget);
_TRY_IO_BEGIN
_Tget_fac.get(_Iter(_Istr.rdbuf()), _Iter(0), _Istr, _State,
_Manip._Tptr, _Manip._Fmtfirst, _Manip._Fmtlast);
_CATCH_IO_(_Istr)
}
_Istr.setstate(_State);
return (_Istr);
}
// TEMPLATE FUNCTION put_time
template<class _Elem> inline
_Timeobj<_Elem>
put_time(struct tm *_Tptr_arg, const _Elem *_Fmt_arg)
{ // return a _Timeobj manipulator
return (_Timeobj<_Elem>(_Tptr_arg, _Fmt_arg));
}
template<class _Elem,
class _Traits,
class _Elem2> inline
basic_ostream<_Elem, _Traits>&
operator<<(basic_ostream<_Elem, _Traits>& _Ostr,
const _Timeobj<_Elem2>& _Manip)
{ // put time information to output stream
typedef basic_ostream<_Elem, _Traits> _Myos;
typedef ostreambuf_iterator<_Elem, _Traits> _Iter;
typedef time_put<_Elem2, _Iter> _Mytput;
static_assert((is_same<_Elem, _Elem2>::value),
"wrong character type for put_time");
ios_base::iostate _State = ios_base::goodbit;
const typename _Myos::sentry _Ok(_Ostr);
if (_Ok)
{ // state okay, insert monetary amount
const _Mytput& _Tput_fac = _USE(_Ostr.getloc(), _Mytput);
_TRY_IO_BEGIN
if (_Tput_fac.put(_Iter(_Ostr.rdbuf()), _Ostr, _Ostr.fill(),
_Manip._Tptr, _Manip._Fmtfirst, _Manip._Fmtlast).failed())
_State |= ios_base::badbit;
_CATCH_IO_(_Ostr)
}
_Ostr.setstate(_State);
return (_Ostr);
}
#endif /* _HAS_CPP0X */
// 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>& 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>& 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
#pragma pop_macro("new")
#pragma warning(pop)
#pragma pack(pop)
#endif /* RC_INVOKED */
#endif /* _IOMANIP_ */
/*
* Copyright (c) 1992-2012 by P.J. Plauger. ALL RIGHTS RESERVED.
* Consult your license regarding permissions and restrictions.
V6.00:0009 */
如何确定VS编译器版本
_MSC_VER是MSVC编译器的内置宏,定义了编译器的版本,_MSC_VER 值对应版本关系
MSVC++ 11.0 _MSC_VER = 1700 (Visual Studio 2012)
MSVC++ 10.0 _MSC_VER = 1600 (Visual Studio 2010)
MSVC++ 9.0 _MSC_VER = 1500 (Visual Studio 2008)
MSVC++ 8.0 _MSC_VER = 1400 (Visual Studio 2005)
MSVC++ 7.1 _MSC_VER = 1310 (Visual Studio 2003)
MSVC++ 7.0 _MSC_VER = 1300 (Visual Studio 2002)
MSVC++ 6.0 _MSC_VER = 1200
MSVC++ 5.0 _MSC_VER = 1100
example:
#if (_MSC_VER == 1300) //vc7
#import "acax16ENU.tlb" no_implementation raw_interfaces_only named_guids
#elif (_MSC_VER == 1200) //vc6
#import "acad.tlb" no_implementation raw_interfaces_only named_guids
#elif (_MSC_VER == 1400) //vc8
#import "acax17ENU.tlb" no_implementation raw_interfaces_only named_guids
#elif (_MSC_VER == 1500) //vc9
#import "acax18ENU.tlb" no_implementation raw_interfaces_only named_guids
#endif
在程序中加⼊_MSC_VER宏可以根据编译器版本让编译器选择性地编译⼀段程序。

例如⼀个版本编译器产⽣的lib⽂件可能不能被另⼀个版本的编译器调⽤,那么在开发应⽤程序的时候,在该程序的lib调⽤库中放⼊多个版本编译器产⽣的lib⽂件。

在程序中加⼊_MSC_VER宏,编译器就能够在调⽤的时根据其版本⾃动选择可以链接的lib库版本,如下所⽰。

#if _MSC_VER >= 1400 // for vc8, or vc9
#ifdef _DEBUG
#pragma comment( lib, "SomeLib-vc8-d.lib" )
#else if
#pragma comment( lib, "SomeLib-vc8-r.lib" )
#endif
#else if _MSC_VER >= 1310 // for vc71
#ifdef _DEBUG
#pragma comment( lib, "SomeLib-vc71-d.lib" )
#else if
#pragma comment( lib, "SomeLib-vc71-r.lib" )
#endif
#else if _MSC_VER >=1200 // for vc6
#ifdef _DEBUG
#pragma comment( lib, "SomeLib-vc6-d.lib" )
#else if
#pragma comment( lib, "SomeLib-vc6-r.lib" )
#endif
#endif。

相关文档
最新文档