#include <iostream>
void func(){
//必须重新声明
using namespace std;
cout<<"Hello World"<<endl;
}
int main(){
//声明命名空间std
using namespace std;
cout<<"C++"<<endl;
func();
return 0;
}
#include <iostream>
//声明命名空间std
using namespace std;
void func(){
cout<<"Hello World"<<endl;
}
int main(){
cout<<"C++"<<endl;
func();
return 0;
}
C++很多教程中都是这样做的,将 std 直接声明在所有函数外部,这样虽然使用方便,但在中大型项目开发中是不被推荐的。
这样做增加了命名冲突的风险,推荐在函数内部声明 std。