Why should I include the <iostream> header file after using the std namespace?

Since the std namespace already has C ++ libraries containing function definitions (if I'm right), why do we include header files on top of this? Since the std namespace includes the standard C ++ libraries, I see no reason to include its declarations separately.

+4
source share
4 answers

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:

+6

, ( 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 .)

+5

... ???

, .

- , .

, .

, , std.

++ . using.

()

, , . , .

, sort advance algorithm. algorithm.

++ " , ", , , , .

, , , .

(, ).

, , ++ .

, " , ".

, ++ , , , , , . , .

. Monolith. , include, , , . windows.h - . , . , .

0
source

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.

0
source

All Articles