我已经告诉别人,编写using namespace std;代码是错误的,我应该用std::cout和std::cin直接代替.
为什么被using namespace std;认为是不好的做法?是低效还是冒着声明模糊变量(与名称std空间中的函数具有相同名称的变量)的风险?它会影响性能吗?
我正在研究一个基本的shell,但是在下面的循环中,程序没有超过标记的行(它会立即循环).当我将其注释掉时,整个块在再次循环之前完成.这里发生了什么?
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main(int argc, char *argv[]) {
string input;
const char *EOF="exit";
string prompt=getenv("USER");
prompt.append("@ash>");
while(true) {
int parent=fork();
if ( !parent ) {
cout << prompt; //The program never gets past this point
getline(cin,input);
if (!input.compare(EOF))
exit(0);
cout << input << '\n';
execlp("ls", "-l", NULL);
return 0;
}
else
wait();
}
}
Run Code Online (Sandbox Code Playgroud)