S T D STD STD是一个标准输入输出类

S T D STD STD定义

s t d std std是一个类(输入输出标准),它包括了 c i n cin cin成员和 c o u t cout cout成员,“ u s i n g    n a m e s p a c e    s t d using\,\,namespace\,\,std usingnamespacestd ;”以后才能使用它的成员。 # i n c l u d e < i o s t r e a m . h > \#include<iostream.h> #include<iostream.h>中不存在类 s t d std std,但是他有 c i n , o u t cin,out cin,out的相关函数,不需要使用命名空间了。

# i n c l u d e < i o s t r e a m > \#include<iostream> #include<iostream>

而第二种标准 # i n c l u d e < i o s t r e a m > \#include<iostream> #include<iostream>,它包含了一个类,在类的使用之前要预处理一下,“ u s i n g    n a m e s p a c e    s t d using\,\, namespace\,\, std usingnamespacestd;”就是这个功能,然后你就可以使用 c i n , c o u t cin,cout cin,cout这两个成员函数了,假设你不使用预处理 ( u s i n g    n a m e s p a c e    s t d ; ) (using\,\, namespace\,\, std;) usingnamespacestd;),麻烦加上 s t d : : c i n std::cin std::cin或者 s t d : : c o u t std::cout std::cout再去使用它的成员函数(因为头文件中存在这个类)所谓 n a m e s p a c e namespace namespace,是指标识符的各种可见范围 C + + C++ C++标准程序库中的所有标识符都被定义于一个名为 s t d std std n a m e s p a c e namespace namespace中。

i o s t r e a m iostream iostream i o s t r e a m . h iostream.h iostream.h的区别

前者没有后缀,实际上,在你的编译器 i n c l u d e include include文件夹里面可以看到,二者是两个文件,打开文件就会发现,里面的代码是不一样的。

  • 后缀为 . h .h .h的头文件 c + + c++ c++标准已经明确提出不支持了,早些的实现将标准库功能定义在全局空间里,声明在带 . h .h .h后缀的头文件里, c + + c++ c++标准为了和 C C C区别开,也为了正确使用命名空间,规定头文件不使用后缀 . h .h .h。 因 此,当使用 < i o s t r e a m . h > <iostream.h> <iostream.h>时,相当于在 c c c中调用库函数,使用的是全局命名空间,也就是早期的 c + + c++ c++实现;当使用 < i o s t r e a m > < iostream> <iostream>的时候,该头文件没有定义全局命名空间,必须使用 n a m e s p a c e    s t d namespace\,\, std namespacestd;这样才能正确使用 c o u t cout cout
  • n a m e s p a c e namespace namespace是指标识符的各种可见范围
  • C + + C++ C++标准程序库中的所有标识符都被定义于一个名为 s t d std std n a m e s p a c e namespace namespace中。 由于 n a m e s p a c e namespace namespace的概念,使用 C + + C++ C++标准程序库的任何标识符时,可以有三种选择:

1.直接指定标识符:例如 s t d : : i o s t r e a m std::iostream std::iostream而不是 i o s t r e a m iostream iostream。完整语句如下:

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

2.使用 u s i n g using using关键字

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

以上程序可以写成如下代码:

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

使用 u s i n g    n a m e s p a c e    s t d using\,\,namespace\,\,std usingnamespacestd
例如:

  #include<iostream>
  #include<sstream>
  #include<string>
  using namespace std;

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

cout << hex << 3.4 << endl;

因为标准库非常的庞大,所以程序员在选择的类的名称或函数名时就很有可能和标准库中的某个名字相同。所以为了避免这种情况所造成的名字冲突,就把标准库中的一切都放在名字空间 s t d std std中。但这又会带来了一个新问题。无数原有的 C + + C++ C++代码都依赖于使用了多年的伪标准库中的功能,他们都是在全局空间下的。 所以就有了 < i o s t r e a m > <iostream> <iostream> < i o s t r e a m . h > <iostream.h> <iostream.h>等等这样的头文件,一个是为了兼容以前的 C + + C++ C++代码,一个是为了支持新的标准。 命名空间std封装的是标准程序库的名称,标准程序库为了和以前的头文件区别,一般不加".h"

什么时候在 C + + C++ C++中什么时候需要加上 s t d : : std:: std::

s t d std std是命名空间,你所用到的很多函数或者类都是被放到命名空间里面的,命名空间是防止名字重复而使用的,比如 S T L STL STL有个类叫 s t r i n g string string,而你也设计一个类叫 s t r i n g string string,那么编译器编译的时候就搞不清楚到底是那个 s t r i n g string string,所以用一个命名空间就比较方便了。具体是这么回事的,比如有两个班级, A A A班和 B B B班,两个班各有一个叫张三的人,而两个班的同学相互之间都是非常熟悉的。那么他们聊天的时候说张三,那其他人肯定会问,哪个张三?对吧,因为搞不清楚到底说的是哪个,所以会犯迷糊。而这个时候,那个人会补充, A A A班的张三或者 B B B班的张三,这样,其他人就知道到底是谁了。这里的 A A A班, B B B班就好像命名空间一样,而张三就好像那个 s t r i n g string string,或者说是对象,变量或者函数。
当你自己定义一个 s t r i n g string string并把它放到命名空间 A A A AAA AAA中的时候,你使用 s t r i n g string string只要指定是哪个命名空间的,就不会导致编译器分不清是哪个 s t r i n g string string了。你使用时会用 s t d : : s t r i n g std::string std::string或者 A A A : : s t r i n g AAA::string AAA::string,前者告诉编译器我用的 s t r i n g string string是在命名空间 s t d std std里面的,后者告诉编译器用的 s t r i n g string string是在命名空间 A A A AAA AAA里面的。这样,编译器就一目了然,不会出错。但是你却发现每次只要用到 s t r i n g string string都必须在前面加上 A A A AAA AAA,这样相当麻烦,有没有办法简化操作呢?当然有,就好像上面的例子,那些人聊天之前,他可以告诉其他人说,注意,下面说到张三都是说的 B B B班的,那么其他人就知道,后面只要出现张三都是在说 B B B班的,不是 A A A班的了。很多文章里面也有这样的情况啊,一般注明是以下简称什么什么的,就是为了避免重复,导致混淆。而程序一样的,你可以先告诉编译器你用的 s t r i n g string string是哪个命名空间的。就要用到这句了。 u s i n g    n a m e s p a c e    s t d using\,\, namespace\,\, std usingnamespacestd;这样告诉编译器,我没有指定命名空间的,就默认使用std这个命名空间,那么你使用 s t r i n g string string就不用再加 s t d : : std:: std::作用域了。只需要直接写string就可以了,编译器就知道你说的是哪个 s t r i n g string string了。这就是命名空间的作用。

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐