使用命名空间标准

2022-01-11 00:00:00 namespaces header visual-c++ c++ std

我在学校上编程课,我想在课外开始做一些 c++ 编程.我的学校使用 Microsoft Visual C++ 6.0(从 1998 年开始),所以它仍然使用 <iostream.h> 而不是 <iostream>using namespace std.当我开始工作时,我不知道如何以及何时使用 using namespace 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!"<<' '; (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;

只是范围问题.范围只是说明编译器如何识别变量和函数名称等的一种奇特方式.命名空间只不过是在该命名空间内的所有变量上添加了一层额外的范围.当您键入 using 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).

会有人告诉你不要使用 using namespace 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.

我可以通过不使用语句using namespace 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.

相关文章