17

这个问题可能是重复的,但我找不到一个好的答案。简短而简单,需要我声明什么

using namespace std;

在 C++ 程序中?

4

12 回答 12

44

由于 C++ 标准已被接受,几乎所有标准库都在 std 命名空间内。因此,如果您不想使用 限定所有标准库调用std::,则需要添加 using 指令。

然而,

using namespace std;

被认为是一种不好的做法,因为您实际上是在导入整个标准命名空间,从而为名称冲突开辟了很多可能性。最好只导入你在代码中实际使用的东西,比如

using std::string;
于 2010-02-07T20:16:28.813 回答
22

什么都没有,这是避免在该命名空间中的所有内容前加上 std:: 前缀的简写。

于 2010-02-07T20:13:52.017 回答
7

从技术上讲,您可能需要使用using(对于整个命名空间或单个名称)才能使用 Argument Dependent Lookup。

考虑以下两个使用swap().

#include <iostream>
#include <algorithm>

namespace zzz
{
    struct X {};


void swap(zzz::X&, zzz::X&) 
{
    std::cout << "Swapping X\n";
}
}

template <class T>
void dumb_swap(T& a, T& b)
{
    std::cout << "dumb_swap\n";
    std::swap(a, b);
}

template <class T>
void smart_swap(T& a, T& b)
{
    std::cout << "smart_swap\n";
    using std::swap;
    swap(a, b);
}

int main()
{
    zzz::X a, b;
    dumb_swap(a, b);
    smart_swap(a, b);

    int i, j;
    dumb_swap(i, j);
    smart_swap(i, j);
}

dumb_swap总是调用std::swap- 即使我们更喜欢使用zzz::swap对象zzz::X

smart_swap使std::swap作为后备选择可见(例如,当使用整数调用时),但由于它不完全限定名称,zzz::swap将通过 ADL 用于zzz::X.


主观上,强迫我使用using namespace std;的是编写使用各种标准函数对象等的代码。

//copy numbers larger than 1 from stdin to stdout
remove_copy_if(
    std::istream_iterator<int>(std::cin), std::istream_iterator<int>(),
    std::ostream_iterator<int>(std::cout, "\n"),
    std::bind2nd(std::less_equal<int>(), 0)
);

IMO,在这样的代码中std::只会产生线路噪音。

using namespace std;如果在实现文件中使用它,我不会在这种情况下发现令人发指的罪行(但它甚至可以限制在函数范围内,如交换示例中所示)。

绝对不要将 using 语句放在头文件中。原因是这会污染其他标头的名称空间,这些标头可能包含在有问题的标头之后,可能导致其他标头中的错误可能不受您的控制。(它还增加了一个令人惊讶的因素:包括该文件的人可能不希望看到所有类型的名称。)

于 2010-02-08T09:47:22.917 回答
5

std无需显式引用即可引用命名空间中的成员的能力std::member。例如:

#include <iostream>
using namespace std;

...
cout << "Hi" << endl;

对比

#include <iostream>

...
std::cout << "Hi" << std::endl;
于 2010-02-07T20:14:06.103 回答
5

你绝对不应该说:

using namespace std;

在您的 C++ 标头中,因为这超出了使用命名空间的全部意义(这样做会构成“命名空间污染”)。关于此主题的一些有用资源如下:

1)使用“std”的标准约定的stackoverflow线程</a>

2) Herb Sutter 的一篇关于迁移到命名空间的文章

3)来自 Marshall Cline 的 C++ Faq lite 的FAQ 27.5 。

于 2010-02-07T20:30:21.163 回答
3

首先,这在 C 中不是必需的- C 没有名称空间。在 C++ 中,std命名空间中包含大部分标准库的任何内容。如果你不这样做,你必须像这样显式地访问命名空间的成员:

std::cout << "I am accessing stdout" << std::endl;
于 2010-02-07T20:14:49.987 回答
3

首先,usingC 中从不需要该指令,因为 C 根本不支持命名空间。

using指令在 C++ 中从未真正需要,因为在命名空间中找到的任何项目都可以通过在它们前面加上前缀来直接访问std::。因此,例如:

using namespace std;
string myString;

相当于:

std::string myString;

您是否选择使用它是一个偏好问题,但是公开整个std命名空间以节省一些击键通常被认为是不好的形式。仅公开命名空间中特定项目的另一种方法如下:

using std::string;
string myString;

这允许您仅公开std命名空间中您特别需要的项目,而不会有无意中公开您不打算公开的内容的风险。

于 2010-02-07T20:18:53.117 回答
1

命名空间是一种包装代码的方式,以避免混淆和名称冲突。例如:

文件 common1.h:

namespace intutils
{
    int addNumbers(int a, int b)
    {
        return a + b;
    }
}

使用文件:

#include "common1.h"    
int main()
{
    int five = 0;
    five = addNumbers(2, 3); // Will fail to compile since the function is in a different namespace.
    five = intutils::addNumbers(2, 3); // Will compile since you have made explicit which namespace the function is contained within.

    using namespace intutils;
    five = addNumbers(2, 3); // Will compile because the previous line tells the compiler that if in doubt it should check the "intutils" namespace.
}

因此,当您编写using namespace std所有内容时,您所做的就是告诉编译器,如果有疑问,它应该在std命名空间中查找函数等,它无法找到定义。这在示例(和生产)代码中很常用,因为它使键入常用函数cout等比必须将每个函数完全限定为std::cout.

于 2010-02-07T20:30:32.050 回答
0

C++ 标准库中的所有文件都在 std 命名空间中声明其所有实体。
例如:使用cin,coutiostream 中定义的

备择方案:

using std::cout;
using std::endl;
cout << "Hello" << endl;
std::cout << "Hello" << std::endl;

于 2010-02-07T20:15:24.917 回答
0

每当您使用在命名空间中声明的内容时都会使用它。C++ 标准库在命名空间 std 中声明。因此你必须做

using namespace std;

除非您想在另一个命名空间中调用函数时指定命名空间,如下所示:

std::cout << "cout is declared within the namespace std";

您可以在http://www.cplusplus.com/doc/tutorial/namespaces/阅读更多相关信息。

于 2010-02-07T20:17:24.100 回答
0

你永远不必声明 using namespace std; 使用它是不好的做法,你应该使用 std:: 如果你不想输入 std:: 总是你可以在某些情况下做这样的事情:

using std::cout;

通过使用 std:: 您还可以判断程序的哪些部分使用标准库,哪些不使用。更重要的是,可能与包含的其他功能发生冲突。

Rgds莱恩

于 2010-02-07T20:19:20.540 回答
0

什么都不需要你做——除非你是 C++ 标准库的实现者,并且你想在以“新”和“旧”样式声明头文件时避免代码重复:

// cstdio
namespace std
{
  // ...
  int printf(const char* ...);
  // ...
}

.

// stdio.h
#include <cstdio>
using namespace std;

Well, of course example is somewhat contrived (you could equally well use plain <stdio.h> and put it all in std in <cstdio>), but Bjarne Stroustrup shows this example in his The C++ Programming Language.

于 2010-02-08T11:21:20.190 回答