询问者
使用控制符的输出,改变默认输出格式后不改回默认输出格式会有什么影响?

常规讨论
-
我是windows7,vc2008速成版c++
#include <iomanip>
void main()
{double amount=22.0/7;
std::cout<<amount<<std::endl;
std::cout<<setiosflags(ios::fixed);
std::cout<<setprecision(8)<<amount<<std::endl;
std::cout<<setiosflags(ios::scientific)<<amount<<std::endl;//使用控制符的输出后,没改回小数点后五位显示
}对windows7,vs2008分别有什么影响?另外setfill,(ios::left)没改回有何影响?如果没有哪为什么?
全部回复
-
<blockquote>论坛有版主吗?怎么没高人回答?谢谢</blockquote>
我不是斑竹,我瞎说两句。
老规矩,先看CRT 源代码:
1)
_MRTIMP2 _Smanip<streamsize> __cdecl setprecision(streamsize prec)
{ // manipulator to set precision
return (_Smanip<streamsize>(&spfun, prec));
}
2)
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
};
3)
static void __cdecl spfun(ios_base& iostr, streamsize prec)
{ // set precision
iostr.precision(prec);
}
你代码里面设定小数点精度,调用的是1), 1) 又调用了2), 2) 最终调用了3)
其实也就是调用了ios_base 类的precision方法设定了小数点精度:
streamsize __CLR_OR_THIS_CALL precision(streamsize _Newprecision)
{ // set precision to argument
streamsize _Oldprecision = _Prec;
_Prec = _Newprecision;
return (_Oldprecision);
}
代码中的streamsize 没啥神奇的,定义就是: typedef _int64 streamsize;
由:
http://www.cplusplus.com/reference/iostream/ios_base/
可知,
The ios_base class is designed to be a base class for all of the hierarchy of stream classes, describing the most basic part of a stream, which is common to all stream objects. Therefore, it is not designed to be directly instantiated into objects.
因此,你不该回precision,只会影响到你当前这个输出流对象小数点显示精度(8位,而不是默认5位)。 对CRT 的其它没有任何影响。
证明完毕。
-
CRT MFC ATL 的源代码都在你Visual Studio 9.0\VC\ 文件夹内。 这些库代码一直都是开源的(VC6 都有),只是有很多人不知道罢了。
MSDN 文档就一个版本,只要定期更新文档,或者访问 msdn.microsoft.com/library 就好了,没啥区别。
你要想研究一下CRT 源代码,推荐你安装一下Source Insight, 这个工具真是太好了。
C++ 垃不垃圾我不知道,反正我觉得说这种话的人挺垃圾的。
C++ 不是类型安全的, 微软实现的CRT 库以前也不是安全的, 缓冲区溢出的隐患导出都是。 但是现在微软已经对CRT 库改正很多了。 C/C++ 之所以快, 一个很大的原因就是可以直接操作内存,你要知道内存操作是效率最高的计算方式。 高级的语言,比如Java C# VB.NET 都是类型安全的,但他们也丧失了直接操作内存的特性。 -
Source Insight, 这个工具是中文版吗?
1) Source Insight 是第三方公司的源代码分析工具, UI 做得非常棒。 如果你需要分析较大规模的源代码项目Source Insight 是一个非常好的工具, 这个工具暂时没发现有中文版本;
我还不知道CRT 源代码是有什么作用呀?请告诉我好吗。
CRT 源代码和MSDN是何关系呢?MSDN有多大呢?会有病毒吗?谢谢。
2) CRT 就是C/C++ RunTime 的意思, 你平时写程序用到的itoa printf scanf getchar 等函数都是CRT 函数库中的成员, 你提问的问题的答案就在CRT 源代码中;
3) MSDN 是Microsoft Developer Network 的简称, 主要是一些微软开发相关的文档, 由微软定期更新, 最新版本的MSDN 可以在msdn.microsoft.com/library 查询。 一般为了开发、检索方便都会在本机安装一个本地版本; 最新版本的MSDN 完全安装大概有1.8G 作为微软平台的开发人员, 没事儿翻翻MSDN 挺好的;
只要是正当渠道获取的微软产品以及文档都不会含有病毒