2010-02-07 297 views

回答

42

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

然而,

using namespace std; 

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

using std::string; 
+14

+1因为是唯一一个提到它而皱眉。 – GManNickG 2010-02-07 20:17:40

23

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

+11

而且也被认为是不好的做法。 – GManNickG 2010-02-07 20:18:49

+2

其糟糕的做法,如果你这样做全局命名空间:D – 2010-02-07 20:22:02

+7

@GMan,@Hassan:在实现文件中使用它,并在头文件中是危险的。为什么每个人都会说“不好的做法”?我不想在代码中使用'std ::',因为我不想使用'namespace some_tools'隐式地导入任何名称空间;' – Potatoswatter 2010-02-07 21:54:31

5

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

#include <iostream> 
using namespace std; 

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

#include <iostream> 

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

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

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

所有的C++标准库文件申报其所有实体的std命名空间内。
e.g:要使用在iostream的定义

替代cin,cout

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

0

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

using namespace std; 

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

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

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

3

首先,因为C根本不支持命名空间,所以在C中不需要指令using

using指令实际上从未需要用C++因为任何在命名空间中的项目可以通过std::前缀,而不是他们直接访问。因此,举例来说:

using namespace std; 
string myString; 

等同于:

std::string myString; 

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

using std::string; 
string myString; 

这可以让你只暴露在std命名空间中,你特别需要的项目,没有你没有无意中暴露出一些风险打算。

0

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

using std::cout; 

使用的std ::你也可以告诉程序的哪一部分使用标准库,哪一部分不使用。更重要的是,可能会与其他包含的功能发生冲突。

RGDS 莱恩

+2

在头文件的全局名称空间中这只是一个不好的做法。在实现文件中,这通常是一个好主意。保存打字无关紧要 - 您的编辑应该为您打字。这很好,因为它使代码比任何地方都具有'std ::'更具可读性,并且比每个文件顶部有三十行'使用std :: whatever;'更易于维护。 – Porculus 2010-02-07 22:47:30

5

你绝对不应该说:

using namespace std; 
在C++头

,因为拍使用名称空间(这样做会构成“命名空间污染”)的整点。有关这个主题的一些有用的资源如下:上Standard convention for using “std”

1)计算器线程)由香草萨特的文章Migrating to Namespaces

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

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

7

从技术上讲,您可能需要使用,使用(对于整个名称空间或单个名称)才能使用参数相关查找。

考虑使用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作为后备选择可见的(例如在与所谓的整数),但因为它不完全限定名称,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语句放在头文件中。原因是这会污染其他标题的名称空间,可能会在违规之后包含其他标题,这可能会导致其他标题中可能不受您控制的错误。 (这也增加了意外因素:人,其中包括该文件可能无法期待各种名目的是可见的。)

0

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

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

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

嘛,当然例子是人为(你同样可以使用纯<stdio.h>,并把它所有性病中<cstdio>),但Bjarne Stroustrup显示了他的这个例子。

相关问题