使用命名空间 std

发布于 2024-11-08 07:46:32 字数 478 浏览 0 评论 0原文

我正在学校上一门编程课,我想在课外开始做一些 C++ 编程。我的学校使用 Microsoft Visual C++ 6.0(从 1998 年开始),因此它仍然使用 而不是 using namespace std。当我开始工作时,我不知道如何以及何时使用 using namespace std 以及何时仅使用 std::cout<<"Hello World!"< <'\n'; (例如)以及它的限制和名称空间关键字的其他用途。特别是,如果我想用 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

只为一人 2024-11-15 07:46:32

好吧,有一些事情,但它是可以管理的。

首先,两者之间的区别:

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

和 using

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

只是一个范围问题。范围只是一种奇特的方式来说明编译器如何识别变量和函数的名称等。命名空间只不过是在该命名空间内的所有变量上添加额外的作用域层。当您输入 using namespace std 时,您将获取命名空间 std 内的所有内容并将其移动到全局范围,以便您可以使用较短的 cout< /code> 而不是更完全限定的 std::cout

关于命名空间需要理解的一件事是它们跨文件延伸。 都使用命名空间 std。因此,如果您同时包含这两个文件,则 using namespace std 的声明将对这两个文件进行操作,并且这两个文件中的所有符号都将移动到程序的全局作用域(或函数的作用域,如果您在函数中使用了它)。

会有人告诉你“不要使用 using namespace std!!!”,但他们很少告诉你原因。假设我有以下程序,我要做的就是定义两个整数并将它们打印出来:

#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 时,我打开了命名冲突的大门。如果我(随机地)将一个变量命名为与标头中定义的变量相同,那么您的程序将崩溃,并且您将很难找出原因。

我可以通过不使用 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;
}

希望这已经澄清了一些事情。

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

First off, the difference between:

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

And using

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

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.

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).

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

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.

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.

黒涩兲箜 2024-11-15 07:46:32

如果您使用不带 .h 的标头名称,则其中声明/定义的内容将位于 std 命名空间中。您只需在要导入内容的范围内使用 using namespace std; 一次即可获取所有内容;多个 using namespace std; 没有任何帮助。

不过,我一般建议不要使用 using namespace std; 。例如,我更喜欢使用 using std::cout; 来代替,以防止 std 中的名称与我的发生冲突。

例如:

#include <iostream>
#include <iomanip>    

int main()
{
     using namespace std;

     int left = 1, right = 2;
     cout << left << " to " << right << "\n";
}

可能会导致神秘的问题,因为 leftright 存在于 std 命名空间中(作为 IO 操纵器),如果您懒洋洋地说using namespace std;。如果您打算实际使用 IO 操纵器而不是输出变量,您可能会有点失望。但无论如何,其意图并不明显。也许您只是忘记了您有名为 leftright 的整数。

相反,如果你说

#include <iostream>
#include <iomanip>

int main()
{
    using std::cout;

    int left = 1, right = 2;
    cout << left << " to " << right << "\n";
}

或者

#include <iostream>
#include <iomanip>

int main()
{
     int left = 1, right = 2;
     std::cout << left << " to " << right << "\n";
}

一切都按预期进行。另外,您还可以看到实际使用的内容(在本例中,不包含 中的任何内容),因此可以更轻松地将包含内容精简为您需要的内容。

If you use the header names without the .h, then the stuff declared/defined in it will be in the std namespace. You only have to use using namespace std; once in the scope where you want stuff imported in order to get everything; more than one using namespace std; doesn't help anything.

I'd recommend against using namespace std; in general, though. I prefer to say, for example, using std::cout; instead, in order to keep names in std from conflicting with mine.

For example:

#include <iostream>
#include <iomanip>    

int main()
{
     using namespace std;

     int left = 1, right = 2;
     cout << left << " to " << right << "\n";
}

may cause mysterious issues, because left and right exist in the std namespace (as IO manipulators), and they get imported if you lazily say using namespace std;. If you meant to actually use the IO manipulators rather than output the variables, you may be a bit disappointed. But the intent isn't obvious either way. Maybe you just forgot you have ints named left and right.

Instead, if you say

#include <iostream>
#include <iomanip>

int main()
{
    using std::cout;

    int left = 1, right = 2;
    cout << left << " to " << right << "\n";
}

or

#include <iostream>
#include <iomanip>

int main()
{
     int left = 1, right = 2;
     std::cout << left << " to " << right << "\n";
}

everything works as expected. Plus, you get to see what you're actually using (which, in this case, includes nothing from <iomanip>), so it's easier to keep your includes trimmed down to just what you need.

风尘浪孓 2024-11-15 07:46:32

这里是一个很好的链接,描述了命名空间及其工作原理。

两种方法都是正确的,即可以用“using”语句引入命名空间,也可以限定命名空间的所有成员。这是编码风格的问题。我更喜欢使用命名空间进行限定,因为它可以让读者清楚地知道函数/类是在哪个命名空间中定义的。

此外,如果包含多个文件,则不必两次引入名称空间。一条 using 语句就足够了。

Here is a good link that describes namespaces and how they work.

Both methods are correct, that is, you can either introduce a namespace with the "using" statement or you can qualify all the members of the namespace. Its a matter of coding style. I prefer qualifying with namespaces because it makes it clear to the reader in which namespace the function / class is defined.

Also, you do not have to introduce a namespace twice if you are including multiple files. One using statement is enough.

a√萤火虫的光℡ 2024-11-15 07:46:32

好问题,瑞安。 using namespace 的作用是将给定命名空间(范围)中的所有符号导入到使用它的范围中。例如,您可以执行以下操作:

namespace A {
  struct foo {};
}

namespace B {
  using namespace A;

  struct bar : foo {};
}

在上面的示例中,命名空间 A 中的所有符号在命名空间 B 中变得可见,就像它们在此处声明一样。

此导入仅影响给定的翻译单元。因此,例如,当您在实现文件(即 .cpp)中执行 using namespace std; 时,您基本上会从 std 导入所有符号命名空间进入全局范围。

您还可以导入某些符号而不是所有内容,例如:

using std::cout;
using std::endl;

您可以在全局范围、命名空间范围或函数范围中执行此操作,如下所示:

int main ()
{
   using namespace std;
}

由程序员决定何时使用完全限定名称以及何时使用 使用关键字。通常,将 using 放入头文件中是一种非常糟糕的做法。专业的 C++ 程序员几乎从不这样做,除非这是解决某些问题所必需的,或者他们 100% 确定这不会扰乱使用该标头的人的类型解析。

然而,在源文件内部(没有人包含源文件),只要不同命名空间中没有冲突的名称,就可以执行任何类型的 using 语句。这只是一个品味问题。例如,如果整个代码中使用了大量来自不同命名空间的符号,我希望至少有一些关于它们实际声明位置的提示。但每个人都熟悉 STL,因此 using namespace std; 绝对不会造成任何损害。

还可能有一些很长的命名空间,在这些情况下命名空间别名会很方便。例如,有一个 Boost.Filesystem 库,它将其所有符号放在 boost::filesystem 命名空间中。使用该命名空间会太多,所以人们通常会这样做:

namespace fs = boost::filesystem;

fs::foo ();
fs::bar ();

另外,在标头中使用命名空间别名几乎是可以的,如下所示:

namespace MyLib {
  namespace fs = boost::filesystem;
}

.. 并受益于更少的打字。发生的情况是,将使用此标头的用户不会通过说 using namespace MyLib; 来导入整个文件系统库。但是,他们将从您的库中导入“fs”名称空间,这可能会与其他内容发生冲突。所以最好不要这样做,但如果你太想要它,最好比在那里说 using namespace boost::filesystem

回到你的问题。如果您使用 C++ I/O 流编写库,最好不要在标头中包含任何 using 语句,并且我会在每个中使用 using namespace std; cpp 文件。例如:

somefile.hpp:

namespace mylib {

class myfile : public std::fstream {
public: 
  myfile (const char *path);

 // ...
};

}

somefile.cpp:

#include "somefile.hpp"

using namespace std;
using namespace mylib;

myfile::myfile (const char *path) : fstream (path)
{
  // ...
}

Good question, Ryan. What using namespace does is importing all symbols from a given namespace (scope) into the scope where it was used. For example, you can do the following:

namespace A {
  struct foo {};
}

namespace B {
  using namespace A;

  struct bar : foo {};
}

In the above examples, all symbols in namespace A become visible in namespace B, like they were declared there.

This import has affect only for a given translation unit. So, for example, when in your implementation file (i.e. .cpp) you do using namespace std;, you basically import all symbols from std namespace into a global scope.

You can also import certain symbols rather than everything, for example:

using std::cout;
using std::endl;

You can do that in global scope, namespace scope or function scope, like this:

int main ()
{
   using namespace std;
}

It is up to a programmer to decide when to use fully qualified names and when to use using keyword. Usually, it is a very bad taste to put using into a header files. Professional C++ programmers almost never do that, unless that is necessary to work around some issue or they are 100% sure it will not mess up type resolution for whoever use that header.

Inside the source file, however (nobody includes source files), it is OK to do any sort of using statements as long as there are no conflicting names in different namespaces. It is only a matter of taste. For example, if there are tons of symbols from different namespaces being used all over the code, I'd prefer at least some hints as for where they are actully declared. But everyone is familiar with STL, so using namespace std; should never do any harm.

There also could be some long namespaces, and namespace aliasing comes handy in those cases. For example, there is a Boost.Filesystem library that puts all of its symbols in boost::filesystem namespace. Using that namespace would be too much, so people usually do something like this:

namespace fs = boost::filesystem;

fs::foo ();
fs::bar ();

Also, it is almost OK to use namespace aliasing in headers, like this:

namespace MyLib {
  namespace fs = boost::filesystem;
}

.. and benefit from less typing. What happens is that users that will use this header, will not import the whole filesystem library by saying using namespace MyLib;. But then, they will import "fs" namespace from your library that could conflict with something else. So it is better not to do it, but if you want it too badly, it is better than saying using namespace boost::filesystem there.

So getting back to your question. If you write a library using C++ I/O streams, it is better not to have any using statements in headers, and I'd go with using namespace std; in every cpp file. For example:

somefile.hpp:

namespace mylib {

class myfile : public std::fstream {
public: 
  myfile (const char *path);

 // ...
};

}

somefile.cpp:

#include "somefile.hpp"

using namespace std;
using namespace mylib;

myfile::myfile (const char *path) : fstream (path)
{
  // ...
}
老娘不死你永远是小三 2024-11-15 07:46:32

特定于 using namespace std

你真的不应该在头文件中使用它。通过这样做,您已将整个“std”导入到包含您的头文件的任何人或包含包含您的文件的文件的任何其他人的全局命名空间中。

在 .cpp 文件中使用它是个人喜好。我通常不会将整个 std 导入到全局命名空间中,但自己这样做似乎没有任何害处,可以节省一些打字时间。

Specific to using namespace std

You really shouldn't ever use it in a header file. By doing so, you've imported the entire 'std' into the global namespace for anyone who includes your header file, or for anyone else that includes a file that includes your file.

Using it inside a .cpp file, that's personal preference. I typically do not import the entire std into the global namespace, but there doesn't appear to be any harm in doing it yourself, to save a bit of typing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文