相关疑难解决方法(0)

为什么"使用命名空间std"被认为是不好的做法?

我已经告诉别人,编写using namespace std;代码是错误的,我应该用std::coutstd::cin直接代替.

为什么被using namespace std;认为是不好的做法?是低效还是冒着声明模糊变量(与名称std空间中的函数具有相同名称的变量)的风险?它会影响性能吗?

c++ namespaces using-directives std c++-faq

2486
推荐指数
36
解决办法
78万
查看次数

为什么cout会阻止后续代码在这里运行?

我正在研究一个基本的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)

c++ string cout

1
推荐指数
1
解决办法
140
查看次数

标签 统计

c++ ×2

c++-faq ×1

cout ×1

namespaces ×1

std ×1

string ×1

using-directives ×1