Why should I include the <iostream> header file after using the std namespace?
When you execute #include <iostream>, this leads to the inclusion of a set of classes and other things in the source file. For iostream and most standard library headers, they put these objects in a namespace with a name std.
, #include <iostream> :
namespace std {
class cin { ... };
class cout { ... };
class cerr { ... };
class clog { ... };
...
}
, , :
#include <iostream>
int main() {
std::cout << "hello\n";
return 0;
}
, std::cout . :
#include <iostream>
using namespace std;
int main() {
cout << "hello\n";
return 0;
}
, , std::cout , , using.
#include <iostream>
using std::cout;
int main() {
cout << "hello\n";
return 0;
}
, using namespace std, stackoverflow:
, ( std - ). .
using namespace std; , " - , std".
#include <iostream> , , iostream . cin, cout . namespace std { ... all the stuff goes here ... }.
- , namespace math;, ", , , cin - , - ?".
, . ++ - , , , . , .
(, using namespace std;, std::cout << "Hello, World!" << std::endl; - , cout, , std one, - . , , , , , std, , llvm - using namespace llvm; , , Type* p = ...; LLVM .)
Using the #include preprocessor directive is aging like C ++. And he doesn’t leave earlier. The C ++ namespace does not import anything into your program; it just defines the scope of your specific header file function. Therefore, both are required. Click here to understand why use the namespace.