C++, three ways to use elements in the std namespace

1. Use std:: prefix

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

2. Use the using directive

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

Among them, the prefixes in front of cout and endl are gone, but we see a new statement:

using namespace std;

The using directive allows us to directly gain access to elements in the std namespace. It is a very concise way to access these elements (especially when the number of visits is large).

3. Use the using statement

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

Clearly state which elements in the STD namespace are localized to the program.
Not as concise as the using directive. However, the advantage is that the elements in the planned namespace are clearly specified, and elements that are unintentionally used are not localized.

Guess you like

Origin blog.csdn.net/weixin_51236357/article/details/112614092