C++,使用std名称空间中元素的三种方式

1.使用std::前缀

#include<iostream>
int main(){
    
    
std::cout<<"使用std::前缀"<<std::endl;
return 0;
}

2.使用using指令

#include<iostream>
using namespace std;
int main(){
    
    
cout<<"使用using指令"<<endl;
return 0;
}

其中,cout和endl前面的前缀不见了,但是我们看见一条新的语句:

using namespace std;

using指令让我们直接获取std名称空间中元素的访问权。是访问这些元素十分简洁的方式(当访问次数很多的时候尤为明显)。

3.使用using声明

#include<iostream>
using std::cout;
using std::endl;
using namespace std;
int main(){
    
    
cout<<"使用using声明"<<endl;
return 0;
}

明确声明STD名称空间中哪些元素对程序本地化。
没有using指令那样简洁。但是,优势在于清晰地指明了计划使用的名称空间中的元素,不会将无意使用的元素本地化。

猜你喜欢

转载自blog.csdn.net/weixin_51236357/article/details/112614092