C++ [The meaning of "using namespace std", the difference between #include "iostream" and #include <iostream>, the difference between <iostream> and <iostream.h>]

table of Contents

1. The meaning of "using namespace std"

2、#include "iostream" 与 #include < iostream>区别

3. The difference between <iostream> and <iostream.h>


1. The meaning of "using namespace std"

Before standard C++, #include<iostream.h> was used because the name of the header file to be included was iostream.h.

Standard C++ introduces the concept of name space, and encapsulates things in the standard library such as iostream into the std name space. At the same time, in order not to be confused with the original header file, it is stipulated that standard C++ uses a new set of header files. Do not add the .h extension after the file name, such as iostream, string, etc., and rename the header files of the original C standard library, such as the original string.h, change it to cstring (that is, remove .h and add The letter c), so the wording of include in the header file becomes #include.

It's not that you have to use using namespace std when you write #include; the reason we usually write this way is to expose all the stuff in the std namespace to the global domain at once (like directly including iostream.h without a name The header file of the space is the same), making the standard C++ library as convenient as the traditional iostream.h. If you don't use using namespace std; when using the standard library, you must always bring the full name of the namespace, such as std::cout << "hello" << std::endl;

2、#include "iostream" 与 #include < iostream>区别

The former first searches for the iostream file in the current directory, and then searches for the system header file path if it cannot be found, the latter vice versa. Therefore, as a good habit, try to use <> when including system header files, and use "" when including header files in your own project.

3. The difference between <iostream> and <iostream.h>

The former has no suffix. In fact, you can see in the include folder of the compiler that the two are two files. Open the file and you will find that the code inside is different.

The C++ standard for header files with a suffix of .h has clearly stated that it is not supported. Earlier implementations define the standard library functions in the global space and declare them in the header files with a .h suffix; the
C++ standard has also been distinguished from C. In order to use the namespace correctly, it is stipulated that the header file does not use the suffix .h.

Therefore, when using <iostream.h>, it is equivalent to calling library functions in c, using the global namespace, which is the early C++ implementation; when using <iostream>, the header file does not define the global namespace , Namespace std must be used; in this way, cout can be used correctly.

#include <iostream.h>
using namespace std; // then an error occurs,


so either write it as

#include <iostream>
using namespace std;


or written as

#include <iostream.h>

Of course, the former is best.

Guess you like

Origin blog.csdn.net/weixin_44949135/article/details/115284087