【发布时间】:2015-10-14 10:16:54
【问题描述】:
我正在关注 C++ Primer 书籍并尝试所有代码示例。 我对这个很感兴趣:
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
string line;
while (getline(cin,line))
cout << line << endl;
return 0;
}
在编译这段代码之前,我猜测编译会失败,因为我没有使用
while (std::getline(cin,line))
为什么 getline 在全局命名空间中? 据我了解,这只有在我使用时才会发生
namespace std;
或
using std::getline;
我在 Linux Mint Debian 版上使用 g++ 版本 4.8.2。
【问题讨论】:
标签: c++ namespaces getline