"#include< iostream>"是什么意思?做? [英] What does "#include <iostream>" do?

查看:69
本文介绍了"#include< iostream>"是什么意思?做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我开始学习基本C ++时,我一直使用标题

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

#include <iostream>
using namespace std;

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

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

推荐答案

为了读取或写入标准输入/输出流,您需要将其包括在内.

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;
}

除非您添加 #include< iostream>

第二行不是必需的

using namespace std;

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

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

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

请注意,您不再需要使用标准名称 std :: cout 引用输出流,而可以使用较短的名称 cout .

Notice you no longer need to refer to the output stream with the fully qualified name std::cout and can use the shorter name 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;
}

但这是个人喜好问题.

这篇关于"#include&lt; iostream&gt;"是什么意思?做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆