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 | setiosflags - 它用于设置格式标志。 |
| 2 | resetiosflags - 用于重置格式标志。 |
| 3 | setbase - 它用于设置basefield标志。 |
| 4 | setfill - 它用于设置填充字符 |
| 5 | setprecision - 它用于设置小数精度。 |
| 6 | setw - 它用于设置字段宽度。 |
| 7 | get_money - 它用于获得货币值。 |
| 8 | put_money - 它用来设置计算货币的值。 |
| 9 | get_time - 它用于获取日期和时间。 |
| 10 | put_time - 它用于放置(或设置)日期和时间。 |
| 控 制 符 | 作 用 |
| 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 = ;
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() << a << " " << std::oct << a << std::endl;
std::cout << "dec: " << std::showbase << std::setbase() << a << " " << std::dec << a << std::endl;
std::cout << "hex: " << std::showbase << std::setbase() << a << " " << std::hex << a << std::endl; // setprecision(n) 设置浮点数的有效数字为n位
// 有效位数默认是6位,即setprecision(6),即小数点前面和小数点后面加起来的位数为6个有效数字(注意会四舍五入)
std::cout << pi << std::endl;
std::cout << std::setprecision() << pi << std::endl; // setfill(n) 设置字符填充,c可以是字符常或字符变量
// setw(n) 设置字段宽度为n位, 若是实际宽度大于被设置的,则setw函数此时失效, 只针对其后的第一个输出项有效
// setiosflags(ios::left) 输出左对齐
// setiosflags(ios::right) 输出右对齐 默认右对齐
std::cout << std::setfill('*') << std::setw() << std::setprecision() << pi << std::endl;
std::cout << std::setfill('*') << std::setw() << std::setprecision() << std::right << pi << std::endl;
std::cout << std::setfill('*') << std::setw() << std::setprecision() << std::left << pi << std::endl; // setiosflags(ios::fixed) 设置浮点数以固定的小数位数显示
std::cout << std::fixed << std::setprecision() << pi << std::endl; // setiosflags(ios::scientific) 设置浮点数以科学计数法表示 科学计数法输出E与十六进制输出默认是以小写的,要换成大写需添加uppercase
std::cout << std::scientific << std::setprecision() << pi << std::endl;
std::cout << std::scientific << std::uppercase << std::setprecision() << pi << std::endl; // resetiosflags() 终止已经设置的输出格式状态,在括号中应指定内容
std::cout << std::setiosflags(std::ios::scientific) << std::setprecision() << pi << " " << std::resetiosflags(std::ios::scientific) << pi << std::endl; system("pause");
return ;
}
运行结果:

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(), _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 != ; ++_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(), _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
C++ 标准库之iomanip的更多相关文章
- C++ 标准库之 iomanip 、操作符 ios::fixed 以及 setprecision 使用的惨痛教训经验总结
本菜鸡自从退役之后就再也没怎么敲过 C++ 代码,在 C++ 语言下,求解关于浮点数类型的问题时,之前有碰到类似的情况,但是似乎都没有卡这块的数据,基本上用一个 setprecision 函数保留几位 ...
- 【转】C++标准库和标准模板库
C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标准头文件中定义.在C++开发中,要尽可能地利用标准库完成.这样做的直接好处包括:(1)成本:已经作为标准提供,何苦再花费 ...
- c++标准库和stl关系
C++标准库的所有头文件都没有扩展名.C++标准库的内容总共在50个标准头文件中定义,其中18个提供了C库的功能. <cname>形式的标准头文件[ <complex>例外]其 ...
- STL笔记(6)标准库:标准库中的排序算法
STL笔记(6)标准库:标准库中的排序算法 标准库:标准库中的排序算法The Standard Librarian: Sorting in the Standard Library Matthew A ...
- C++标准库概述 [转]
C++标准库的所有头文件都没有扩展名. C++标准库的内容总共在50个标准头文件中定义,其中18个提供了C库的功能.<cname>形式的标准头文件[<complex>例外]其内 ...
- 什么是C++标准库?
C++中的标准程序库(简称标准库)是类库和函数的集合,其使用核心语言写成.标准程序库提供若干泛型容器.函数对象.泛型字符串和流(包含交互和文件I/O),支持部分语言特性和常用的函数,如开平方根.C++ ...
- C++标准库和标准模板库
转自原文http://blog.csdn.net/sxhelijian/article/details/7552499 C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标 ...
- C++著名类库和C++标准库介绍
C++著名类库 1.C++各大有名库的介绍——C++标准库 2.C++各大有名库的介绍——准标准库Boost 3.C++各大有名库的介绍——GUI 4.C++各大有名库的介绍——网络通信 5.C++各 ...
- C++之标准库map
目录 1.成员函数 2.元素访问 3.迭代器Iterators(C++ 11) 4.容量Capacity 5.修改函数(C++ 11和C++ 17) 6.查找表Lookup 7.观察Observers ...
随机推荐
- [js高手之路] 设计模式系列课程 - DOM迭代器(2)
如果你对jquery比较熟悉的话,应该用过 eq, first, last, get, prev, next, siblings等过滤器和方法.本文,我们就用迭代设计模式来封装实现,类似的功能 < ...
- 用vue开发一个app(2,main.js)
昨天跟着vue的官网搭建了vue的一个脚手架,我也是第一次用VUE一切都在摸索阶段. 今天试着看下里面脚手架里面有点什么东西 先看看main.js 导入了3个模块 一个vue,一个app,还有rout ...
- [实战演练]python3使用requests模块爬取页面内容
本文摘要: 1.安装pip 2.安装requests模块 3.安装beautifulsoup4 4.requests模块浅析 + 发送请求 + 传递URL参数 + 响应内容 + 获取网页编码 + 获取 ...
- js转换字符串为数值的方法
在js读取文本框或者其他表单数据的时候获得的值是字符串类型的,比如两个文本框a和b,假设获得a的value值为11,b的value值为9 ,那么a.value要小于b.value,由于他们都是字符串形 ...
- Java虚拟中内存分块
Java虚拟机JVM(Java Virtual Machine)中内存分块 JAVA中通常分为5个区域虚拟机栈.堆.方法区.程序计数器.本地方法区.我们一般讲的是Java虚拟机管理的四个区域虚拟机栈. ...
- Springmvc学习笔记(一)
一.sprinvmvc的介绍 1.1.Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全 ...
- 初始Socket编程(python)
通信双方要有一个服务端和一个客户端,所以要分开去写代码. 所以我创建了两个py程序,第一个是服务端:iServer.py 和客户端 iClient.py 服务端: #coding:utf-8from ...
- 基于Quartz实现简单的定时发送邮件
一.什么是Quartz Quartz 是一个轻量级任务调度框架,只需要做些简单的配置就可以使用:它可以支持持久化的任务存储,即使是任务中断或服务重启后,仍可以继续运行.Quartz既可以做为独立的应用 ...
- redis C接口hiredis 简单函数使用介绍
hiredis是redis数据库的C接口,目前只能在linux下使用,几个基本的函数就可以操作redis数据库了. 函数原型:redisContext *redisConnect(const char ...
- windows下怎么解决Python双版本问题
相信大家会在windows下会遇到Python双版本问题 当我们装了Python2和Python3时我们好只能在命令栏调出最高版本的那个低版本的难道消失了吗?今天我们就解决这个问题! 1.下载 我们在 ...