Using namespace std details

Source: Internet
Author: User

I:

<Iostream> and <iostream. h> It is different. The former has no suffix. In fact, you can see in the include folder of your compiler that the two are two files. When you open the file, you will find that the code inside is different.

Suffix. h header file c ++ standards have been explicitly proposed not to support, earlier implementation of the standard library function defined in the global space, declared in the. in the header file with the suffix h, the c ++ standard is used to distinguish it from C, and to correctly use the namespace, it is required that the header file does not use the suffix. h.

Therefore, when <iostream. h> when <iostream> is used, it is equivalent to calling the library function in c and using a global namespace, that is, the early c ++ implementation, the header file does not define a global namespace. You must use namespace std; To use cout correctly.

II:

Namespace refers to various visible ranges of identifiers.

All identifiers in the C ++ standard library are defined in a namespace named std.

Due to the concept of namespace, when using any identifier of the C ++ standard library, there are three options:

1. Specify the identifier directly.For example, std: ostream instead of ostream. The complete statement is as follows:

Std: cout <std: hex <3.4 <std: endl;

2. Use the using keyword.

Using std: cout;
Using std: endl;

The above program can be written

Cout <std: hex <3.4 <endl;

3. the most convenient method is to use using namespace std;

For example:

# Include <iostream>
# Include <sstream>
# Include <string>
Using namespace std;
In this way, all identifiers defined in the namespace std are valid (exposed ). They are declared as global variables. The preceding statement can be written as follows:

Cout

Because the standard library is very large, the programmer may have the same name as a standard library when selecting the class name or function name. To avoid name conflicts caused by this situation, everything in the standard library is put in the namespace std. However, this poses a new problem. Countless original C ++ Code relies on features in the pseudo-standard library that have been used for many years. They are all in the global space.

Therefore, the header files such as <iostream. h> and <iostream> are designed to be compatible with the previous C ++ code, and to support new standards.

The namespace std encapsulates the name of the standard library. to distinguish the standard library from the previous header files, ". h" is generally not added"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.