C++中的std::

  • std:: 是个空间标识符;
  • C++标准库中的函数/对象都是在命名空间std中定义的 ,所以每当我们要使用标准函数库中的函数/对象时,都要使用std来限定;
  • 对象cout是标准函数库所提供的对象,而标准库在名字空间中被指定为std,所以在使用cout的时候要加上std::
    示例如下:
#include<iostream.h>
 
int main(){
   std::cout<<"Hello world"<<std::endl;
return 0;
}

下面没有在cout和endl的前面加上std::就出错了
错误 C2065 “endl”: 未声明的标识符 C2065 “cout”: 未声明的标识符

#include<iostream>
int main()
{
    cout << "Hello world" << endl;
}

而使用非标准库头文件<iostream.h>可不用写,iostream是C++的头文件,iostream.h是C的头文件,即标准的C++头文件没有.h扩展名)

#include<iostream.h>
int main()
{
    cout << "Hello world" << endl;
}
  • 直接在main函数下面输入 using std::cout; using std::endl; 这种方法也可以

  • 或者直接在头文件下面加上using namespace std;即

#include <iostream>
using namespace std;
int main() {
    int n;
    cin >> n;
}