使用命名空间std [英] Using Namespace std

查看:176
本文介绍了使用命名空间std的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在学校上课,我想开始做类的一些c ++编程。我的学校使用Microsoft Visual C ++ 6.0(这是从1998年),因此它仍然使用< iostream.h> 而不是< iostream> code>和使用命名空间std 。当我开始工作,我不知道如何和何时使用使用命名空间std 和什么时候只使用 std :: cout< <Hello World!<<'\\\
';
(例如)以及它对命名空间关键字的限制和其他用法。特别是,如果我想用iostream和iomanip做一个程序,我必须声明使用命名空间std两次,或有一些不同的,我必须使用,或者我可以做同样的事情,我用iostream?我试着谷歌,但我真的不明白什么。先感谢您的帮助。

I am taking a programming class in school and I wanted to start doing some c++ programming out of class. My school using Microsoft Visual C++ 6.0 (which is from 1998) so it still uses <iostream.h> rather than <iostream> and using namespace std. When I started working, I couldn't figure out how and when to use using namespace std and when to just use things like std::cout<<"Hello World!"<<'\n'; (for example) as well as it's limits and other uses for the namespace keyword. In particular, if I want to make a program with iostream and iomanip, do I have to state "using namespace std" twice, or is there something different that I would have to use as well, or can I just do the same thing as I did with iostream? I tried googling it but I didn't really understand anything. Thanks in advance for the help.

推荐答案

好吧,一些东西,但它是可管理的。

Ok, handful of things there, but it is manageable.

首先,区别:

using namespace std;
...
cout << "Something" << endl;

并使用

std::cout << "Something" << std::endl;

只是范围问题。范围只是一个奇怪的方式,说如何编译器识别变量和函数的名称,除其他外。命名空间只需向该命名空间中的所有变量添加一个额外的范围层。当你使用namespace std 键入时,你将所有内部的命名空间 std 并将其移动到全局范围,因此您可以使用较短的 cout 而不是更完全限定的 std :: cout

Is simply a matter of scope. Scope is just a fancy way of saying how the compiler recognizes names of variables and functions, among other things. A namespace does nothing more than add an extra layer of scope onto all variables within that namespace. When you type using namespace std, you are taking everything inside of the namespace std and moving it to the global scope, so that you can use the shorter cout instead of the more fully-qualified std::cout.

有关命名空间的一点是,它们跨文件扩展。 < iostream> < iomanip> 使用命名空间 std 。因此,如果同时包含这两个文件,那么 using namespace std 的声明将对这两个文件进行操作,并且两个文件中的所有符号都将被移动到程序的全局范围或者函数的范围,如果你在函数中使用它)。

One thing to understand about namespaces is that they stretch across files. Both <iostream> and <iomanip> use the namespace std. Therefore, if you include both, then the declaration of using namespace std will operate on both files, and all symbols in both files will be moved to the global scope of your program (or a function's scope, if you used it inside a function).

有人会告诉你不要使用命名空间std !!!!,但他们很少告诉你为什么。让我们说,我有以下程序,我所要做的是定义两个整数并打印出来:

There are going to be people who tell you "don't use using namespace std!!!!", but they rarely tell you why. Lets say that I have the following program, where all I am trying to do is define two integers and print them out:

#include <iostream>

using namespace std;

int main(int argc, char** argv) {
    int cout = 0;
    int endl = 1;
    cout << cout << endl << endl;     // The compiler WILL freak out at this :)
    return 0;
}

当我使用 using namespace std ,我打开了命名碰撞的门。如果我(通过随机的机会),命名一个变量与在标题中定义的变量一样,那么你的程序将会中断,你将有一个艰难的时间找出为什么。

When I use using namespace std, I am opening the door for naming collisions. If I (by random chance), have named a variable to be the same thing as what was defined in a header, then your program will break, and you will have a tough time figuring out why.

我可以通过不使用语句使用命名空间std 来编写与之前相同的程序(但让它工作):

I can write the same program as before (but get it to work) by not using the statement using namespace std:

#include <iostream>

int main(int argc, char** argv) {
    int cout = 0;
    int endl = 1;
    std::cout << cout << endl << std::endl; // Compiler is happy, so I'm happy :)
    return 0;
}

希望这澄清了一些东西。

Hopefully this has clarified a few things.

这篇关于使用命名空间std的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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