C++ “#include <iostream>”有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22645097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:08:52  来源:igfitidea点击:

What does "#include <iostream>" do?

c++

提问by William

As I started learning basic C++, I've always used the headings

当我开始学习基本的 C++ 时,我总是使用标题

#include <iostream>
using namespace std;

I want to question what is the point of iostream. Is it required every time as a heading?

我想质疑iostream的意义是什么。每次都需要作为标题吗?

回答by amdn

In order to read or write to the standard input/output streams you need to include it.

为了读取或写入标准输入/输出流,您需要包含它。

int main( int argc, char * argv[] )
{
    std::cout << "Hello World!" << std::endl;
    return 0;
}

That program will not compile unless you add #include <iostream>

除非您添加,否则该程序将无法编译 #include <iostream>

The second line isn't necessary

第二行不是必需的

using namespace std;

What that does is tell the compiler that symbol names defined in the stdnamespace are to be brought into your program's scope, so you can omit the namespace qualifier, and write for example

这样做是告诉编译器将std命名空间中定义的符号名称带入程序的范围,因此您可以省略命名空间限定符,并编写例如

#include <iostream>
using namespace std;
int main( int argc, char * argv[] )
{
    cout << "Hello World!" << endl;
    return 0;
}

Notice you no longer need to refer to the output stream with the fully qualified name std::coutand can use the shorter name cout.

请注意,您不再需要使用完全限定名称来引用输出流,std::cout而可以使用较短的名称cout

I personally don't like bringing in all symbols in the namespace of a header file... I'll individually select the symbols I want to be shorter... so I would do this:

我个人不喜欢在头文件的命名空间中引入所有符号......我将单独选择我想要更短的符号......所以我会这样做:

#include <iostream>
using std::cout;
using std::endl;

int main( int argc, char * argv[] )
{
    cout << "Hello World!" << endl;
    return 0;
}

But that is a matter of personal preference.

但这是个人喜好的问题。

回答by Elliott Frisch

That is a C++ standard libraryheader file for input output streams. It includes functionality to read and write from streams. You only need to include it if you wish to use streams.

那是输入输出流C++ 标准库头文件。它包括从流中读取和写入的功能。如果您希望使用流,则只需要包含它。

回答by Ramzan

#indicates that the following line is a preprocessor directive and should be processed by the preprocessor before compilation by the compiler.

#表示下一行是预处理器指令,应该在编译器编译之前由预处理器处理。

So, #includeis a preprocessor directive that tells the preprocessor to include header files in the program.

所以,#include是一个预处理器指令,它告诉预处理器在程序中包含头文件。

< >indicate the start and end of the file name to be included.

< >指示要包含的文件名的开始和结束。

iostreamis a header file that contains functions for input/output operations (cinand cout).

iostream是一个头文件,其中包含用于输入/输出操作(cincout)的函数。

Now to sum it up C++ to English translation of the command, #include <iostream>is:

现在总结一下C++命令的英文翻译,#include <iostream>就是:

Dear preprocessor, please include all the contents of the header file iostreamat the very beginning of this program before compiler starts the actual compilation of the code.

亲爱的预处理器,iostream在编译器开始实际编译代码之前,请在程序的最开始包含头文件的所有内容。