简体   繁体   English

C ++ I / O库

[英]C++ I/O library

I tried googling this but I get different answers at different places. 我尝试使用谷歌搜索,但在不同地方得到了不同的答案。 I want to know the cases wherein one should use one of the following: 我想知道以下情况之一:

#include <stdio>
#include <cstdio>
#include <iostream>

I cannot figure out the difference since in my case all my C++ programs seem to work if I use these interchangeably. 我无法弄清楚它们之间的区别,因为如果我互换使用所有C ++程序,它们似乎都能正常工作。 That being said, iostream seems to support the stream of input and output by defining cin and cout etc. However, I maybe wrong. 话虽如此,iostream似乎通过定义cin和cout等来支持输入和输出流。但是,我可能错了。 I would appreciate answers / credible citations for the usage of these with reference to the C++ standards. 对于这些参考C ++标准的用法,我将不胜感激。 I wonder if there are any performance benefits involved in using one over the other. 我想知道一个使用另一个是否会带来任何性能上的好处。

Nonstandard Headers 非标头

  • <stdio> is not defined in any of the standards that I know of. 在我所知的任何标准中都没有定义<stdio>

Standardized Headers for C C的标准标头

  • <stdio.h> is the c header containing functions like printf() and scanf() . <stdio.h>是c头,包含诸如printf()scanf()类的printf()

Standardized Headers for C++ C ++的标准化标头

  • <stdio.h> is included in the c++ standard but is deprecated. <stdio.h>包含在c ++标准中,但已弃用。
  • <cstdio> is the c++ header that includes things like printf() and scanf() . <cstdio>是c ++头文件,包含诸如printf()scanf()
  • <iostream> is a c++ header that include things like std::cout , std::cerr and std::cin . <iostream>是一个c ++标头,其中包含std::coutstd::cerrstd::cin

stdio is for standard IO in C. It should have a .h at the end. stdio用于C中的标准IO。末尾应带有.h In C++, all C headers have been encapsulated in cxxxxxx headers (without .h ). 在C ++中,所有C头文件都封装在cxxxxxx头文件中(不带.h )。 So, <stdio.h> is the same as <cstudio> . 因此, <stdio.h><cstudio>相同。 These offer functions, like printf and scanf , for simple IO. 这些提供了用于简单IO的功能,例如printfscanf

iostream on the other hand is an IO library for C++, and offers streams like cin and cout , as you mentioned. iostream是C ++的IO库,并且提供了cincout类的流,如您所提到的。

Depending on your application you can use them interchangeably for most of the time. 根据您的应用程序,您可以在大多数情况下互换使用它们。 The syntax is going to be different, obviously. 显然,语法会有所不同。

Formatting text can be easier using the C functions. 使用C函数可以更轻松地格式化文本。 For example: 例如:

printf("item %04d has a value of %+.6e\n", index, value);

is easier to write than (needs <iomanip> in addition to <iostream> ): 比(比<iostream>还需要<iomanip> )更容易编写:

std::cout << "item " << std::setw(4) << std::setfill('0') << index
          << "has a value of " << std::setprecision(6) << value << "\n";

However, you need to be more careful when using the first one. 但是,使用第一个时,您需要格外小心。 For example, the following line won't produce a compile error (but as sharth mentioned, you might get warnings when compiling) but will cause runtime issues: 例如,以下行不会产生编译错误(但正如sharth所述,编译时可能会收到警告),但会导致运行时问题:

printf("I wonder what will happen? %d\n");

I don't think there is a lot of difference in their performance as most of the stream "magic" happens in compile time, and they should produce similar results. 我认为它们的性能没有太大差异,因为大多数流“魔术”都发生在编译时,它们应该产生相似的结果。 I'm not 100% sure though, so correct me if I'm wrong. 不过我不确定100%,如果我错了,请纠正我。

there is no stdio (stdio.h and cstdio). 没有stdio(stdio.h和cstdio)。 the 'c' and the missing '.h' in the header name indicates that it's the C++ version of the C header. 标头名称中的'c'和缺少的'.h'表示它是C标头的C ++版本。

check cstdio and iostream (references) 检查cstdioiostream (参考)

some compilers (including MSVC) include stl headers in other stl headers which leads to the effect you observed. 一些编译器(包括MSVC)在其他stl标头中包含stl标头,这会导致您观察到效果。 this is not portable though! 这不是便携式的!

if you are concerned with performance: use the C++ variants and check this 如果您担心性能,请使用C ++变体并进行检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM