【C++】C++中“std::“是什么意思?起什么作用?

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

S T D STDSTD定义

s t d stdstd是一个类(输入输出标准),它包括了c i n cincin成员和c o u t coutcout成员,“u s i n g n a m e s p a c e s t d using\,\,namespace\,\,stdusingnamespacestd ;”以后才能使用它的成员。# 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 stdstd,但是他有c i n , o u t cin,outcin,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\,\, stdusingnamespacestd;”就是这个功能,然后你就可以使用c i n , c o u t cin,coutcin,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::cinstd::cin或者s t d : : c o u t std::coutstd::cout再去使用它的成员函数(因为头文件中存在这个类)所谓n a m e s p a c e namespacenamespace,是指标识符的各种可见范围C + + C++C++标准程序库中的所有标识符都被定义于一个名为s t d stdstdn a m e s p a c e namespacenamespace中。

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

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

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

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

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

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

  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\,\,stdusingnamespacestd
例如:

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

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

cout << hex << 3.4 << endl;

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


版权声明:本文为liuyiming2019原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。