"有什么作用?-6ren"> "有什么作用?-当我开始学习基本的 C++ 时,我一直使用标题 #include using namespace std; 我想问一下 iostream 的意义何在。每次都需要作为标题吗? 最佳答案 为了读取或写入-6ren">
gpt4 book ai didi

c++ - "#include "有什么作用?

转载 作者:IT老高 更新时间:2023-10-28 22:00:45 27 4
gpt4 key购买 nike

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

#include <iostream>
using namespace std;

我想问一下 iostream 的意义何在。每次都需要作为标题吗?

最佳答案

为了读取或写入 standard input/output流,您需要包含它。

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

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

第二行不是必须的:

using namespace std;

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

#include <iostream>
using namespace std;

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

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

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

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

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

但这是个人喜好问题。

关于c++ - "#include <iostream>"有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22645097/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com