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

using namespace std;

在C++程序中?

有帮助吗?

解决方案

由于C ++标准已被接受,几乎所有的标准库的是std命名空间的内部。所以,如果你不想限定所有标准库调用与std::,你需要添加using指令。

然而,

using namespace std;

被认为是不好的做法,因为你几乎导入整个标准的命名空间,从而打开了很多的名称冲突的可能性。这是更好地导入只有你实际使用在你的代码的东西,像

using std::string;

其他提示

没有任何东西,这是一个缩写,以避免在命名空间前缀的std ::一切

从技术上讲,你可能需要使用使用(适用于整个命名空间或个人的名称),以能够使用参数相关的查找。

考虑使用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::swapzzz::X对象。

smart_swap使得作为后备的选择(例如当与整数称为),但由于它不完全限定的名称,std::swap将通过ADL为zzz::swap使用。

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语句中的头文件。其原因是,这种污染对其他头,其中可能包括违规一前一后的命名空间,有可能导致其他错误,头这可能不是你的控制之下。 (这也增加了意外的因素:人,包括该文件可能无法期待各种名目的是可见的)

指成员在std命名空间,而不需要的能力是指明确std::member。例如:

#include <iostream>
using namespace std;

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

VS

#include <iostream>

...
std::cout << "Hi" << std::endl;

您绝对不应该说:

using namespace std;

在C ++头,因为跳动使用名称空间(这样做,将构成“命名空间污染”)的整点。关于此主题的一些有用的资源如下:

1)上标准惯例计算器线程使用“STD”

2)上的一篇文章通过香草萨特迁移到命名空间

3) FAQ 27.5 从马歇尔克莱因的C ++ FAQ精简版。

首先,这不是在C需要 - C没有命名空间。在C ++中,任何在std命名空间,其包括大多数标准库。如果你不这样做,你必须明确地访问该命名空间的成员,像这样:

std::cout << "I am accessing stdout" << std::endl;

首先,using指令从未使用C需要的,因为C没有在所有支持命名空间。

using指令是从来没有实际的需要在C ++中,因为任何在该命名空间中找到的项的可通过与std::前缀来代替直接访问。因此,例如:

using namespace std;
string myString;

等同于:

std::string myString;

无论您选择使用它是偏好的问题,但暴露了整个std命名空间来保存几个按键通常被认为是不好的形式。仅在该命名空间暴露特定物品的另一种方法如下:

using std::string;
string myString;

这允许你只暴露在std命名空间中,你特别需要的项目,如果没有意外泄露的东西你不打算风险。

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

文件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更快。

您从来没有使用命名空间std申报;使用它是不好的做法,你应该使用std ::如果你不想打字的std ::总是你可以做这样的事情在某些情况下:

using std::cout;

使用的std ::你也可以告诉你的程序的一部分,使用标准库,哪些不是。这是更重要的,有可能是与包括得到其他职能的冲突。

RGDS 莱恩

在C ++标准库中的所有文件的所有声明及其实体std命名空间内。结果, e.g:要使用在iostream的定义cin,cout

备选方案:

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

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

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

.

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

好吧,当然示例有些人为(您同样可以使用简单的 <stdio.h> 并将其全部放入 std 中 <cstdio>), 但 比亚内·斯特鲁斯特鲁普 在他的例子中展示了这个例子 C++ 编程语言.

它时,你正在使用的是一个命名空间内声明的东西使用。 C ++标准库命名空间std中声明。因此,你必须做的。

using namespace std;

除非你想另一个命名空间中调用函数时指定的命名空间,就像这样:

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

您可以在 http://www.cplusplus.com/doc阅读更多关于它/教程/命名空间/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top