为什么我们应该使用“#include< iostream>”当我们使用“ using namespace std”时? [英] Why should we use "#include<iostream>" while we are using "using namespace std"?

查看:230
本文介绍了为什么我们应该使用“#include< iostream>”当我们使用“ using namespace std”时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Google中搜索了此问题。我在 stackoverflow.com quora.com 中找到了一些相关的问题,但是我仍然不清楚这两个主题。所有人都说我们使用#include< iostream> 进行输入/输出操作。现在,我们使用 cin 进行输入,并使用 cout 打印输出,这意味着这两个应该在<$ c $中定义c>#include< iostream> 。但是如果不使用使用命名空间std ,我们仍然无法接受任何输入,也无法在控制台上打印某些内容。所以,我的问题是-

I have searched this question in Google. I found some related question in stackoverflow.com and quora.com but i am still not clear about this two topics. Everyone says that we use #include<iostream> for input/output operation. Now, we take input using cin and print output using cout that means this two should be defined in #include<iostream>. But without using using namespace stdwe still can't take any input nor can print something on console. So, my questions are-


  1. cin cout 实际声明和定义了吗?是在#include< iostream> 还是在命名空间std 中?

  2. 如果在#include&iostream> 中为什么要使用使用命名空间std

  3. 如果在命名空间std 中,为什么要使用#include< iostream

  1. Where is cin and cout actually declared and defined? Is it in #include<iostream>or in namespace std?
  2. If in #include<iostream>why should we use using namespace std?
  3. If in namespace std why should we use #include<iostream?

在阅读了网络上的一些文章并在YouTube上观看了一些视频之后,我假设 cout cin 名称空间std 中定义,而名称空间std 没有定义因为它是在#include< iostream> 中定义的,所以没有任何意义。这就是为什么我们需要同时使用它们。 (只是我的想法让我知道我是否正确。)

After reading some article on the web and watching some videos on YouTube, I'm assuming cout and cin is defined in namespace std and the namespace std doesn't make any sense alone because it is defined in #include<iostream>. That's why we need to use them both. (Just my thought let me know if I am right or not.)

这个问题的目的是要弄清楚这两个事实。

The purpose of this question is to be clear about this two facts. If you can help it would be great.

推荐答案


为什么要使用 #include< iostream>

将标准库的I / O功能引入我们的程序。

To bring the standard library's I/O functionality into our program.


而我们在使用使用命名空间std 的时候?

这使我们无需每次都编写 std :: 即可使用该功能。

This allows us to use that functionality without writing std:: each time we do.

这与上一步无关。使用命名空间std只写不会带来I / O功能添加到您的程序中,并且只写 #include< iostream> 不允许我们在不完整写出其组件名称的情况下使用该功能(包括 std :: 前缀)。

This is unrelated to the previous step. Writing only using namespace std does not bring I/O functionality into your program, and writing only #include <iostream> does not allow us to use that functionality without writing its components' names out in full (including the std:: prefix).


  • #include 指令确定我们可以使用的内容;

  • 使用命名空间声明决定我们如何使用它。

  • The #include directive determines what we can use;
  • The using namespace declaration determines how we can use it.

完全可以:

#include <iostream>

int main()
{
   std::cout << "Hello world!\n";
}

也有效:

#include <iostream>

using namespace std;

int main()
{
   cout << "Hello world!\n";
}

无效:

int main()
{
   std::cout << "Hello world!\n";
}

也不是:

using namespace std;

int main()
{
   std::cout << "Hello world!\n";
}

或此:

using namespace std;

int main()
{
   cout << "Hello world!\n";
}

这篇关于为什么我们应该使用“#include&lt; iostream&gt;”当我们使用“ using namespace std”时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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