© / Posted in 知识如海 / August 11, 2009

using namespace std 意思:
using 和namespace都是C++的关键词。
std 是标准程序库所驻之命名空间(namespace)的名称。
如果使用Boost的库 ,那就写 using namespace boost;
如果使用C++ 标准库 那就写 using namespace std;

就是暴露std这个名字空间,你就可以调用std这个名字空间下的东西,包含在这个命名空间的所有类还有函数都可以使用了


一般的名字空间用法:
这样使用:

  •   namespace    sdm  

  •    {  

  •             int    temp;  

  •              ......  

  •    }   

  • 那么你在一个文件中有三种用法:
    1> using namespace sdm;
    然后你就可以使用变量temp:
    temp=9;
    还可以使用sdm中的其他每一个成员。
    2>using sdm::temp;
    在这个文件中只能使用sdm中的temp这个变量。
    3>你在使用时直接使用sdm::temp:
    cout<<sdm::temp<<endl;
    使用其它的成员也这样调用。

    哈哈 using namespace这个东西 我觉得涉及到一种组织学 就是怎么把一堆代码归在一起,把它们纳入一个space(空间);然后使用它们的时候,先要声明使用它所在的这个空间

    比如A B两个班都有叫张三的人,你要使用A班的张三,必然要先指名是A班这个名字空间(namespace),然后你对张三的所有命令才能达到你的预想,不会叫错人 呵呵

    感觉语言这东西,重名是最麻烦的,因为人类自身的限制,不能使用太复杂的来作为名字(变量名,函数名),所以很容易出现名字冲突,所以开发了名字空间这种机制来保证大家有足够的名字可以用 呵呵
    具我的体会class(类)这种东西,也具有类似的作用,不同类的同名函数可以存在但不冲突,就是表现之一呀

    namespace 就是一个名字的空间。是为了避免名字冲突而使用的。在不同的空间里可以使用同一个名字而不会引起名字冲突。
    它的声明就像声明一个类一样。
    例:

  •   namespace    A{  

  •       int    f(){……};  

  •       char    g(){……};  

  •        ……}   

  • 在使用名字空间里函数的时候有几种方法:
    1-就像声明一个类的成员函数一样,只是类名换成了空间名 A::f();
    2-可以用using关键字 using namespace A; 这样以后的代码使用的就是名字空间A中的函数了。

    扩展阅读http://www.kuqin.com/language/20080107/3532.html
    #include<iostream.h>
    这是C++早期的东西
    当时这个类库里没有模版的东西
    而#incldue<iostream>
    using namespace std;
    用的是新编制出来的STD
    是跟原来的那个不同的一个类
    是模版类
    6.0用的时候都兼容
    而2005就把早期的非模版库给抛弃了
    你可以在6.0中进行追踪cout这些东西
    跳进去看,你可以发现,你引入不同的时候
    跳进去的库也不同

    ——————————————————华丽的分割线————————————————

    一 :

    <iostream>和<iostream.h>是不一样,前者没有后缀,实际上,在你的编译器include文件夹里面可以看到,二者是两个文件,打开文件就会发现,里面的代码是不一样的。

    后缀为.h的头文件c++标准已经明确提出不支持了,早些的实现将标准库功能定义在全局空间里,声明在带.h后缀的头文件里,c++标准为了和C区别开,也为了正确使用命名空间,规定头文件不使用后缀.h。

    因此,当使用<iostream.h>时,相当于在c中调用库函数,使用的是全局命名空间,也就是早期的c++实现;当使用<iostream>的时候,该头文件没有定义全局命名空间,必须使用namespace std;这样才能正确使用cout。

    二:

    所谓namespace,是指标识符的各种可见范围。

    C++标准程序库中的所有标识符都被定义于一个名为std的namespace中。

    由于namespace的概念,使用C++标准程序库的任何标识符时,可以有三种选择:

    1、直接指定标识符。例如std::ostream而不是ostream。完整语句如下:

    std::cout << std::hex << 3.4 << std::endl;

    2、使用using关键字。

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

    以上程序可以写成

    cout << std::hex << 3.4 << endl;

    3、最方便的就是使用using namespace std;

    例如:

    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    这样命名空间std内定义的所有标识符都有效(曝光)。就好像它们被声明为全局变量一样。那么以上语句可以如下写:

    cout << hex << 3.4 << endl;

    因为标准库非常的庞大,所程序员在选择的类的名称或函数名时就很有可能和标准库中的某个名字相同。所以为了避免这种情况所造成的名字冲突,就把标准库中的一切都被放在名字空间std中。但这又会带来了一个新问题。无数原有的C++代码都依赖于使用了多年的伪标准库中的功能,他们都是在全局空间下的。 

             所以就有了<iostream.h>和<iostream>等等这样的头文件,一个是为了兼容以前的C++代码,一个是为了支持新的标准。

    命名空间std封装的是标准程序库的名称,标准程序库为了和以前的头文件区别,一般不加".h"

     

    因为标准库非常的庞大,所程序员在选择的类的名称或函数名时就很有可能和标准库中的某个名字相同。所以,第三种用法,很可能会照成冲突。

    如果在只用少数的标准库函数的时候,应该选用第一种或者第二种用法,避免在定义自己的函数时名称与标准库中的函数名相同,导致编译时报错

    上一篇:NTFS文件系统中的卷 | 下一篇:吉他初学

    本文有 3 篇评论 ↓↓

      1. 这么简单一个问题能写这么长的文章……佩服……
        我印象中,加了.h是不用声明使用命名空间的。
        但是这样gcc或者g++会给警告。

        1. @Euyuil, 呵呵~~以前的文章都不是我写的哦~~都是转的,是碰到什么问题就上网找资料,找到了就转一下,为了以后看资料方便~~
          如果是C++的代码,不用命名空间,警告很正常~~


      2. welcome to our burberry outlet store ,there are many fashion and hight quality burberry bags wait for

        you!
        do you want to buy burberry bags for yourslf or your beat friends in sometimes?

        please come to our burberry outlet store.
        our burberry outlet store offer many new style burberry watches for you.do

        not miss the fashion burbery Burberry T-shirts.
        In burberry sunglasses stores it really is typically some sort of

        most effective style which precisely the top notch and several middle-class are typically in a region to afford
        Fouthy-six Putting on a new burberry watches for men and relish the

        summertime Does one prefers to invest in Burberry bags?


        as we all know the burberry brand is very famouse in the world.in burberry bags outlet store you

        will have a good time!
        there is new store,the name is burberry outlet !you are interested in new store,you will have a good

        time in there.
        If you use burberry bags ,you will feel very Summer feeling, so many girls put it

        down.
        evey women love the burberry shirts in life,the burberry can give you more

        confidence in sometimes.
        Jack bought the burberry sunglasses and the next day he came to the

        interview in the Burberry cheak shirt.


        Join forums that areparticularlymeant to talk about watches. If you are searching for burberry sale for women, then

        obviouslycheck out women's forum.
        One of the most popular British exports is the burberry outlet online .
        The particular burberry bags typically reveal the actual Burberry renowned examine layout

        which has been made popular by the garment range.
        The color of burberry ties are bright but with the characters of mature and steady
        the burberry shoes collection had occipied a large markey share and enjoys a massive

        fan following.
        I believe one's destiny of your burberry wallets long decrease jackets are

        often more large-scale progress.

      添加新评论 ↑↑